Praelium Physics Engine

From KruelWiki

Jump to: navigation, search

phys notes from KruelKonf '05 available, for any who can decrypt them.

The praelium physics engine needs to be highly flexible and handle large numbers of different types of objects. This page will document the methods and development of the physics engine as the game progresses. The system presented here is a goal for the first public build of Praelium.

Contents

Collisions

We only need a simple system to check for collisions in the first demo. A good first approximation is spheres. It is very easy to tell when two spheres collide; just check the distance between them, and if it is smaller than the sum of their radii, then they have collided. Mathematically, this is:

   ||(p1 - p2)|| < r1 + r2

Where p1 and p2 are the object positions (vectors), and r1 and r2 are their radii.

  • This system has many shortcomings, and cannot handle a wide variety of object shapes. In the future it will need to be replaced by something more customisable, but hopefully of similar speed.

Reactions

Solids

When two objects collide, they need to bounce off each other (or blow up, but that's an issue for higher in the program). When a pair of asteroids hit, aside from a little dust, it is probable that they will act similarly to billiard balls (this is an elastic collision). In such a collision, both momentum and kinetic energy must be conserved. This gives us the solutions:

 v1' = v1*(m1 - m2)/(m1 + m2) + v2*(2*m1)/(m1 + m2)
 v2' = v1*(2*m2)/(m1 + m2) + v2*(m2 - m1)/(m1 + m2)

These are generalised formulae, and can be applied to any elastic collision along 1 dimension (ie head-on). Since spheres always strike each other tangentially, there is always a one dimensional vector from one sphere center to the other which represents the dimension of a head on collision. Here is a visual illustration of how the decomposition for a collision will work:

Image:Billiards.jpg

Springs

The elastic approach works well for solid objects, and can be easily extended to deformible objects (such as ships). What it does not allow for, are objects which have a soft repulsive force. These include planets with atmospheres, and ships with shields. when an object comes near one of these, it should be repelled over time; accelerated in the opposite direction. ie if the distance separating two objects is d, and d is smaller than the sum of their radii r1 + r2, then force is proportional to r1 + r2 - d. This means that as the objects get closer, the force becomes greater. Here is an illustration:

Image:Springs.jpg

Runtime

The solution for this will be finished later. For now I will just outline the problem. The only way to know if two objects are colliding is to check the collision criterion above. If we have n objects, the first object will have to check against n-1 other objects. the second object will check against n-2 others, since it has already been checked against the first. This results in a quadratic runtime which, while fairly efficient, does not scale to large numbers of objects (thousands will likely be needed) very well.

Personal tools