Jump to content
  • Advertisement
Sign in to follow this  

Ship Movement ala Asteroids

This topic is 3870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm looking for a good tutorial on ship movement in a 2D plane ala asteroids. I've looked through the articles and didn't find anything there. Thanks for the assist.

Share this post

Link to post
Share on other sites
the ultra short version:

When you hold down the thust button, a force is applied to the ship. The force direction must be opposite the ships angle, like this:

force in x plane: force * sine to ship angle
force in y plane: force * -cosine to ship angle


Share this post

Link to post
Share on other sites
It's really basic physics:

r = (rx,ry) - The location vector of your ships center.
v - The velocity vector.
a - Acceleration
f - Force
m - Mass of the ship
α - The direction angle of the ship.

a = f/m
f = (cos α * f, sin α * f), where f is your chosen magnitude for the force. f = 0, when the thrust key is not being pressed.

You run your simulation in time steps, preferably of uniform length, e.g. once every 40th of a second. Let Δt be the length of the time step.
You need to numerically integrate r from a. It's easiest to do it by simply adding each derivative, multiplied by Δt to the old value:
v' = v + a*Δt
r' = r + v'*Δt
v' and r' are the new values.

Share this post

Link to post
Share on other sites
Thanks for the replies! OK, I know this is basic physics, but for some reason my tiny little brain is refusing to just simply get it. Here's what I have so far in code:

//Thrust method for my ship class:
public void Thrust()
//Take current angle, convert to radians and figure out X,Y components of that angle.
//Then increase thrust based on Acceleration.
//Need to limit thrust still.
ConvertedAngle = OrbMech.ConvertAngle(Angle);
Rads = OrbMech.ConvertDegreesToRadians(ConvertedAngle);
Xvel += (Acceleration * (float)Math.Cos(Rads));
Yvel += (Acceleration * (float)Math.Sin(Rads));
vecMovement.X = (float)Xvel;
vecMovement.Y = (float)Yvel;

//Movement method which gets called each OnPaint:
public void Move()

//And then update that gets called on each Render cycle:
//Update ship current location
matrixLocation = matrixRotation * matrixMovement;

What I get is the ship will move, but essentially I'm just telling it (via keyboard) to go to X,Y location. There's certainly no drift involved and when I lift off the keys it just stops.

I know this is the interwebs but if anyone wants to take a look at the .exe it can be found here: http://solprominence.com/sp.exe (so that you can see what I've got). Adjust speed and turn rates with +, -, /, * on the keypad and reset everything with TAB.


[Edited by - LazySumo on April 9, 2008 2:17:19 PM]

Share this post

Link to post
Share on other sites
Yup. I did a very similar thing with a helicopter game I wrote.

It's pretty simple. Basically you need to store a value of the ship's current velocity.
At each loop of your input you need to check if you're accelerating (i.e. your finger is on the accelerate button).

if (theButtonToAccelerateIsBeingHeldDown) //lol
current_velocity += acceleration_value;
current_velocity -= acceleration_value;

Of course you need to check if the current_velocity is at or above the maximum velocity (we wouldn't want to keep going faster and faster, although it is pretty amusing to watch).
You also need to check for stopping as well. What mine did at one stage was slow down, and then go in reverse!

It'll be a fun learning experience for you. I hope you have as much fun as I did when I was coding it.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!