**0**

# Bullet Rotation

Started by Jan 07 2010 02:34 PM

,
5 replies to this topic

###
#1
Members - Reputation: **100**

Posted 07 January 2010 - 02:34 PM

I am using pyBox2D for my game. My problem is when the player shoots the bullet's angle never changes on the path (i.e I shoot a bullet into the air, its angle stays the same through out its life, whereas in real life if a bullet is fired into the air at its highest point it will do a "180" degree flip)
How can achieve this effect?

###
#2
Crossbones+ - Reputation: **19900**

Posted 07 January 2010 - 11:58 PM

Quote:

Original post by KillaChaos0

I am using pyBox2D for my game. My problem is when the player shoots the bullet's angle never changes on the path (i.e I shoot a bullet into the air, its angle stays the same through out its life, whereas in real life if a bullet is fired into the air at its highest point it will do a "180" degree flip)

I don't think it will. If the bullet is fired straight up, once it loses enough speed it will fall wobling chaotically. If fired at a lower angle, it retains a ballistic trajectory and then the bullet's orientation follows the trajectory, as you would expect. There is a MythBusters episode about it.

###
#4
Crossbones+ - Reputation: **2194**

Posted 08 January 2010 - 07:01 AM

Quote:

Original post by KillaChaos0

I am using pyBox2D for my game. My problem is when the player shoots the bullet's angle never changes on the path (i.e I shoot a bullet into the air, its angle stays the same through out its life, whereas in real life if a bullet is fired into the air at its highest point it will do a "180" degree flip)

How can achieve this effect?

if you have access to the bullet velocity, add gravity to it (bullet.velocity += gravity +* dt).

if you have access to forces acting on a bullet, add a gravity force to it (bullet.addForce(gravity * bullet.mass)).

If you have wind and air resistance, you can do something similar as well.

Everything is better with Metal.

###
#5
Members - Reputation: **181**

Posted 08 January 2010 - 06:51 PM

You can use kinematic equations.

Assuming you have a initial launch velocity and an angle, then you can do something like this,

as you see if the launchAngle is 90 degree then there will be no velocity in

the x direction because cosine of 90 degrees is 0. That means all the force goes

in the y-direction.

Also here :

The "- 1.0f/2.0.f * (accel) *(dt * dt) " factor will eventuall get bigger than

velocityMagnitudeInY, then that means it will eventually decrease; which in

turns means that bullet.y will eventually decrease.

[Edited by - Concentrate on January 10, 2010 9:51:56 PM]

Assuming you have a initial launch velocity and an angle, then you can do something like this,

const float accel = -9.8 * 0.001f; //factor

const float initLaunchVelocity = 4.0f;

const float launchAngle = 90.0f; //degree

const float velocityMagnitudeInX = initLaunchVelocity * cos(launchAngle);

conat float velocityMagnitudeInY = initLaunchVelocity * sin(launchAngle);

const float dt = 1; [EDIT : dt should be the elapsed time, sorry ]

bullet.x += velocityMagnitudeInX * dt;

//Y = vt - 1/2at^2

bullet.y += velocityMagnitudeInY * dt - 1.0f/2.0.f * (accel) *(dt * dt );

as you see if the launchAngle is 90 degree then there will be no velocity in

the x direction because cosine of 90 degrees is 0. That means all the force goes

in the y-direction.

Also here :

bullet.y += velocityMagnitudeInY * dt - 1.0f/2.0.f * (accel) *(dt * dt );

The "- 1.0f/2.0.f * (accel) *(dt * dt) " factor will eventuall get bigger than

velocityMagnitudeInY, then that means it will eventually decrease; which in

turns means that bullet.y will eventually decrease.

[Edited by - Concentrate on January 10, 2010 9:51:56 PM]

###
#6
Crossbones+ - Reputation: **2466**

Posted 09 January 2010 - 11:29 AM

Never heard of pyBox2D, but the following should hopefully apply regardless of what you're using.

Simply simulate the effects of gravity and perhaps air-resistance on your bullet, just as you would with any other object in a realistic simulation.

In addition to doing

Tweak both constants to suit your aplication.

E.g. Assuming you use 1.0 for one metre and your bullets travel at realistic speeds, GRAVITY (on earth) is about 9.8 which we divide by the number of game logic updates per second, since we apply it that many times. Assuming 50 updates per second, GRAVITY = 9.8 / 50 => 0.196

The AIR_RESISTANCE factor makes sure that the object doesn't exceed terminal velocity, or increase in speed forever.

It should be a number just a smidgen below 1.0, though the actual value again depends on the update rate (in a more complex way).

Say you want your speed to drop by 5% (i.e. 0.05) per second, and your update rate again is 50 updates per second.

AIR_RESISTANCE = (1 - 0.05) ^ (1 / 50) => 0.998974...

Your bullet angle is calculated from the arctan of it's x and y velocity, which iirc is: BulletAngle = atan2(velocity.y, velocity.x);

avaro is corrct though that if fired directly upwards then there is no particular way that it would turn to reorient itself towards the ground, and it will in fact flop all around for a moment, eventually settling into whatever orientation provides the least air-resistance, and depending on its angular momentum it may even result in

Edit: While Concentrate has tried to make something useful out of kinematic equations, what he posted definitely isn't right. He has confused a fixed time step with elapsed time, and confused actual height with accumulated height, and as such what he posted has unfortunately not come out at all correct.

Even with the correct usage of the formula, the equation would describe a parabolic path of the projectile. However that formula is only correct for when air-resistance is negligible, and with bullets at high velocities, air-resistance is far from negligible. In fact it may even have more effect than gravity initially! A bullet fired off a cliff with the formula used correctly, would hit the ground far below at an impossible speed. Not to mention that you'd still have to differentiate the function in order to get the actual angle. Needless to say that it is not the right approach.

[Edited by - iMalc on January 9, 2010 5:29:24 PM]

Simply simulate the effects of gravity and perhaps air-resistance on your bullet, just as you would with any other object in a realistic simulation.

In addition to doing

position += velocity;for the bullet each frame, also do:

velocity.y += GRAVITY;It's that easy!

// and

velocity *= AIR_RESISTANCE;

Tweak both constants to suit your aplication.

E.g. Assuming you use 1.0 for one metre and your bullets travel at realistic speeds, GRAVITY (on earth) is about 9.8 which we divide by the number of game logic updates per second, since we apply it that many times. Assuming 50 updates per second, GRAVITY = 9.8 / 50 => 0.196

The AIR_RESISTANCE factor makes sure that the object doesn't exceed terminal velocity, or increase in speed forever.

It should be a number just a smidgen below 1.0, though the actual value again depends on the update rate (in a more complex way).

Say you want your speed to drop by 5% (i.e. 0.05) per second, and your update rate again is 50 updates per second.

AIR_RESISTANCE = (1 - 0.05) ^ (1 / 50) => 0.998974...

Your bullet angle is calculated from the arctan of it's x and y velocity, which iirc is: BulletAngle = atan2(velocity.y, velocity.x);

avaro is corrct though that if fired directly upwards then there is no particular way that it would turn to reorient itself towards the ground, and it will in fact flop all around for a moment, eventually settling into whatever orientation provides the least air-resistance, and depending on its angular momentum it may even result in

*spiraling*downwards. However, from my understanding, there isn't any need to go into that much detail in your case.Edit: While Concentrate has tried to make something useful out of kinematic equations, what he posted definitely isn't right. He has confused a fixed time step with elapsed time, and confused actual height with accumulated height, and as such what he posted has unfortunately not come out at all correct.

Even with the correct usage of the formula, the equation would describe a parabolic path of the projectile. However that formula is only correct for when air-resistance is negligible, and with bullets at high velocities, air-resistance is far from negligible. In fact it may even have more effect than gravity initially! A bullet fired off a cliff with the formula used correctly, would hit the ground far below at an impossible speed. Not to mention that you'd still have to differentiate the function in order to get the actual angle. Needless to say that it is not the right approach.

[Edited by - iMalc on January 9, 2010 5:29:24 PM]