Jump to content
  • Advertisement
Sign in to follow this  

2D rocket science... literally

This topic is 2891 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

hi im working on a worms clone game and am stuck with letting shots fly as they are supposed to.
given a starting position and an angle to indicate the direction, how would you compute the path for the rocket?

Share this post

Link to post
Share on other sites
Okay given an angle you can create a unit vector by doing (cos(angle), -sin(angle)) assuming y is positive going down. A unit vector is a vector with length 1. It's essentially a direction. You can create the rocket at that position. How realistic do you want the rocket to be? Is it a seeking rocket?

Assuming it's not a seeking rocket with no thrust you just give the rocket a velocity in the direction already found. You can scale the direction vector by multiplying both of the components by the magnitude for the shot.

rocket.position.X = player.X;
rocket.position.Y = player.Y;
rocket.velocity.X = cos(angle) * magnitude;
rocket.velocity.Y = -sin(angle) * magnitude;

Then just apply gravity every tick to the rocket by doing:
rocket.velocity.Y += 0.98; or some magic number.
rocket.position.X += rocket.velocity.X;
rocket.position.Y += rocket.velocity.Y

Then for rendering make the rocket face the direction of its velocity. First normalize the velocity and take the atan2(y, x) if you need the angle. You'll need to explain what you're using if you need help with that though.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!