Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I been implementing (in XNA) the examples in this physics presentation by Richard Lord where he discusses various integration techniques. Bearing in mind that I am a newcomer to game physics (and physics in general) I have some questions.

15 slides in he shows ActionScript code for a gravity example and an animation showing a bouncing ball. The ball bounces higher and higher until it is out of control. I implemented the same in C# XNA but my ball appeared to be bouncing at a constant height. The same applies to the next example where the ball bounces lower and lower.

After some experimentation I found that if I switched to a fixed timestep and then on the first iteration of Update() I set the time variable to be equal to elapsed milliseconds (16.6667) I would see the same behaviour. Doing this essentially set the framerate, velocity and acceleration to zero for the first update and introduced errors(?) into the algorithm causing the ball's velocity to increase (or decrease) over time. I think!

My question is, does this make the integration method used poor? Or is it demonstrating that it is poor when used with variable timestep because you can't pass in a valid value for the first lot of calculations? (because you cannot know the framerate in advance).

I will continue my research into physics but can anyone suggest a good method to get my feet wet? I would like to experiment with variable timestep, acceleration that changes over time and probably friction. Would the Time Corrected Verlet be OK for this?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

A higher timestep introduces more calculation errors, it also depends on the used integration method.

Use constant time verlet integration, not the variable time verlet integration because it doesn't have the advantages of the verlet integration technique. The advantage of the constant time verlet integration technique is that the enery of the system can only decrease, not increase, which is a very good property of a physics simulation.

Here is a comparisation of some techniques:

Euler integration

pro

  • simple
  • fast

contra

  • energy addition/loss easily possible

Variable time verlet integration

pro

  • better energy conservation property than the euler method

contra

  • introduces calculation errors
  • a bit slower to calculate than euler

Constant time verlet integration

pro

  • ball jumps everytime in the same height
  • no energy addition possible (but loss)

contra

  • a bit more compuation requirement than theeuler integration

how to write code for it

just let the time actor of the variable time verlet integration at the same value.


Why is energy loss better than the addition of energy over time?

Because the objects in the gameworld will get eventually to an halt. This is good because the object can be put to sleep and it doesn't need any more calculations for the collision if no object is near it.

It is also better because your gameworld can't 'explode' because everything collides with everything.


Pseudocode for the constant time verlet integration with drag

Friction for an object can be simulated with the usage of the Acceleration field of an object

PhysicsObject has
   Vector2 OldPosition
   Vector2 CurrentPosition

   // difference between the two is the speed

   float Dragfactor // 0.0f ... 1.0f

   Vector2 Acceleration

// Timestep is the time in seconds of a timestep, can be/is a constant
physics.timestep()
{
   foreach PhysicsObject as IterationPhysicsObject
   {
      Speed = (IterationPhysicsObject.CurrentPosition - IterationPhysicsObject.OldPosition)

      Speed.scale(IterationPhysicsObject.Dragfactor);

      IterationPhysicsObject.OldPosition = IterationPhysicsObject.CurrentPosition;
      IterationPhysicsObject.CurrentPosition = IterationPhysicsObject.CurrentPosition + Speed + IterationPhysicsObject.Acceleration * Timestep * Timestep;

      // reset acceleration
      IterationPhysicsObject.Acceleration = new Vector2(0.0f, 0.0f);
   }
}
share|improve this answer
    
Many thanks. I'll accept this answer as the pros and cons a particularly useful to me. –  Steve Jun 26 '13 at 17:42
add comment

As mentioned, Euler integration is simple and fast, but not very accurate. It is fairly easy for a stiff system (one requiring large forces to satisfy constraints) to explode in a small number of frames.

Choosing an integration method is highly dependent on what it's for. If you are making a simple particle system, there is no point in having variable-timestep, 4th-order Runge Kutta integration.

Euler is fine but you NEED some damping.

To simulate air resistance, just subtract a small portion of the velocity every frame.

To simulate energy loss due to collision, reduce the component of the velocity in the direction of the normal. For a collison with a horizontal floor, this is just the y component.

float GRAVITY = -0.1;
float AIR_RESISTANCE = 0.001;
float RESTITUTION = 0.8;

void update() {
    _vel.y += GRAVITY; // downward force
    _vel -= _vel*AIR_RESISTANCE; // slowing force
    _pos += _vel; // Euler timestep (symplectic)

    if( _pos.y<0.0 ) { // collison with floor
        _pos.y = 0.0; // resolve penetration
        _vel.y *= -RESTITUTION; // bounce up but lose energy
    }
}

This is as simple as I can make it. Hope it helps.

share|improve this answer
    
Thanks for the help. I wondered what Restitution meant when I've seen it mentioned. Is the term used specifically for the energy lost when something hits something else? Also, what sort of value is it usually stored as? A percentage or a fixed value? –  Steve Jun 26 '13 at 15:53
1  
Simply put, the coefficient of restitution is the ratio of speed of approach before collision to the speed of separation after it. A value of 1.0 is perfectly elastic, meaning no energy was lost at all and the objects bounce off each other. A value of 0.0 is perfectly plastic so all energy was lost and the objects stick together like lumps of clay. –  DaleyPaley Jun 27 '13 at 7:32
    
That makes perfect sense, thank you. –  Steve Jun 27 '13 at 15:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.