![](sad.gif)
physics of a flying bulllet
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.
![](sad.gif)
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!
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!
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:
edited because of visual mistakes...
[edited by - Peaceman on October 16, 2002 11:08:09 AM]
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
![](wink.gif)
/ /____/______ 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]
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.
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!
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!
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
y = y0 + vy0 -0.5g*t²
To find the vertical position of the bullet, and
x = x0 + vx0
For the horizontal distance.
Cédric
I believe you are missing t''s in several places. The equations should be:
y = -0.5*g*t2 + vy0*t + y0
x = vx0*t + x0
y = -0.5*g*t2 + vy0*t + y0
x = vx0*t + x0
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
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
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... ![](wink.gif)
PS. In case sb comes here and starts talking about air resistance, for him my game will play in space...
![](wink.gif)
PS. In case sb comes here and starts talking about air resistance, for him my game will play in space...
![](wink.gif)
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement