I'm writing an asteroid game and I would like to align the bullets on the ship's tip. I also want when the ship fires, the bullet gets the same orientation(angle, direction) of the ship and also be fired at a different velocity.

I'm drawing the ship by the following code:

  • \$\begingroup\$ In general drawing has nothing to do with your physics "engine". You calculate objects in there and then just draw them as they are. \$\endgroup\$ – Kromster says support Monica Aug 2 '12 at 9:58
  • \$\begingroup\$ yes, but how to draw the bullet on the tip of the ship ? what math calculations needed ? \$\endgroup\$ – Ahmed Saleh Aug 2 '12 at 10:45

If x and y are origins of your ship, then

angle = atan2 (y, x);

returns its orientation towards (0, 0) which is angle represented in radians. Now, if the tip of your ship is d points distant from its origin, the location where you should draw the bullet (x1, y1) is computed like so:

x1 = cos (angle) * d;


y1 = sin (angle) * d;

As for the bullet's movement, you should propel it like so:

// each tick:
x1 += cos (angle)*speed;
y1 += sin (angle)*speed;

where speed is the speed of the bullet.

EDIT: Just make sure that you compute angle, x1 and y1 only when the player presses the fire button (or clicks or whatever) and not each time the engine ticks, which would be, as Vaughan Hilts huggested, a waste of cycles.

  • \$\begingroup\$ You've defined angle as the ships angle but say to recompute it every frame. Not only is this a waste, if the ship moves all your bullets follow. :) Consider revising to cache value. \$\endgroup\$ – Vaughan Hilts Aug 2 '12 at 12:11
  • \$\begingroup\$ I was just about to add that. :) This was taken almost directly from a proof-of-concept Flash game I was developing in my spare time, and I was calculating angle, x1 and y1 when the player clicked the mouse which then instantiated a new Bullet object with it's own b.angle and b.speed. I'll edit the answer now. Thanks :) \$\endgroup\$ – Vladimir Mitrovic Aug 2 '12 at 12:16
  • 1
    \$\begingroup\$ You can use whatever system you want. His code is correct - clarify what's wrong? You marked it as the answer. \$\endgroup\$ – Vaughan Hilts Aug 2 '12 at 14:28
  • 1
    \$\begingroup\$ i.imgur.com/g7OM3.jpg \$\endgroup\$ – Vladimir Mitrovic Aug 2 '12 at 15:00
  • 1
    \$\begingroup\$ No, bullet's velocity remians the same; it's position in time depends on ship's angle. So, if your ship is rotated by for example 60 degrees and it's speed is 10, your bullet will move every frame cos(60)*10 which is ~ cos(1.01479)*10 in radians or 0.5*10 or 5 in the x direction. For y direction, sin function is used, and you get sin(1.01479)*10 which is ~0.866*10 or 8.66. So, in conclusion, the position of the bullet will be updated by 5 for x and 8.66 for y if your ship's angle is 60 degrees and the bullet's velocity is 10. That's the position on the screen where you should draw it. \$\endgroup\$ – Vladimir Mitrovic Aug 4 '12 at 16:41

You can the trigonometric functions to compute the angle at which the bullet leaves. You can do the same for where it leaves using Cos and Sin. In fact, the velocity vector your bullet should leave is x=cos(angle) * speed. Similarly, the same goes for the y component, just use sin.


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.