# Aligning bullet's position in Asteroid Game

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:

gl::drawLine(Vec2f(-43,-52),Vec2f(59,1));
gl::drawLine(Vec2f(-14,-31),Vec2f(-14,38));
gl::drawLine(Vec2f(59,1),Vec2f(-43,54));

• In general drawing has nothing to do with your physics "engine". You calculate objects in there and then just draw them as they are. Aug 2, 2012 at 9:58
• yes, but how to draw the bullet on the tip of the ship ? what math calculations needed ? Aug 2, 2012 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;


and

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.

• 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. Aug 2, 2012 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 :) Aug 2, 2012 at 12:16
• You can use whatever system you want. His code is correct - clarify what's wrong? You marked it as the answer. Aug 2, 2012 at 14:28
• i.imgur.com/g7OM3.jpg Aug 2, 2012 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. Aug 4, 2012 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.