Win an Ipad
  Developer   Tutorials   Breakout   Physical engine
Lesson 0

Physical engine

The most important parts of box2d physics engine are world and bodies.

World

The world creates background for all bodies, joints or contacts. The world can have gravity, which can be specified on both axis (x, y). The world has always width which is equivalent of 10 metres in real world. All objects and things in the world are scaled to ensure this width. The scale property says how many pixels are in one meter. The box2d also uses its own coordinates which start in the left bottom corner and are counted in meters.

Image: box2d coordinates

Fortunately, the Moscrif framework usually uses normal pixel coordinates and conversions are made automatically.

Bodies

The bodies represent all real objects / things which interact in the world. There are three types of bodies which behave differently.

  • #static Static body does not move under simulation and behaves as infinite mass. Static bodies can be moved manually by PhysicsBody.setPosition. A static body has zero velocity and does not collide with other static or kinematic bodies.
  • #kinematic Kinematic body moves under simulation according to its velocity. Kinematic bodies do not respond to forces. They can be moved manually by the user, but normally a kinematic body is moved by its velocity which needs to be set. A kinematic body behaves as infinite mass, but it does not collide with other static or kinematic bodies.
  • #dynamic Dynamic body is fully simulated. It can be moved manually by setPosition, but normally they move according to forces. A dynamic body can collide with all body types and always has finite, non-zero mass. If you try to set the mass of a dynamic body to zero, it will automatically acquire a mass of one kilogram.

As you can see not every body collides with every other body. The next table shows which bodies collide together.

body type static dynamic kinematic
static      
dynamic      
kinematic      
  collide together
  do not collide together

The bodies have many properties which affect their physical behaviour. The three properties which directly affect the body behaviour are bounce, density and friction.

  • Density is a property which affects the body mass.
  • Friction is the force resisting the relative motion elements sliding against each other.
  • Bounce property affects the size of bounce. The value 0.0 means that the body will not bounce from other bodies. The value 1.0 means that the body will bounce with the same speed as it fell; however, there is a possibility to increase the bounce rate and the ball will bounce off with more velocity.
 Menu Scene   Physical engine   Game scene 
Tutorial details and chapters
Technology:
Difficulty:
Completion:
Breakout chapters