flying object physics

Started by
2 comments, last by Samith 16 years, 11 months ago
i am working on a game for school. It is like tanks where you shoot at a target across the screen and blow the target up. The only problem is that the physics equations that i have tried dont work right. i wanted to see if anybody had equations that could be used to position the ball every step of the way across the screen. i have used some from my physics textbook but the ball moves like an inch and stops. it doesn't do anything after that.
Advertisement
I'm afraid you'll need to be more specific. What "equation" are you using now, and how exactly are you implementing it in code?
Assuming no air resistance, the problem is that of Newtonian motion with constant gravity. If the projectile is fired from (x0, y0) with velocity v0 at an angle θ from the horizontal, then the position of the particle at a later time t is given by:

x(t) = x0 + v0cos(θ)t
y(t) = y0 + v0sin(θ)t - gt2/2

In terms of a game-loop, this corresponds to performing the following operations once upon firing:

vx = v0cos(θ)
vy = v0sin(θ)

and these once per frame:

d := amount of time since previous frame
x = x + vx*d
vy = vy - g*d
y = y + vy*d - g*d*d/2

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
If I had to guess what was happening I'd say you aren't taking into consideration the fact that the time variable is continuously changing each frame. The equations in your physics book typically solve for a particle's position at a certain, known, time. In a game or simulation, typically only the time since the last frame is kept. So, you will start with a certain initial x and y position, an initial velocity, and an initial angle. Then, each frame you'll find the particle's new position using the delta time (time since the last frame), the particle's current position, and it's current velocity (which should be changing due to acceleration from gravity). TheAdmiral's post covers most of what you need to do.

This topic is closed to new replies.

Advertisement