Click or drag to resize
DigitalRuneForces, Force Effects and Force Fields

This section explains how to use forces to influence objects in the simulation.

Forces and rigid bodies

Forces can be applied to rigid bodies. Forces will cause an acceleration of rigid bodies (Remember: Force = Mass ∙ Acceleration). Forces only influence dynamic bodies. Static and kinematic bodies are not influenced by forces.

To apply a force directly to a rigid body the class RigidBody has several force-related methods: AddForce, AddTorque, ClearForces, etc.

All forces/torques set with the AddForce/AddTorque methods are accumulated and act during the next time step of the simulation. At the end of the time step all forces set with AddForce/AddTorque are automatically cleared. - If a constant force should be applied for a longer period, the force must be re-added with AddForce/AddTorque after each simulation update.

Forces effects

Force effects are classes derived from ForceEffect that affect multiple rigid bodies. Force effects can be added to the Simulation:

C#
Simulation simulation = new Simulation();
...

// Add a gravity force field and a damping effect.
simulation.ForceEffects.Add(new Gravity());
simulation.ForceEffects.Add(new Damping());
...

The simulation will automatically call all force effect instances in a time step to apply forces to the rigid bodies. Examples of force effects are: gravity, damping, explosions, tornadoes, etc. The force effects that come with DigitalRune Physics can be found in DigitalRune.Physics.ForceEffects.

The force effects Gravity and Damping are two important force effects that are used in almost all games. Gravity pulls objects to the ground. Without gravity all objects are weightless and float around as in outer space. Damping models a simply form of air resistance and makes sure that moving bodies will slow down. As in the example code above, these two effects are needed in all games (unless it is a space game or a game with specialized gravity forces).

Force effects in SimulationForceEffects are applied in each time step until they are removed from the collection. Some force effects, such as Explosion, automatically remove themselves after a certain duration. (See the descriptions of the force effects.)

Force fields

ForceFields are specialized force effects that act on all objects in an area of effect (see AreaOfEffect). Gravity and Damping are such force fields.

The area of effect can be defined using any object that implements the interface IAreaOfEffect. The standard implementations are:

  • GlobalAreaOfEffect: The force field applies forces to all bodies in the simulation.
  • ListAreaOfEffect: The force field applies forces to all bodies in a given list.
  • GeometricAreaOfEffect: The force field applies forces to all bodies that collide with a given collision object.
Creating custom forces effects and force fields

It is very easy to create new force effects. Please, have a look at the Samples and read the class documentation of ForceEffect and ForceField.

Here, is how the gravity effect could be implemented:

C#
public class Gravity : ForceField
{
  public override void Apply(RigidBody body)
  {
    AddForce(body, new Vector3F(0, -9.81f, 0) * body.MassFrame.Mass);
  }
}

When implementing a custom force effect it is important to note that force effect classes use the AddForce/AddTorque methods of the base class ForceEffect. They must not use the AddForce/AddTorque methods of the RigidBody directly.

See Also