Post by BlueMagnificent on Aug 18, 2014 22:02:42 GMT 1
INTRODUCTION
Let’s say you want to make a bowling game. You have the ball and the pins arranged. But the problem is you’d have to code all the steps involved in rolling the ball and the ball knocking down the pins. So you set out to code the dynamics involved: how the ball will roll on the floor, its collision with the pins, the force of collision, how the pins will collide against each other and so on. Before you are through you are already regretting why you did pay attention to Physics Mechanics in school.
Well, before you start regretting, some people have probably regretted it too and came up with a solution, Physics Engines. A physics Engine is a software, a group of codes or a collection of objects and algorithms that provides functions for the simulation of a physical world be it in games and other virtual reality application. You don’t have to stress yourself coding the movement of a bowling ball or how it collides with pins when you can have them represented by physics objects and then simulating the whole action in a realistic way.
What do you want to make? Is it a catapult game like AngryBirds or a shooting game or you even want to make your own soccer game, Physics Engine got you back well covered.
There are a handful of Physics Engines available, some are free while others you might pay a token to use and still yet others we don’t even know about mainly because they are in-house tools. Some of the ones we know are ODE, PhysX, Newton and Bullet. We will shed a little bit light on Bullet Physics since it is the implemented Physics Engine in Urho3D.
IT’S A PHYSICAL WORLD
Bullet Physics is an Open Source Physics Engine that provides objects and functions for Collision Detections and Rigid Body Dynamics. It has quite a decent amount of fame having a wide integration into so many Game Engines and 3D modelling packages. It has even been used in some movies, if you doubt me ask Google.
To use a Physics Engine like Bullet there are certain things you’d have to understand.
Physics World
There has to be a world that obeys the laws of physics except you are in a parallel Universe that has its own set of Physical Laws. In Bullet Engine this world is called a Collision World and has among its derivatives the Dynamic World.
The Physics World has options to set gravity and exposes functions and objects for the following:
Collision Detection:
Detects when two objects collide so that you can deduct the health of a monster when your sword slashes through it.
Collision Filtering:
Being able to set which objects should collide and which should not. Like a 1Up appearing and the enemies can pass through without absorbing it, but your character passes and picks it up.
Rigid Body Dynamics:
The force, mass, inertia, velocity and constraints of the World. In a snooker game you take a shot, the cue ball rolls and knocks against ball which gradually rolls before coming to a stop. Or you shot a hanging sign post and it swings around.
Above all these functions is that to run the physics simulation:
To this function you pass in the elapsed time since it was last called, with this the world updates all of its objects (collisions are checked for, dynamics applied to rigid bodies and so on) and performs other necessary functions to achieve a proper physics simulation for that time step.
Rigid Body and Collision Shape
For any form of physics interaction to occur there has to be a body, yes somebody has to be there. In Bullet this body is called a collision object or a rigid body (note: rigid body derives from collision object). A rigid body is what moves, collides, has mass and can have impulse applied to it. But on its own it is formless, more like a ghost without a shell. It needs a shape so as to interact in collisions and to also help in calculating its inertia tensor (distribution of mass). This is achieved by the addition of a Collision Shape
Some of the collision shapes supported by Bullet are primitive shapes (e.g box, sphere, cylinder, capsule, cone and multisphere) , compound shapes, triangle mesh, convex hull and static plane. The explanation for some of these will come when we enter the next section.
Note that when a rigid body has a mass of zero it means the body has infinite mass hence it is static.
The Physics Illusion
It is important to note that in most cases the physics world and its objects are actually not part of your scene or game world. The physics world is in a world of its own on a different realm from your game. What the physics world really does is to model the physical objects of your scene and their possible interaction using its own objects. It is now your duty to update the state (transform) of your object, especially in the main loop, based on the state of its corresponding physics object. An example will make this clear.
Let us assume you have a box and a plane model in your scene, and you want the box to fall unto the plane from a height. You’d have to model your scene in the physics world using objects provided by Bullet:
First set the Physics World's gravity, 9.8 is okay. Add rigid body with a mass of zero and a static plane collision shape. Next add another rigid body with a non-zero mass and a box collision shape to the world. Set its transform to have a position some heights above the plane
After your physics world has been setup, in your application’s Main loop call the world’s stepSimulation() function, obtain the new transform of the box rigid body and update your box model’s transform with it.
That is how it happens: while the Physics World is running, you are busy extracting out information from it to update your scene.
[b
DODGING THE BULLET[/b]
If you’ve ever worked with a rendering engine, game engine, or a 3D visualization application that has no physics and you want to integrate physics into it you’ll find that there are frustrations and a bit of stress involved in it. You’d have to synchronize the both worlds and so many other things in order to make the physics work.
Urho3D tried to save us a lot of these stresses by neatly integrating the Bullet Physics into its structure. In fact, Bullets is completely buried into the framework that you use the physics objects just like every other Urho3D components, with the Physics illusion completely taken care of.
The important Physics components in Urho3D are the PhysicsWorld, RigidBody, CollisionShape and Constraints.
PhysicsWorld
This is what it is, a Physics World, just as has been explained in the previous section. Unlike in raw Bullet where you’d pass in a couple of objects to instantiate the world, in Urho3D you just create it as a regular component unto the scene object.
Lots of things are set by default for you and you can still make your own configurations through the provided functions.
Rigid Body
The rigid body in Urho3D is just as was explained for Bullet but with the exception that it is created as a component unto a node:
The good thing about this is format is you don’t have to manually update the transform of the node for each iteration of the main loop; it is done automatically for you.
Collision Shape
Just as in raw Bullet Engine, a rigid body needs a collision shape. The collision shape component of Urho3D is created unto a node just like every other component:
Constraints
Joints, that is what this is in its simplest explanation, just joints. A constraint component connects two rigid bodies together or connects a rigid body to a static point in the world. The following are Bullet Physics constraints supported in Urho3D (explanation and diagram are gotten from Bullet Physics User Manual) :
Point (or Point to Point)
Point to point constraint limits the translation so that the local pivot points of 2 rigid bodies match in worldspace. A chain of rigid bodies can be connected using this constraint.

Hinge Constraint:
Hinge constraint, or revolute joint restricts two additional angular degrees of freedom, so the body can only rotate around one axis, the hinge axis. This can be useful to represent doors or wheels rotating around one axis. The user can specify limits and motor for the hinge.

Slider Constriant:
The slider constraint allows the body to rotate around one axis and translate along this axis.

Cone Twist Constraint:
To create ragdolls, the cone twist constraint is very useful for limbs like the upper arm. It is a special point to point constraint that adds cone and twist axis limits. The x-axis serves as twist axis.
In Urho3D
COLLISION FILTERING
An important task in using a Physics Engine is the ability to select which sets of objects collides and which sets don’t. For example in a shooting game you want your bullet to hit enemy troops but not friendly troops or you in a Fantasy /Mystical game and you are able to cast a slightly transparent wall to block a monster but yet you are able to shoot fire balls through the wall.
The Bullet Physics Engine has three easy ways to ensure that only certain objects collide with each other: masks, broadphase filter callbacks and nearcallbacks. Of these three only masks is supported in Urho3D and as such is the method we will look into.
Every rigid body in Urho3D has a collision layer and collision mask member variables. The collision layer represents the collision group of the rigid body while the collision mask represents the mask for other rigid bodies that can be collided with. Let us assume we have to bodies A and B, collision between them can only occur if a bitwise AND operation between the collision mask of A and the collision layer of B is anything but zero and vice versa (that is the collision mask of B against the collision layer of A also).
Using the data provided in the table below
ANDing the mask of A, 1 (which is 0001 in binary form), with the layer of B, 3, will give us
0001 AND 0011 = 0001
which is non-zero. Doing it the other way round using B’s mask against A’s layer would be
0010 AND 0010 = 0010
which again is non-zero. Since the non-zero condition was met in both ways bodies A and B will collide. Using this method you’ll also find out that A and C will equally collide but not B and C.
You can have at most 15 unique collision layers and collision masks. The reason for this goes down to Bullet Physics which Urho3D uses. Bullet uses a signed short int to store the collision groups (say collision layer) and the collision masks.
Note that additionally in Urho3D
COLLISION DETECTION
Filtering collision is one thing while being able to detect collision is another. Using a physics Engine or its implementation would be very uninteresting if we are not able to detect when two objects collide. In designing your game you’d want to know when a player makes contact with a health bottle so as to refill the health bar, you also want to know when a bullet hits the enemy in order to deduct health.
Collisions in Urho3D physics are reported through the collision physics events. Since these are events all you need do is subscribe to the appropriate one for your situation. The event you might use often is the E_NODECOLLISIONEVENT that is sent out by a node undergoing collision. Among the information provided by these events are the collided scene nodes and rigid bodies, whether either of the bodies is a trigger, and the list of contact points.
The contact point gives further details about the collision such as the world position of the contact between the two bodies and the impulse applied in collision.
Let’s say you want to make a bowling game. You have the ball and the pins arranged. But the problem is you’d have to code all the steps involved in rolling the ball and the ball knocking down the pins. So you set out to code the dynamics involved: how the ball will roll on the floor, its collision with the pins, the force of collision, how the pins will collide against each other and so on. Before you are through you are already regretting why you did pay attention to Physics Mechanics in school.
Well, before you start regretting, some people have probably regretted it too and came up with a solution, Physics Engines. A physics Engine is a software, a group of codes or a collection of objects and algorithms that provides functions for the simulation of a physical world be it in games and other virtual reality application. You don’t have to stress yourself coding the movement of a bowling ball or how it collides with pins when you can have them represented by physics objects and then simulating the whole action in a realistic way.
What do you want to make? Is it a catapult game like AngryBirds or a shooting game or you even want to make your own soccer game, Physics Engine got you back well covered.
There are a handful of Physics Engines available, some are free while others you might pay a token to use and still yet others we don’t even know about mainly because they are in-house tools. Some of the ones we know are ODE, PhysX, Newton and Bullet. We will shed a little bit light on Bullet Physics since it is the implemented Physics Engine in Urho3D.
IT’S A PHYSICAL WORLD
Bullet Physics is an Open Source Physics Engine that provides objects and functions for Collision Detections and Rigid Body Dynamics. It has quite a decent amount of fame having a wide integration into so many Game Engines and 3D modelling packages. It has even been used in some movies, if you doubt me ask Google.
To use a Physics Engine like Bullet there are certain things you’d have to understand.
Physics World
There has to be a world that obeys the laws of physics except you are in a parallel Universe that has its own set of Physical Laws. In Bullet Engine this world is called a Collision World and has among its derivatives the Dynamic World.
class btDynamicsWorld : public btCollisionWorld {...}
The Physics World has options to set gravity and exposes functions and objects for the following:
Collision Detection:
Detects when two objects collide so that you can deduct the health of a monster when your sword slashes through it.
Collision Filtering:
Being able to set which objects should collide and which should not. Like a 1Up appearing and the enemies can pass through without absorbing it, but your character passes and picks it up.
Rigid Body Dynamics:
The force, mass, inertia, velocity and constraints of the World. In a snooker game you take a shot, the cue ball rolls and knocks against ball which gradually rolls before coming to a stop. Or you shot a hanging sign post and it swings around.
Above all these functions is that to run the physics simulation:
btDynamicWorld::stepSimulation()
To this function you pass in the elapsed time since it was last called, with this the world updates all of its objects (collisions are checked for, dynamics applied to rigid bodies and so on) and performs other necessary functions to achieve a proper physics simulation for that time step.
Rigid Body and Collision Shape
For any form of physics interaction to occur there has to be a body, yes somebody has to be there. In Bullet this body is called a collision object or a rigid body (note: rigid body derives from collision object). A rigid body is what moves, collides, has mass and can have impulse applied to it. But on its own it is formless, more like a ghost without a shell. It needs a shape so as to interact in collisions and to also help in calculating its inertia tensor (distribution of mass). This is achieved by the addition of a Collision Shape
Bullet supports a large variety of different collision shapes, and it is possible to add your own. For best performance and quality it is important to choose the collision shape that suits your purpose.
Some of the collision shapes supported by Bullet are primitive shapes (e.g box, sphere, cylinder, capsule, cone and multisphere) , compound shapes, triangle mesh, convex hull and static plane. The explanation for some of these will come when we enter the next section.
Note that when a rigid body has a mass of zero it means the body has infinite mass hence it is static.
The Physics Illusion
It is important to note that in most cases the physics world and its objects are actually not part of your scene or game world. The physics world is in a world of its own on a different realm from your game. What the physics world really does is to model the physical objects of your scene and their possible interaction using its own objects. It is now your duty to update the state (transform) of your object, especially in the main loop, based on the state of its corresponding physics object. An example will make this clear.
Let us assume you have a box and a plane model in your scene, and you want the box to fall unto the plane from a height. You’d have to model your scene in the physics world using objects provided by Bullet:
First set the Physics World's gravity, 9.8 is okay. Add rigid body with a mass of zero and a static plane collision shape. Next add another rigid body with a non-zero mass and a box collision shape to the world. Set its transform to have a position some heights above the plane
After your physics world has been setup, in your application’s Main loop call the world’s stepSimulation() function, obtain the new transform of the box rigid body and update your box model’s transform with it.
That is how it happens: while the Physics World is running, you are busy extracting out information from it to update your scene.
[b
DODGING THE BULLET[/b]
If you’ve ever worked with a rendering engine, game engine, or a 3D visualization application that has no physics and you want to integrate physics into it you’ll find that there are frustrations and a bit of stress involved in it. You’d have to synchronize the both worlds and so many other things in order to make the physics work.
Urho3D tried to save us a lot of these stresses by neatly integrating the Bullet Physics into its structure. In fact, Bullets is completely buried into the framework that you use the physics objects just like every other Urho3D components, with the Physics illusion completely taken care of.
The important Physics components in Urho3D are the PhysicsWorld, RigidBody, CollisionShape and Constraints.
PhysicsWorld
This is what it is, a Physics World, just as has been explained in the previous section. Unlike in raw Bullet where you’d pass in a couple of objects to instantiate the world, in Urho3D you just create it as a regular component unto the scene object.
Scene* scene_ = new Scene(context_);
scene_->CreateComponent<PhysicsWorld>();
Lots of things are set by default for you and you can still make your own configurations through the provided functions.
Rigid Body
The rigid body in Urho3D is just as was explained for Bullet but with the exception that it is created as a component unto a node:
Node *boxNode = scene_->CreateChild(“BoxNode”);
boxNode->CreateComponent<RigidBody>();
The good thing about this is format is you don’t have to manually update the transform of the node for each iteration of the main loop; it is done automatically for you.
Collision Shape
Just as in raw Bullet Engine, a rigid body needs a collision shape. The collision shape component of Urho3D is created unto a node just like every other component:
boxNode->CreateComponent<CollisionShape>();
The supported shapes are box, sphere, cylinder, capsule, cone, triangle mesh, convex hull and heightfield terrain (requires the Terrain component in the same node).
CollisionShape provides two APIs for defining the collision geometry. Either setting individual properties such as the shape type or size, or specifying both the shape type and all its properties at once: see for example SetBox(), SetCapsule() or SetTriangleMesh().
Constraints
Joints, that is what this is in its simplest explanation, just joints. A constraint component connects two rigid bodies together or connects a rigid body to a static point in the world. The following are Bullet Physics constraints supported in Urho3D (explanation and diagram are gotten from Bullet Physics User Manual) :
Point (or Point to Point)
Point to point constraint limits the translation so that the local pivot points of 2 rigid bodies match in worldspace. A chain of rigid bodies can be connected using this constraint.

Hinge Constraint:
Hinge constraint, or revolute joint restricts two additional angular degrees of freedom, so the body can only rotate around one axis, the hinge axis. This can be useful to represent doors or wheels rotating around one axis. The user can specify limits and motor for the hinge.

Slider Constriant:
The slider constraint allows the body to rotate around one axis and translate along this axis.

Cone Twist Constraint:
To create ragdolls, the cone twist constraint is very useful for limbs like the upper arm. It is a special point to point constraint that adds cone and twist axis limits. The x-axis serves as twist axis.
In Urho3D
Both a RigidBody and at least one CollisionShape component must exist in a scene node for it to behave physically (a collision shape by itself does nothing). Several collision shapes may exist in the same node to create compound shapes. An offset position and rotation relative to the node's transform can be specified for each. Triangle mesh and convex hull geometries require specifying a Model resource and the LOD level to use.
COLLISION FILTERING
An important task in using a Physics Engine is the ability to select which sets of objects collides and which sets don’t. For example in a shooting game you want your bullet to hit enemy troops but not friendly troops or you in a Fantasy /Mystical game and you are able to cast a slightly transparent wall to block a monster but yet you are able to shoot fire balls through the wall.
The Bullet Physics Engine has three easy ways to ensure that only certain objects collide with each other: masks, broadphase filter callbacks and nearcallbacks. Of these three only masks is supported in Urho3D and as such is the method we will look into.
Every rigid body in Urho3D has a collision layer and collision mask member variables. The collision layer represents the collision group of the rigid body while the collision mask represents the mask for other rigid bodies that can be collided with. Let us assume we have to bodies A and B, collision between them can only occur if a bitwise AND operation between the collision mask of A and the collision layer of B is anything but zero and vice versa (that is the collision mask of B against the collision layer of A also).
Using the data provided in the table below
Rigid Body | Collision Layer | Collision Mask |
A | 0010 (2) | 0001 (1) |
B | 0011 (3) | 0010 (2) |
C | 0101 (5) | 0011 (3) |
ANDing the mask of A, 1 (which is 0001 in binary form), with the layer of B, 3, will give us
0001 AND 0011 = 0001
which is non-zero. Doing it the other way round using B’s mask against A’s layer would be
0010 AND 0010 = 0010
which again is non-zero. Since the non-zero condition was met in both ways bodies A and B will collide. Using this method you’ll also find out that A and C will equally collide but not B and C.
You can have at most 15 unique collision layers and collision masks. The reason for this goes down to Bullet Physics which Urho3D uses. Bullet uses a signed short int to store the collision groups (say collision layer) and the collision masks.
Note that additionally in Urho3D
A rigid body can also be set to trigger mode to only report collisions without actually applying collision forces. This can be used to implement trigger areas
COLLISION DETECTION
Filtering collision is one thing while being able to detect collision is another. Using a physics Engine or its implementation would be very uninteresting if we are not able to detect when two objects collide. In designing your game you’d want to know when a player makes contact with a health bottle so as to refill the health bar, you also want to know when a bullet hits the enemy in order to deduct health.
Collisions in Urho3D physics are reported through the collision physics events. Since these are events all you need do is subscribe to the appropriate one for your situation. The event you might use often is the E_NODECOLLISIONEVENT that is sent out by a node undergoing collision. Among the information provided by these events are the collided scene nodes and rigid bodies, whether either of the bodies is a trigger, and the list of contact points.
The contact point gives further details about the collision such as the world position of the contact between the two bodies and the impulse applied in collision.