# 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.

## 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 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 on other sites
thx
i guess i was overcomplicating it... i looked up on parabolic trajectory xDDD
works now

1. 1
2. 2
3. 3
Rutin
12
4. 4
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633695
• Total Posts
3013383
×