## The Logic of A Ship Flying in Space

So what's the logic behind it? I was thinking about using vector combination, but I don't think it works quite right.

You have forward thrust. If using physics then it just keeps traveling at that speed until it hits something. If you have more advanced physics then you have gravity pulling on it...in any and all directions, which will either speed it up or slow it down to the point of reverse.

Other thrust vectors are possible with stabilizers...which would be active to combat the effects of gravity so the ship isn't pulled off course.

"Finest Programmer iDevGames ever saw and he winds up playing space cowboy in some back room"

Try Vendetta for very cool space game play, in physics mode I find it irritating, but it tends to be the winning way.

Every frame:

0) adjust heading by user input.

1) find the normalized heading vector H

2) Calculate thrust by: A = -(H*strength)

3) Calculate the effect of the thrust on V: V = V+A*dT

4) Calculate position: P = P+V*dT

And that's about it. If you have external forces, it is probably the best idea to add them in step 3.

[SOURCECODE]typedef struct

{

float x; //x position of object

float y; //y position of object

float dx; //movement vector

float dy;

float mass; //Mass

} object2d;

And have a couple functions to go with it:

void updateObject(object2d *theobject)

{

theobject->x = theobject->x + theobject->dx;

theobject->y = theobject->y + theobject->dy;

}

void Force(object2d *theobject, float direction, float magnitude)

{

theobject->dx = theobject->dx+(cos(direction)*(magnitude/mass));

theobject->dy = theobject->dy+(cos(direction)*(magnitude/mass));

}[/SOURCECODE]

Couldn't be easier, now you just put in the direction and amount you want the object to be forced. Note, depending on how you want to do it, you may need some degree/radian conversion macros, and may need to automatically add or subtract a fraction of a rotation depending on where you want 0 rotation to be pointing.

I'm not sure why there are extra semicolons in the code...just ignore them. They aren't in the actual code I see in my post field, so..*shrug*

Quote:Originally posted by Dave

Couldn't be easier...

Huh. Looks simple enough and logically it works I guess, so lemme go fiddle

Ah wait.... just fixed that. An acceleration rate of 3 pixels a second (or .1 per frame at 30fps) was too little. Doubling this value made the acceleration smooth. I'm assuming this was because the low float values converting to integers was "0" instead of 1 to make the direction more angular.

Well thanks. I think I got it working.