• Advertisement

• ### Popular Now

• 13
• 17
• 27
• 9
• 9
• Advertisement
• Advertisement
• Advertisement

# 2D rocket science... literally

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

##### Share on other sites
Advertisement
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

##### Share on other sites
thx
i guess i was overcomplicating it... i looked up on parabolic trajectory xDDD
works now

#### Share this post

##### Share on other sites

• Advertisement