The Logic of A Ship Flying in Space

⌘-R in Chief
Posts: 1,278
Joined: 2002.05
Post: #1
Ok, well I had an explanation of how I was thinking about going about it, but I just realized it wouldn't work even if I was doing the math correctly.

So what's the logic behind it? I was thinking about using vector combination, but I don't think it works quite right.
Quote this message in a reply
Posts: 715
Joined: 2003.04
Post: #2

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 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.
Quote this message in a reply
Posts: 869
Joined: 2003.01
Post: #3
I would say you have a position vector P, a velocity vector V, and thrust vector A. And you have some heading (maybe also a vector, lets say H). In a game you work with some time steps, dT. I will do my explanation without external influence. We can safely assume that you can rotate the ship, and the thrust vector always points to the back.

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.
Quote this message in a reply
Post: #4
How I'd do it is have a simple object struct of some sort, maybe something like this:

[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));

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 this message in a reply
⌘-R in Chief
Posts: 1,278
Joined: 2002.05
Post: #5
Quote:Originally posted by Dave
Couldn't be easier...

Huh. Looks simple enough and logically it works I guess, so lemme go fiddle Smile
Quote this message in a reply
⌘-R in Chief
Posts: 1,278
Joined: 2002.05
Post: #6
I'm assuming that second Cos should be Sin, but doing that naturally makes the ship go down instead of up and up instead of down so I flipped that but it still doesn't work quite correctly. It has problems starting and stopping. For example, when you start to move, you might move sideways first then start moving at an angle and it's quite obvious.

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.
Quote this message in a reply
⌘-R in Chief
Posts: 1,278
Joined: 2002.05
Post: #7
Oh, well I guess I won't move this to a new topic yet, but how would I handle an asteroid exploding? do I just randomize the direction of the particles either 60 degrees to the right or left of the angle of the projectile? Sounds like it would work....
Quote this message in a reply
Post Reply