Sign in to follow this  
Halsafar

2D Shooting Bullets

Recommended Posts

Halsafar    205
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 this post


Link to post
Share on other sites
matches81    474
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Add the ship's velocity to the bullet's initial velocity when you fire the bullet.

Share this post


Link to post
Share on other sites
Bursar    142
Quote:
Original post by Anonymous Poster
Add 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 this post


Link to post
Share on other sites
benishor    164
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 this post


Link to post
Share on other sites
Jumpster    122
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this