Jump to content
  • Advertisement
Sign in to follow this  
gp343

Trajectory / Projectile problem

This topic is 4868 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 guys, I've been trying to get to grips with simple projectile motion theory. I've written the below code in C# for a simple projectile. The problem is that the y-component is only increasing, it never goes down. Can somebody help me out? Thanks James. public void fire() { const double G = -9.81; double xvel = 15.0; double yvel = 20.0; double dt = 0.05; int i = 0; double time = 0.0; double x = 0.0; double y = 0.0; while(i < 83) { x = x + xvel * dt; y = y + yvel * dt + 0.5 * G * dt * dt; time=time+dt; Debug.WriteLine("time = " +time +" , x="+x + " , y = "+ y); i++; }//End of while loop }//End of Fire Method

Share this post


Link to post
Share on other sites
Advertisement
Hi guys, I have solved the problem. I didn't update the y velocity as that changes with the time: yvel = yvel+ G *dt;

so now my code is like the below code and it work perfectly.

y = y + yvel * dt + 0.5 * G * dt * dt;
yvel = yvel+ G *dt;//Update y- velocity with gravity and change in time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!