LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   a small game design algorithm (https://www.linuxquestions.org/questions/programming-9/a-small-game-design-algorithm-161314/)

feetyouwell 03-23-2004 08:08 AM

a small game design algorithm
 
I am designing a game for my modeling class. I am using the free game (battle???) from most linux distro. The game is pretty simple, two airship shoot each other in space, and if it hits the boarder, it wraps around.

I am looking for a good algorithm to program the movements of the airship. I am using vb6 to program the game ( i know i know, but it's a class requirement) and I have created 20 pictures for the rotation of the airships. For example, airship1 is point to the top, then airship2 is airship1 rotating 18 degree to the right, airship3 is airship2 rotate 18 to the right and so on.

What I want user to be able to do is that they use the arrow up/down to control the speed of the airship, arrow left/right to rotate the airship --> control the direction which way the airship is going. Ex, if usr just presses arrow left twice, the the airship will rotate 32 degrees to the right, if user just presses arrow up twice, the airship will move upwards with a certain speed (i have done that part). if user press arrow up and arrow right, then the airship will move 18 degree to the right with a certan speed going forward.

Does anyone know a general algorithm that will work for this? I don't need the actual code, just the algorithm. Any help will be greatly apprecated!

kev82 03-23-2004 11:48 AM

ok, so you have the engine thrust(changed with up/down) which will be proportional to acceleration, and you have an angle measured from north. so you can break the acceleration into x and y components something like A_x = Thrust*sin(Angle), A_y = Thrust*cos(Angle). the new velocity of the ship is u+At where u is the old velocity and t is the time in which the ship has undergone the acceleration for simplicity set this to one frame, but you should consider measuring in real time rather than frames as real time is not affected by computer speed. then do the same thing with position. new position = old position + velocity*time.

i dont think ive explained very well, youd probably be best to ask a maths or physics teacher about 2d newtonian mechanics/projectile motion. i can give some example code if it would help.

feetyouwell 03-23-2004 12:23 PM

if you would, that would be great. I will take a more close look to what you said tonite. Thanks for responding, man.

aluser 03-23-2004 02:58 PM

Since the game comes with most linux distros, it's probably open source --- all the algorithms you need are guaranteed to be in there : )

kev82 03-23-2004 03:08 PM

its in c but should demonstrate the basic idea, each frame you call nextFrame passing the thrust of the engines(changed by up/down) and the angle of the ship(changed with left/right) and it updates the position and velocity vectors.

Code:

#include <stdio.h>
#include <math.h>

typedef struct {
        float x,y;
} vect2d;

vect2d pos;
vect2d vel;

void vect2dadd(vect2d *a, vect2d *b)
{
        a->x += b->x;
        a->y += b->y;
}

void vect2dclear(vect2d *a)
{
        a->x = 0;
        a->y = 0;
}

void nextFrame(float thrust, float angle)
{
        vect2d accel;

        accel.x=thrust*sin(angle);
        accel.y=thrust*cos(angle);

        vect2dadd(&vel,&accel);
        vect2dadd(&pos,&vel);
}

int main()
{
        float t, a;
        vect2dclear(&pos);
        vect2dclear(&vel);

        while(1) {
                printf("pos-(%f,%f) vel-(%f,%f)\n", pos.x, pos.y, vel.x, vel.y);
                printf("Enter thrust:");
                scanf("%f", &t);
                printf("Enter angle:");
                scanf("%f", &a);

                nextFrame(t,a);
        }

        return 0;
}



All times are GMT -5. The time now is 12:20 AM.