Overview of Physics engines

From Devel

Jump to: navigation, search

Contents

Overview of Physics engines

Introduction

A physics engine is a set of functions to let you describe the layout of the system of physics objects and then tells you how the state of this system changes over time. For example, when you define layout using shapes, masses, positions, dimensions, global gravity vector, etc. (Refer Figure 1), then Physics engine will tell you how your system changes over time (Refer Figure 2). Figure 1: Define the Layout Figure 2: system will predict the change A physics engine are commonly used to provide an approximate simulation of certain simple physical systems, such as rigid body dynamics (including collision detection), soft body dynamics, and fluid dynamics, of use in the domains of computer graphics, video games and film.

Types of Physics engine

There are generally two classes of physics engines:

  • Real-time
  • High - precision.

High-precision physics engines require more processing power to calculate very precise physics and are usually used by scientists and computer animated movies. In video games, or other forms of interactive computing, the physics engine simplifies its calculations and lowers its accuracy so that they can be performed in time for the game to respond at an appropriate rate for gameplay. This is referred to as real-time physics.

Another classification of physics engines can be done in terms of whether or not it involves a user interaction. These two categories are:

  • Interactive : They accept user input while performing dynamic simulations
  • Non Interactive: They work as standalone with no active user participation.

Architecture of a physics engine

Physics engines for video games typically have two core components as shown in Figure 3.

  • A collision detection/collision response system
  • The dynamics simulation component responsible for solving the forces affecting the simulated objects.
  • Modern physics engines may also contain fluid simulations, animation control systems and asset integration tools.

Figure 3:Core components of a physics engine

Fluid simulation methods use numerical methods and algorithms to solve and analyze problems that involve fluid flows. Animation control system is responsible for controlling the position and/or velocity of machines using some type of device such as a hydraulic pump, linear actuator, or an electric motor, generally a servo, while, the asset integration is commonly done with COLLADA. It defines an open standard XML schema for exchanging digital assets among various graphics software applications that might otherwise store their assets in incompatible file formats. Core components

Physic engines work with following fundamental concepts as shown in Figure 4. Let us briefly explain each of them. Figure 4: Core concepts

shape

A Shape is a 2D geometrical object, such as a circle or polygon.

rigid body

It is a chunk of matter that is so strong that the distance between any two bits of matter on the chunk is completely constant. They are hard like a diamond. In the following discussion we use body interchangeably with rigid body.

fixture

A fixture binds a shape to a body and adds material properties such as density, friction, and restitution.

constraint

A constraint is a physical connection that removes degrees of freedom from bodies. In 2D a body has 3 degrees of freedom (two translation coordinates and one rotation coordinate). If we take a body and pin it to the wall (like a pendulum) we have constrained the body to the wall. At this point the body can only rotate about the pin, so the constraint has removed 2 degrees of freedom.

contact constraint

A special constraint designed to prevent penetration of rigid bodies and to simulate friction and restitution. You do not create contact constraints; they are created automatically by Box2D physics engine.

joint

This is a constraint used to hold two or more bodies together. Engines like Box2D support several joint types: re volute, prismatic, distance, and more. Some joints may have limits and motors.

joint limit

A joint limit restricts the range of motion of a joint. For example, the human elbow only allows a certain range of angles.

joint motor

A joint motor drives the motion of the connected bodies according to the joint's degrees of freedom. For example, you can use a motor to drive the rotation of an elbow.

world

A physics world is a collection of bodies, fixtures, and constraints that interact together. Engines like Box2D support the creation of multiple worlds, but this is usually not necessary or desirable.

Implementation Strategies

There are three major paradigms for the physical simulation of solids:

  • Penalty methods, where interactions are commonly modeled as mass-spring systems. This type of engine is popular for deformable or soft-body physics.
  • Constraint based methods, where constraint equations are solved that estimate physical laws.
  • Impulse based methods, where impulses are applied to object interactions.

Figure 5: Implementation Strategies

Finally, hybrid methods are possible that combine aspects of the above paradigms.

Tips and tricks

  • Keep things Simple (Less objects less animation, more impact).
  • Approximate complex volumes with simple volume (performance gain).
  • Do not simulate static or stationary objects.
  • Use spatial partitioning.
  • Less contact points to avoid collisions.
  • Prefer 2D over 3D.

Examples of Physics engines

Physic engines are available in the market for both 2D simulations and 3D simulations. The most popular ones are listed in table 1.

2D engines 3D engines box2D Bullet ChipMunk ODE Table 1: Popular physics engines

Important References

General programming:

  • "Physics for the game developers" written by David Bourg, published by O'Reilly Media, Inc.
  • "Physics for the game programmers" written by Grant Palmer, published by Apress.

Theory:

  • "Physics Based Animation" written by Kenny Erleben/Sporring, published by Charlers River Media.
  • "Real-time collision detection" by Christer Ericson, published by Morgan Kaufmann.
  • "Collision Detection in 3D environments", by Gino van den Bergen, published by Elsevier.
  • On-line MsC/PhD thesis: Helmut Garstenauer, A Unified Framework for Rigid Body Dynamics
  • Kenny Erleben, "Stable, Robust, and Versatile Multibody Dynamics Animation",
  • Lacoursière, Claude (2007). Regularized variational methods for interactive simulations of multibodies with dry frictional contacts.
  • Bart Barenbrug,"Designing a class library for interactive simulation and rigid body dynamics",
  • Gino van den Bergen PhD, Collision Detection in interactive 3D Computer Animation
  • Brian Mirtich, Impulse-based Dynamic Simulation of Rigid Body Systems Other on-line resources: CATCH, Continuous Collision Detection, Zhang, http://graphics.ewha.ac.kr/CATCH/
  • Game Physics Performance on the Larrabee Architecture, Intel,
  • GPhysics, Erin Catto, http://gphysics.com
  • Bullet Physics Wiki, http://www.continuousphysics.com/mediawiki-1.5.8
  • Continuous Collision Detection, Erwin Coumans, Books with in-depth *information about LCP and Featherstone constraints: "The Linear Complementarity Problem", Cottle, Pang Stone.

Case Study: Box2D

Introduction

Box2D is a 2D rigid body simulation library for games. Programmers can use it in their games to make objects move in believable ways and make the game world more interactive. From the game's point of view a physics engine is just a system for procedural animation. It is developed in C++, but custom Libraries are also available for Javascript and Flash. It has been used in Crayon Physics Deluxe, Rolando, Fantastic Contraption, Incredibots and many online Flash games

Architecture

Box2D is composed of three modules: Common, Collision, and Dynamics. The Common module has code for allocation, math, and settings. The Collision module defines shapes, a broad-phase, and collision functions/queries. Finally the Dynamics module provides the simulation world, bodies, fixtures, and joints.

Figure 6: Box2D Architecture

Important guidelines

  • Box2D works with floating point numbers, it works well with meters-kilogram-second (MKS) units.
  • Do not be tempted to use pixels as a scale. Box2D is tuned for MKS units. Keep the size of moving objects roughly between 0.1 and 10 meters.
  • Box2D uses radians for angles.
  • Memory management plays a central role in the design of the Box2D API., always use safe factory functions.

Tips for Converting from pixels to meters

Box2D is tuned for meters-kilograms-seconds (MKS). Your moving objects should be between 0.1 - 10 meters. Do not use pixels as units! You will get a jittery simulation. Use the following example to covert from pixels to meters. Suppose you have a sprite for a character that is 100x100 pixels. You decide to use a scaling factor that is 0.01. This will make the character physics box 1m x 1m. So go make a physics box that is 1x1. Now suppose the character starts out at pixel coordinate (345,679). So position the physics box at (3.45,6.79). Now simulate the physics world. Suppose the character physics box moves to (2.31,4.98), so move your character sprite to pixel coordinates (231,498). Now the only tricky part is choosing a scaling factor. This really depends on your game. You should try to get your moving objects in the range 0.1 - 10 meters, with 1 meter being the sweet spot.

References

Emanuele Feronato has created a series of articles on Box2D. Todd Kerpelman hosts a number of Box2D tutorials in video format.

Summary

  • Physics engine:

Physics engine is a set of functions to let you describe the layout of the system of physics objects and then tells you how the state of this system changes over time.

  • Used For
    • Simulation of certain simple physical systems, such as rigid body dynamics (including collision detection), soft body dynamics, and fluid dynamics, of use in the domains of computer graphics, video games and film.
  • Types
    • 1) Real time Vs High precision (Speed Vs Accuracy)
    • 2) Interactive Vs Non Interactive (User Input or No Input)
  • Core Components
    • Collision detection system,
    • Dynamic simulation system
  • Additional components
    • Fluid simulations
    • motion control system
    • asset integration
  • Important concepts
    • Shape, Rigid body, Fixture, Constraint, Contact constraint,
    • Joint, Joint limit, Joint motor, World
  • Example
    • 2D: box2D, Chipmunk
    • 3D: ODE, Bullet
  • Important tips
    • Keep things Simple (Less objects less animation, more impact).
    • Approximate complex volumes with simple volume (performance gain).
    • Do not simulate static or stationary objects.
    • Less contact points to avoid collisions.
    • Prefer 2D over 3D.
Personal tools