# 2D Shooting Bullets

This topic is 4654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay I got an Asteroid ship and I'm trying to get the shooting to feel right. I just cannot get it right... So far this is all I do: //Get a velocity the angle the user is pointing //Divide by 2 to slow it down double dFVx = cos(m_iAngle) / 2.0; double dFVy = sin(m_iAngle) / 2.0; //A bullet is then created and on its update it m_BulletVelocity.set(dFVx * timeDelta, dFVy * timeDelta); m_BulletPos.add(m_BulletVelocity); A lot of problems arise from this simplicity... I can catch up to my bullets for example which shouldn't happen.

##### Share on other sites
the bullet´s velocity should be higher than the maximum velocity of the ship. If you create the bullet a bit in front of your ship, you shouldn´t be able to catch it, since the bullet is faster than the ship would ever be?

##### Share on other sites
Add the ship's velocity to the bullet's initial velocity when you fire the bullet.

##### Share on other sites
Quote:
 Original post by Anonymous PosterAdd the ship's velocity to the bullet's initial velocity when you fire the bullet.

But if the ship is stationary when the bullet is fired, and then the player accelerates, he'll overtake the bullet and you're back to square one. You'll also have bullets travelling at different speeds as the player zooms around the screen.

##### Share on other sites
don't allow ship velocity to be greater than bullets'. the angle the ship is pointing to should only give you the unit velocity. multiply that with an amount and don't allow your ship's velocity to reach that amount

e.g :

// given a 2D vector m_ShipPosition and a m_ShipAngle value

Vector2D theBulletPosition, theBulletVelocity, theBulletDirection;

theBulletDirection.Set( cos( m_ShipAngle ), sin( m_ShipAngle ) );
theBulletVelocity = theBulletDirection * 5.0f; // bullet is 5 times faster than unit movement

theBulletPosition = m_ShipPosition;
theBulletPosition += theBulletDirection * 2.0f; // initially put bullet in front of ship

on update :

theBulletPosition += theBulletVelocity * timeDelta;

##### Share on other sites
Determine how fast you want your bullet to fly across the screen in PixelsPerSecond. So if you have 800x600 resolution, and you want the bullet to take 3 seconds to cross the screen (left to right), then velocity would be 800/3 = 267 pps. Between frames, I assume you keep track of frame timing, set the bullet's velocity to:

BaseVelocityX = (267 * FramingTimingMS);
BaseVelocityY = (200 * FramingTimingMS);

Assuming a 30fps, FramingTimingMS would be about 33ms or .033 giving a BaseVelocityX of 8.8ppf and BaseVelocityY of 6.6ppf where ppf = PixelsPerFrame.

Note: Moving Left to Right/Top to Bottom = +ppf. Moving Right to Left / Bottom to Top = -ppf.

Of course, for this to feel right, you'd have to take the same approach for your ship's movement and the individual asteroids as well. But, this will give a consistant feel between frames, regardless of the movement/angle of the ship, asteroids etc.

This is how I'd go about it anyway, good luck and happy gaming!

Regards,
Jumpster

1. 1
Rutin
26
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 21
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631763
• Total Posts
3002189
×