Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

share|improve this question
In general drawing has nothing to do with your physics "engine". You calculate objects in there and then just draw them as they are. – Krom Stern Aug 2 '12 at 9:58
yes, but how to draw the bullet on the tip of the ship ? what math calculations needed ? – Ahmed Saleh Aug 2 '12 at 10:45
up vote 1 down vote accepted

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.

share|improve this answer
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. – Vaughan Hilts Aug 2 '12 at 12:11
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 :) – Vladimir Mitrovic Aug 2 '12 at 12:16
You can use whatever system you want. His code is correct - clarify what's wrong? You marked it as the answer. – Vaughan Hilts Aug 2 '12 at 14:28
1 – Vladimir Mitrovic Aug 2 '12 at 15:00
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. – 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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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