2
\$\begingroup\$

I'm making an Asteroids-like game. I've successfully implemented linear bullets.

Now I want to add some rocket-styled bullets, with a certain flight path: The rockets should be "dropped" back first, then accelerate towards the enemy.

I'm having difficulty making this work when the shooter is rotated. To visualize what I mean:

rocket being shot at an arc, toward up, then toward left

Bullets have a lifeTime which increases every frame.

\$\endgroup\$
5
\$\begingroup\$

A simple way to achieve this effect would be to launch the missile with an initial velocity of (ship's backwards vector + ship's rightwards vector), then apply a constant acceleration of (ship's forward vector) each frame, until it reaches some maximum forward velocity.

Of course, that describes a "dumb" rocket that always follows that (rightward) trajectory. If you're looking for a "homing" rocket, you can achieve that by launching with only (ship's backward vector) as the initial velocity, and applying the acceleration in the direction of the target (instead of just forwards) every frame instead.

If the engine you're using doesn't give you your ship object's directional vectors for free (Unity's transform.forward is a godsend for this sort of task), then you'll have to calculate them yourself. Given the ship's rotation as a number of degrees from some starting angle, and the ship's forward and rightward vectors when at the starting angle, you can calculate the ship's forward and rightward vectors at the specified rotation like this:

rotated_vector_x = original_vector_x * cos(angle) - original_vector_y * sin(angle);
rotated_vector_y = original_vector_x * sin(angle) + original_vector_y * cos(angle);

For further reading, I highly recommend the Wolfire Games blog posts on "Linear algebra for game developers", which begin here. Part 3 contains a description of calculating and applying basis vectors, which is pretty much exactly the problem you're trying to solve here.

\$\endgroup\$
  • \$\begingroup\$ Yeah, I know that, but how do I do this regarding the rotation of the Ship? \$\endgroup\$ – Raildex Oct 18 '15 at 12:55
  • \$\begingroup\$ I've added some detail to my answer regarding a general method for calculating the rotation of a vector. Depending on the game engine you're using, though, it might very well have some convenience methods to do that for you. What language and framework are you developing this game in? \$\endgroup\$ – 4026 Oct 18 '15 at 14:13
  • \$\begingroup\$ I don't use a framework. The bullets/Rockets do have a Velocity for X and Y. And the Position is changed with these two values. x+= dx; y+= dy; I somehow made it work, but the solution is unflexible . this.dx = Math.sin(Math.toRadians(rotation)) *(-20+lifeTime)*0.2; this.dy = -Math.cos(Math.toRadians(rotation)) *(-20+lifeTime)*0.2; this.dx+= Math.sin(Math.toRadians(rotation+90))*(2-lifeTime/20); this.dy+= -Math.cos(Math.toRadians(rotation+90))*(2-lifeTime/20); \$\endgroup\$ – Raildex Oct 18 '15 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.