Skip to main content
gamedev.net
🔒 Locked

Position after a certain time

Started by Lode Jan 10, 2005 at 6:47 PM 4 replies 750+ views
Original Post
Lode
Lode
Given: a particle that has an initial position and initial velocity, and a constant force (==> accelaration) Wanted: position of the particle after a certain time has passed. What do I need this for? NOT for a homework, I have no homeworks (do have exams tho, but they have to do with electronics!) FOR a particle system, I want to "warmup" all the particles when they are created as if the particle system has been running for a while alraedy, but without actually waiting that long. For that I need to calculate where the particles will be after a certain amount of time, the particles move with their velocity, and the force changes their velocity. Here's a screeny of what it looks like after time X, but after waiting that long in reality. At time 0, all particles are at the beginning = annoying.
Oluseyi
Oluseyi
Newtonian Physics.

s = s0 + v * t + ½a * t²
Roof Top Pew Wee
Roof Top Pew Wee
That is, to clear things up:

If your object starts at x = 0, has a xVelocity of xVel, and an acceleration of xAcc, then your position after T seconds (assuming units per second) is:

newX = 0 + T*xVel + 1/2 * a * T^2;

I know this is a repost of what Oluseyi said, but thought I'd explain the variables a little.

--Vic--
Lode
Lode
Indeed, now I remember the formula from some old mechanics classes. I had forgotten about it, such an essential one :D

But it doesn't totally work.

I do the following, every frame, for every particle:

    //update velocity    velX = velX + dts * accX;    velY = velY + dts * accY;    //update position    x = x + dts * velX + 0.5 * dts * dts * accX;    y = y + dts * velY + 0.5 * dts * dts * accY;


x and y are the position of the particle, velX and velY it's velocity, and accX and accY the force given to the particle by the emitter. dts is the time difference since the previous frame, in seconds.

Well I made an emitter that lets particles move around some gravity center, after running it for some time it looks like this:



Now, I pause the program for a second or so (by dragging the SDL window, because SDL then pauses the application in windows). I'd expect after this second, since the particles use the code I gave above, the particles would still be close to the center. dts will be something like 1 second then. However, they now do this:



For some reason they suddenly got much bigger velocities and manage to reach much farther.

Is there something incorrect about my code, like, the way to update velocity, or the order in which things happens?

EDIT:

Ok, I think I found why it happens: the accelaration isn't constant in time, it changes direction when the particles move around the gravity center.

How can I precalculate THAT? Only the direction of the accelaration changes, it's magnitude is constant.
Oluseyi
Oluseyi
For that, you'll need integral calculus. Yes, the magnitude of the acceleration is constant, but acceleration is a vector quantity (time to model it as one) and since the direction changes over an interval t...

Provided you can describe the change in a as a function of t, then you can do a simple integral:
integrate [s<sub>0</sub> + v * t + ½a * t² * da/dt][0, T]
where [0, T] is the interval you are integrating over. If you can't describe a as a function of t, then you're just going to have to run the simulation, though you can speed it up by not actually waiting for a full second, etc (just run it flat out, as fast as possible, disconnected from the game world timer).

JohnBolton
JohnBolton
The problem may be your velocity calculation. Oluseyi's equation needs a very small clarification. It should be

s = s0 + v0 * t + ½a * t²

It appears that in your code you update the velocity and then use the current velocity to get the position. You must use the original velocity (i.e. s0 and v0 are the position and velocity at t = 0). This is a VERY common mistake. The change to your code is simple: just calculate the position before updating the velocity.
    //update position    x = x + dts * velX + 0.5 * dts * dts * accX;    y = y + dts * velY + 0.5 * dts * dts * accY;   //update velocity    velX = velX + dts * accX;    velY = velY + dts * accY; 


John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.