2D Physic Engine Part 1

What i have done so far?

I read through books and bachelor/master thesises till i felt comfortable with everything. I even implemented a 3D impulsed based Engine. The 3D engine is based on Game Physics Engine Development by Ian Millington. The engine has problems if you want to extend the engine with joints and resting contacts (you don’t have to understand that it will be explained in the next parts).

The engine i build is based on Farseer Physics…

I can’t find a developer friendly physic engine theory explanation. So i read through alot math thingys till i got a basic understanding. Now i read physic engine codes and reverse them to build “my own” engine. This process is documented here. Once in a week or even more frequently if a special topic needs distinct explanation.

We build the basics and extend the engine till we got a full rigid body engine.

In the game loop before the rendering we do a world simulation with the given elapsed time. The world simulation has the following structure (based on Farseer):

  1. Management of Body Adding and Removing – If bodies are added while we process them, the array get inconsistent. (Multithreading for example)
  2. Collision Detection – Check which bodies collides and creates contacts
  3. Integrate Gravity and Damping – Adds the Gravity and Damping on Linear and Angular Velocities
  4. Convert Contacts to Constraints – Constraints are used to solve penetration and calculate Velocity changes. These are the ones for Contacts
  5. Convert Joints to Constraints* – I will add joints at the end of the 2D Physic Engine. The Constraints structure makes this easy extendable
  6. Solve Constraints – Does the magic we call physic 😉
  7. Integrate new Position/Rotation – Finish?!? Nearly we just need Error correction
  8. Correct penetration Errors – Farseer does this after the new position is applied. Most basic engines does this before the new Position/Rotation is computed. It is basicly the same
  9. Sleep State for Performance* – This will be added later, because it is “just” an performance improvement and we want to understand how it works first till we improve such things

* Things will be added later, because it’s just performance boost or features which will prevent the basic understanding.

What shapes do we support?

I try to seperate the Collision and Physic Engine so you could add your shapes. The first shape i use are Boxes. I don’t want spheres because:

  1. You don’t see Rotation
  2. We need a Sphere – Box collision detection to detect screen edges

What’s next?

The first 3 points of the Physic Engine Loop: Cheap Rendering, Management of Body Adding and Removing, Integrating Gravity and Damping, Integrate new Position/Rotation and Collision Detection

Cheap Rendering

We just draw boxes with orientation and position… Easy

Management of Body Adding and Removing

Short code to make it thread safer

Integrating Gravity and Damping

Very short code just basic formula

Integrate new Position/Rotation

Just basic formula

Collision Detection

This will be the main topic. Since we are in 2D Space not that hard ^^

Leave a Reply

Your email address will not be published. Required fields are marked *