gravity/physics tutorial

I’m looking for a gravity/physics tutorial, i’m trying to just do some basic ballistics simulations.

In particular I am interested in:

  1. applying -9.8Z gravity to actors in a scene
  2. applying other forces to actors in that scene
    2a. instantaneously
    2b. impulse over a set time
  3. calculating Z axis velocity
  4. calculating z-axis distance from actor to terrain


First of all, what do you have in place at the moment? Do you have velocities and perhaps accelerations for your objects? A method that allows you to apply forces? Perhaps some “force state” that records all of the forces currently acting on your objects, and their durations?

Are you using fixed-step simulation, or delta-t?

As to specific situations, the simplest should be the application of gravity.

v = v + at (velocity = initial_velocity + acceleration x delta_time)
For gravity, acceleration should be your -9.8 in the z. From this you should be able to calculate your velocity, and from that the distance and direction in which to move your object for a given update.

Applying a force instantaneously shouldn’t be too difficult, I don’t think - f(orce) = m(ass) x a(cceleration), therefore a = f/m. So, for an object of mass m, if we apply an instantaneous force f its velocity should increase by f/m, I think.

(It’s been a while since I took Physics, however, so I may be off on that with regards to instantaneous force application. ^^; )

For forces over time, I think that you would probably just use f = ma along with the equations of motion:
v = v0 + at
pos = pos0 + v0 + 0.5at²
pos = pos0 + ((v0 + v)/2)t
v² = v0² + 2a(pos - pos0)

Where v is velocity, pos is position, a is acceleration, t is time (probably time since last step, in your case), and the values with zero appended are initial values (at the start of your step, presumably).

Simply replace the x values above with vector versions (in other words, pos instead of x).

These of course assume constant acceleration, but for sufficiently small time-steps they should work, I think.

You might be well served by a search or two of this forum.

Finally, your fourth problem can, I believe, be solved by firing a ray down from your character and detecting the first collision with whatever you consider to be “terrain”.

(Take a look at the collision detection section of the manual, if you haven’t already. I would suggest a ray collision solid, parented to the object being tested, with a bitmask being set as the ray’s “from” bitmask, and the same value being set in the relevant terrain objects as the “to” bitmask. Use a CollisionHandlerQueue, and call the sortEntries function to get the entries in order from nearest to furthest collision - the one that you want should be the first.

(I think that this was described in the manual somewhere, although I may be wrong.))

Of course, if you have a simple horizontal plane as your terrain, then you can simply fetch its z-value and use that. :wink:

Mmmkay, so I used some of the jetpack stuff to get underway… here is my new hitch:

I load up an actor JP and apply physics to him, and give him a mass.
I apply gravity to the world. Cool.

I set JP.setPos(25,35,10)
I set JP.setR(45)
I set JP.setH(45)
So now he’s pointed off at 45 degrees up and right

Now I apply a force to him and he flies off at 45/45. Life is good.
I even setup dt to remove the force after 0.2
So now JP flies up a ways, and eventually gravity decelerates him to zero and starts pushing him back down.

I’m interested now in figuring out at exactly what point JP’s vertical velocity is zero (or reasonably close to zero)

Here’s my main problem at this juncture…

I use a print statement to report JP’s position
this print statement reports that at every instant
self.JP.getX() == == 25
self.JP.getY() == == 35
self.JP.getZ() == ==10
*these are JP’s starting position from my last setPos()

So apparently, JP’s position is not being updated as the physics engine pushes him around.

If I could getZ() I could do the math to figure out his vertical velocity.
getZ() - prevz / elapsed

Is there something i’m doing wrong in trying to query JP position?
Is there some function that already does what I need done?

I’m still sifting docs…


*EDIT: ah i guess the thing to do is collision ray with a plane at the ground, yes? that would give me distance from the collision plane, correct?

getPos, getX, getY and getZ return relative positions. You need to do getPos(render), getX(render), getY(render) and getZ(render) to retrieve the absolute positions of your actor.

Using a collision ray would be overkill.