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

## 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 on other sites
You're never updating yvel. Your best bet is to update yvel, then update y

yvel += 0.5*G*dt*dt;y += yvel*dt;

##### Share on other sites
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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 23
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
634782
• Total Posts
3019266
×