Archived

This topic is now archived and is closed to further replies.

Peaceman

physics of a flying bulllet

Recommended Posts

Hi, I''m struggling to implement realistic motion to the bullets fired by my guns. Assuming that I know the velocity at which the bullet is shot, and we are on earth, meaning the g = 9.81, how can I retrieve the position of the bullet on a 2d grid f.e. I hope you understand my problem, thank you in advance to any replies. PS. In case this topic was covered often, I couldn''t do a search as the feature is currently disabled.

Share this post


Link to post
Share on other sites
I presume that you are creating a "tanks" like game (viewed from the side), and you want to get that nice ballistics going.

I also presume that you have a X and a Y velocity (two float values), and you calculate the position of the bullet at a given frame something like this:

NewPosition(x, y) = OldPosition(x, y) + Velocity(x, y) * timeInSecondsPassed;

with all the (x, y) being 2D vectors.

Now, presuming again that the speed stored in these values is in metres/second, just substract 9.81 from the Y value for every second of motion (or an appropriate fraction thereof for every frame). You will have to implement some kind of air viscosity to stop it from accelerating indefinitely.

I hope this helped.

Ciao, ¡muh!

Share this post


Link to post
Share on other sites
thanks for your reply.
Actually I'm programming a fps (I know that this sounds sort of stupid assuming the level of difficulty of this question, but I'm already well off )
Know if I used your solution, then my bullets would be flying straight towards the sky. I'll try to explain with some brilliant ascii-art:

    
/
/
____/______ Your solution, we subtract a certain value form the
/ y-value and then subtract it times t.

What I want:

****** The bullet starts flying on the guns height. Then
***** gravity slowly pulls it back to earth. It looks like
**** half an x² rotated by 90 degrees, but I couldn't find
*** out which variable I have to square :(
**
*


edited because of visual mistakes...

[edited by - Peaceman on October 16, 2002 11:08:09 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think what he means is to subtract 9.81*dt from the y component of the velocity. That will give the curve you''re asking for.

Share this post


Link to post
Share on other sites
Because my english is not very good i will try to explain what you have to do on the example:
used VECTORs Fb,Rb,Rb0,Vb,Vb0;
where Fb is force to the bullet,Fb=Fb(0,-9.81) in our case is it constant vector.
Rb is actual position of bullet,Rb=Rb(x,y)
Vb is actual velocity of bullet,
Vb0 is initial condition(initial vector of velocity,for example
Vb0=Vb0(100,0)) and Rb0 is initial position

used SCALARS mb,dt.
mb is mass of the bullet.
dt is time step

So lets start with simple vector diferencial equation:
mb*dVb/dt=Fb
dVb/dt=Fb/mb
dVb=Fb/mb*dt

(1)dVb is velocity in time T minus velocity in time T-dt,
we can write dVb=Vb(T)-Vb(T-dt)=Fb/mb*dt,
in other words Vb(T) is current velocity and Vb(T-dt)is previous velocity.

It does mean that current velocity you can compute in this way:
(2)Vb(T)=Fb/mb*dt + Vb(T-dt)

But the Vb(T) is dRb/dt,if we put it to the (2) we get
(3)dRb/dt=Fb/mb*dt + Vb(T-dt)
if you look to (1) you will know that Rb(T) is current position and Rb(T-dt) is previous position,in other words...:

Rb(T)-Rb(T-dt)=(Fb/mb*dt + Vb(T-dt))*dt


Rb(T)=(Fb/mb*dt + Vb(T-dt))*dt + Rb(T-dt)


And because it was the vector equations you can write result as this:
(4)
Rb.x=(Fb.x/mb*dt + Vb_prev.x)*dt + Rb_prev.x
Rb.y=(Fb.y/mb*dt + Vb_prev.y)*dt + Rb_prev.y

Using in the program:


//initial conditions:
Rb_prev.x=Rb0.x;
Rb_prev.y=Rb0.y;

Vb_prev.x=Vb0.x;
Vb_prev.y=Vb0.y;
//size of time step
dt=0.1;

//loop for determinig Rb:
while()
{
.
.
//velocities first:
Vb.x=Fb/mb*dt + Vb_prev.x
Vb.y=Fb/mb*dt + Vb_prev.y


Rb.x=(Fb.x/mb*dt + Vb_prev.x)*dt + Rb_prev.x
Rb.y=(Fb.y/mb*dt + Vb_prev.y)*dt + Rb_prev.y
//In Rb you have the actual position now

DrawBulletInAction();

//store for next time:
Rb_prev.x=Rb.x;
Rb_prev.y=Rb.y;

Vb_prev.x=Vb.x;
Vb_prev.y=Vb.y;

.
if(m_antigravity_on)Fb.y=0;
else Fb.y=-9.81;

.
}
If you wanted to display bullet on 2D grid and your vectors are 3D then the procedure will be the same,but you must map 3D to 2D.If you use the following system x to the front(from the monitor) y to the right and z upwards,then do this:
VECTOR2D v2;//we used this
VECTOR3D v3;
from 3D to 2D:
v2.y=v3.z; //it is the same
v2.x=sqrt(v3.x^2+v3.y^2);
alfa=arctg(x/y);
and back from 2D to 3D(in DrawBulletInAction()):
v3.z=v2.y;
v3.x=v2.x*sin(alfa);
v3.y=v2.x*cos(alfa);

I think that is all..Enjoy!

Share this post


Link to post
Share on other sites
Use the simple equation:

y = y0 + vy0 -0.5g*t²

To find the vertical position of the bullet, and

x = x0 + vx0

For the horizontal distance.

Cédric

Share this post


Link to post
Share on other sites
Only two... Out of three, that''s not so bad, right?

Ah, it''s autumn break over here, and I have to know all the wiggly details about the Forced Harmonic Oscillator (overdamped, underdamped, and critically-damped), so...

Cédric

Share this post


Link to post
Share on other sites
thx for all your replies. So the next time you get fragged playing my FPS you know that the bullet was flying realistically and in real life you''d be dead...

PS. In case sb comes here and starts talking about air resistance, for him my game will play in space...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
But in space you have to take into account how the gravitation field varies in different locations. The uniform gravitation model used above is then probably not what you want.

Share this post


Link to post
Share on other sites
Ooohhh! Oh! And don't forget about relativistic effects

[edited by - cedricl on October 17, 2002 11:07:42 AM]

Share this post


Link to post
Share on other sites
Also in reality the bullet hops up a bit as it comes out of the gun, how much this is really depends on the gun itself. But then this is easily modelled by adding a small vertical motion to the bullet when it is fired.

Share this post


Link to post
Share on other sites
Shouldn't be much a problem with varying gravitational forces. You simply have to keep increasing the degree of the position equation until you have an accurate flight path, or one that matches your varying gravitational fields blah blah blah insert buzzword here

[edited by - Zipster on October 17, 2002 12:31:39 PM]

Share this post


Link to post
Share on other sites
Don`t forget the rotation of the bullet caused by the riflings !
That adds a stabilizing effect.

Add to that the type of powder used(changes velocity slightly)


Then don`t forget about bullet deformation on impact- you simply have to have that correct- which requires knowing the bullet material. Does it pop a hole through you, or does it gut you like a dull knife pushed in your stomach and jerked down ?:D

hahah

Just do a reasonably accurate approximation.

Bugle4d

Share this post


Link to post
Share on other sites
quote:
Original post by Vlion
Don`t forget the rotation of the bullet caused by the riflings !
That adds a stabilizing effect.

Isn''t this caused by air friction? We''re in space, so it''s not valid!

The Doppler effect is still there, though. So if the bullet is flying away from you, it should look more red than normal

Cédric

Share this post


Link to post
Share on other sites
quote:
Original post by cedricl
Isn''t this caused by air friction? We''re in space, so it''s not valid!

The Doppler effect is still there, though. So if the bullet is flying away from you, it should look more red than normal

Cédric


This gives me a bumper sticker idea. "If this looks blue you''re driving too fast!"

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
But in space you have to take into account how the gravitation field varies in different locations. The uniform gravitation model used above is then probably not what you want.



If you''re doing it in space, then "to take into account how the gravitation field varies in different locations", you will need to use the universal law of gravitation.

Fg = G*(m1*m2) / (r*r);

where:

Fg is the force of gravity acting on the object
G is the Universal Gravitational Constant (6.67 * 10^-11 Nm^2/kg^2) -> Newton Metres Squared per Kg Squared
m1 & m2 are the masses of the 2 objects (ie bullet and meteor)
r is the separation (distance) between the objects


Hope this helps.

Share this post


Link to post
Share on other sites
OK guys, enough...
I didn''t want to model those bullets that exact...
Thx to all of your replies, I particularly like the bumper sticker idea...

Share this post


Link to post
Share on other sites