Jump to content
  • Advertisement


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


physics of a flying bulllet

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

If you intended to correct an error in the post then please contact us.

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:

(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:
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:

//size of time step

//loop for determinig Rb:
//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


//store for next time:


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
from 3D to 2D:
v2.y=v3.z; //it is the same
and back from 2D to 3D(in DrawBulletInAction()):

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.


Share this post

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

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...


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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!