Title: Introducing Physics to Ogre
1EIE360 Integrated Project
- Lecture 4
- Introducing Physics to Ogre
References 1 Gregory Junker, Pro OGRE 3D
Programming, Apress, 2006 2 Ogre Tutorials
Ogre Wiki http//www.ogre3d.org/wiki/index.php/Ogr
e_Tutorials 3 Microsoft MSDN, C reference 4
Newton Game Dynamics http//newtondynamics.com 5
OgreNewt http//walaber.com/index.php?actionshow
itemid9 6 OgreNewt Source https//svn.ogre3d.o
rg/svnroot/ogreaddons/branches/ogrenewt/newton20/s
rc/
1
2Architecture of the Interactive Virtual Aquarium
System
Computer A
USB port
Your program
Kinect Sensor Device
Network
Computer B
3D Graphics System
Your program
3D Graphics
2
3Game Physics
- Computer animation physics or game physics
involves the introduction of the laws of physics
into a simulation or game engine - With game physics, we will see game objects react
to stimulation similar to real objects in real
world - Become more and more important in computer game
development since it provides the realism to the
game - To facilitate the implementation of game physics,
physics engines have been developed - Professional ones Havok very expensive
- Free engines ODE, Tokamak, PhysX, and Newton
Game Dynamics
3
4Newton Game Dynamics
- Newton Game Dynamics is a physics engine for
realistically simulating rigid bodies in games - In contrast to most other real-time physics
engines it goes for accuracy over speed - Its solver is deterministic and not based on
traditional iterative methods - Advantages it can handle higher mass ratios (up
to 4001) and the simulation is very robust and
easy to tune - Disadvantage a bit slower than engines with an
iterative solver - However the new Newton 2.0 has greatly improved
the simulation speed - To use Newton, a SDK should be downloaded from
4 and installed in the computer
5OgreNewt Library
- OgreNewt is a library that wraps the Newton Game
Dynamics physics SDK into an object-oriented set
of classes that facilitate integration with the
OGRE 3D engine - OgreNewt library is pretty much a 11 conversion
of the Newton functions into a class-based
environment - OgreNewt is available as a part of "ogreaddons"
in the Ogre CVS. It is also available from 5 - Different from Ogre, there is not a
well-documented API reference for OgreNewt - Can check the usage of APIs from their source 6
5
6Newton Basic Elements
Basic Elements
World
Collisions
Joints
Rigid Bodies
Materials
Others
Primities
TreeCollisions
MaterialID
Convex Hulls
MaterialPair
6
7Newton Basic Elements World
- WORLD (OgreNewtWorld)Â
- This is the "space" in which all objects exist
- In most applications, we only need 1 World
object, inside which all other objects are placed - However the system allows for multiple worlds to
co-exist
OgreNewtWorld mWorld new OgreNewtWorld()
mWorld-gtsetWorldSize( , ) //specify a
box with 2 3-dimensional points //as the min
and max
7
8Newton Basic Elements World framelistener
- The Newton world has its own framelistener to
update all the objects in each time step. Need to
be created and add to Ogre - A framelistener receives notification before and
after a frame is rendered to the screen
OgreFrameListener mNewtonListener new
OgreNewtBasicFrameListener(mWindow,
mWorld) mRoot-gtaddFrameListener(mNewtonListener)
//One more framelistener is added
9Newton Basic Elements Rigid Body
- RIGID BODY (OgreNewtBody)Â
- This is the basic object in the physics world
- It represents a solid (rigid) body, which can
interact with other bodies in the scene - Bodies can have mass, size, and shape. Basically
everything we want to be affected by physics
calculations needs a Body - However, to create a Body, we need to first
define its collision primitive
9
10Newton Basic Elements Rigid Body Example
Body for the barrel
Body for the floor
Body for the fish
11Newton Basic ElementsCollision
- COLLISION (OgreNewtCollision)Â
- Visible shape of an object is defined by its mesh
model - Rigid Bodies require a Collision object to define
their shape for physics calculation E.g. to
calculate the reaction after colliding with
another object - Not necessarily equal to the visible shape,
actually often much simple - One of the purposes is to reduce the computation
complexity since visible shape can be very
complex - Newton provides several different kinds of
collision objects. They include Primitive shapes,
Convex Hulls and Tree Collisions
11
12Newton Basic ElementsCollision - TreeCollisions
- TREE COLLISIONS
- TreeCollision objects are just polygon collision
objects - For any object made up by polygons, we can create
a TreeCollision object from it - However, TreeCollision objects CANNOT be used for
active rigid bodies (e.g. movable bodies) - All Bodies created from a TreeCollision will
automatically have infinite mass, and therefore
be completely immobile - Best used for "background" objects that will not
move - For moving objects, use convex hulls or
primitives.
12
13Newton Basic ElementsCollision - TreeCollisions
- Assume the entity of the ground groundEnt has
been created
An id for the object
OgreNewtCollisionPtr col( new
OgreNewtCollisionPrimitivesTreeCollision (
mWorld, groundNode, false, 1 )) // Create the
TreeCollision object OgreNewtBody ground_body
new OgreNewtBody( mWorld, col ) // Create
the rigid body
mean we dont want newton to try to optimize the
object for us
- In the example, we would like to create a
TreeCollision object for the ground, since it
will not move
13
14Newton Basic ElementsCollision Convex Hulls
- Convex Hulls
- Convex hulls are a more general primitive type
- They take a series of points in space, and create
the smallest possible convex shape based on all
of those points - In most cases, we would use the vertices that
makeup a model for the points - This results in a primitive shape that looks
something like our 3D model wrapped up in
wrapping paper
14
15Newton Basic ElementsCollision Convex Hulls
- In the example, the collision objects for the
barrel and the fish are of the primitive type
convex hulls
16Newton Basic ElementsCollision Convex Hulls
- Assume the entity for the Barrel has been created
and pointed by barrelEnt
An id for the object
OgreNewtConvexCollisionPtr col_convex(
new OgreNewtCollisionPrimitivesConvexHull (m
World, barrelEnt, 2)) // Create the convex hull
object OgreNewtBody barrel_body new
OgreNewtBody( mWorld, col_convex ) //
Create the Body
16
17How the Body is connected to the 3D World?
- Newton will automatically update the position and
rotation of object bodies based on physics laws - Newton has a callback system built in, which
automatically calls only the callback for bodies
that are moving, bodies that have not moved since
the last update are properly ignored - To enable the above, we only need to attach the
body to the scene node and set the initial
position and orientation
ground_body-gtattachToNode( groundNode
) ground_body-gtsetPositionOrientation( )
17
18Mass, MOI, and Force
- If we want objects to move following physics
laws, a few parameters of the objects should be
defined - Mass the mass of the object
- MOI moment of inertia
- Force the force applied to the object
- Mass is simple, we can use any units that see
fit, although usually Kilograms is used - Inertia might not be so intuitive, as it's a
value that represents an objects resistance to
rotation around a specific axis, many factors can
affect this value - An object having mass and inertia defined still
cannot move since it needs a force to trigger the
motion
18
19Mass and MOI
- OgreNewt provides standard functions to define
the mass and inertia of objects
OgreReal mass 1.0 OgreVector3 inertia
OgreNewt MomentOfInertiaCalcSphereSolid(mass,
10) //Compute the MOI of a sphere of size
10 //Check OgreNewt reference for other
shapes //such as cylinder, box,
etc. bod-gtsetMassMatrix( mass, inertia )
//Assume bod is the Body we want to define
19
20Mass and MOI (cont)
- For convex hulls, OgreNewt also provides a more
general function calculateInertiaMatrix() - It calculates the MOI for a collision primitive,
as well as the computed center of mass - Two Vector3 objects should be passed into the
function and they will be populated with the info - It is only for boxes, ellipsoids and convex hulls
but NOT work on TreeCollisions.
OgreVector3 inertia, centre_of_mass col_convex-
gtcalculateInertialMatrix(inertia,
centre_of_mass) //Assume the collision
object is col_convex
20
21Force
- In Newton world, object cannot be translated by
moving its scene node, since it does not follow
physics - Need to apply a force to the object to make it
move - The most common type of force is the gravity
- OgreNewt provides a standard function
setStandardCallback() to apply a constant
gravitational (-Y) force of 9.8units/sec2 to
bodies
barrel_body-gt setStandardForceCallback() //So
the barrel will fall //onto the floor
Gravity force
21
22Force (cont)
- For custom force, one needs to add a special
callback function to apply to a body
EIE360ProjectcreateScene() mFish_body-gtsetCu
stomForceAndTorqueCallback
ltEIE360ProjectAppgt(EIE360ProjectApp
customSwimCallback, this) // register the
callback function EIE360ProjectcustomSwimCallb
ack(OgreNewtBody body, float timestep, int
threadIndex) //This callback function will be
called when newton // wants to apply force to the
body body-gtaddForce() body-gtsetForce()
22
Time since last call
Id of the thread calling this function
23Example Fish Swimming
EIE360Project customSwimCallback(OgreNewtBody
body, float timestep, int threadIndex)
OgreReal mass OgreVector3 inertia
body-gtgetMassMatrix(mass, inertia)
OgreVector3 velocity (mFishLastPosition -
pos)/ timeStep OgreVector3 accel (velocity
body-gtgetVelocity()) / timeStep
OgreVector3 force mass accel
body-gtaddForce(force)
Current pos
Pos it wants to go
F MA!!!
Velocity needed to go the target pos
Current velocity
23
23
24Example Changing Orientation
OgreVector3 direction
mFishLastPosition - newPos direction.normalise()
OgreVector3 pos mFish_body-gtgetPosition() m
Fish_body-gtsetPositionOrientation(pos,
OgreVector3UNIT_X.getRotationTo(direction)) Â
The default orientation of the fish model
Return the orientation of the fish that heads to
the required direction
24
24
25Collision Detection
- Collision detection is one of the most important
operations to enable interactions between objects
in the game world - By collision detection, it refers to the
automatic detection and subsequent actions when
two game objects are collided - An automatic approach is essential since there
can be numerous objects collide with each other
at a particular time. It will be extremely time
consuming if programmers need to manually do this
in their programs - A standard procedure for collision detection can
be found in Ogre - Newton (and OgreNewt) provides an even simpler
approach by using materialID and materialPair
25
26Newton Basic ElementsMaterial
- MATERIAL (OgreNewtMaterialID
OgreNewtMaterialPair)Â - Materials are how Newton lets one adjust the
interaction between bodies when they collide - This can be as simple as adjusting the friction,
or much more complex - The material system is pretty simple
- First create "MaterialID" objects to represent
each material that might be required in the
system - Then build what is called a "MaterialPair". A
material pair is a description of what happens
when 2 materials collide with each other
26
27OgreNewts Collision Detection System
- When the screen is updated with a new frame,
OgreNewt will also update the position and
orientation of each body by calling
OgreNewtWorldupdate() - At the same time, OgreNewt also checks if two
bodies are collided - The collision detection procedure starts with the
overlapping of the AABB of two bodies
- AABB stands for Axis Aligned Bounding Box
- All meshes have a "bounding box" (a cube in which
the entire mesh fits in)
28OgreNewts Collision Detection System (cont)
A simplified flow diagram (assume bodies only
contact once and all functions return 1)
Update()
Move all bodies
AABB overlap?
Y
onAABBOverlap()
N
Bodies actually contact?
Y
contactsProcess()
N
End
29OgreNewts Collision Detection System (cont)
- onAABBOverlap()
- Called if the AABB of the 2 bodies in question
overlap - If it returns 0, OgreNewt will not perform
further action even if it is really a collision.
Return 1 otherwise. - contactsProcess()
- Called if the 2 bodies in question really collide
- Inside this function you can get lots of
information about the contact (contact normal,
speed of collision along the normal, tangent
vectors, etc), or even modify some collision
parameters
30A Simple Example
- Assume we want to detect if a fish collides with
the barrel in the game world. If yes, some
bubbles are generated around the barrel
31Step 1
- Create a materialID for the fish and the barrel.
Then group them into a materialPair and connect
them to their body. In createScene(), add the
following codes
OgreNewtMaterialID fish_material_id new
OgreNewtMaterialID(mWorld) OgreNewtMaterialI
D barrel_material_id new
OgreNewtMaterialID(mWorld) //Group them into
a pair OgreNewtMaterialPair pair new
OgreNewt MaterialPair(mWorld,
fish_material_id, barrel_material_id
) mFish_body-gtsetMaterialGroupID(fish_material_id
) barrel_body-gtsetMaterialGroupID(barrel_material
_id)
32Relationship between classes
EIE360Project
FishBarrelContactCallback
new FishBarrelContactCallback(this) pair-gt
setContactCallback (fishBarrelCallback) start
Bubbles() ...
mProject this
contactsProcess() mProject-gt startBubbles()
Call if there is contact between fish and barrel
33Step 2
- Tell OgreNewt where the callback functions of
this materialPair can be found - In this example, the callback functions are
implemented in a class called FishBarrelContactCal
lback - In createScene(), add the following statements
FishBarrelContactCallback fishBarrelCallback
new FishBarrelContactCallback(this) //Instantia
te an object of that class pair-gtsetContactCallbac
k(fishBarrelCallback) //Tell the system the
callback functions of //the materialPair pair
can be found in that //object
34Step 3
- Design the class FishBarrelContactCallback
FishBarrelContactCallbackFishBarrelContactCallba
ck (EIE360Project project) mProject
project //mProject is a member variable of the
class FishBarrelContactCallback FishBarrelCo
ntactCallback(void) void FishBarrelContactCall
backcontactsProcess(
OgreNewtContactJoint contactJoint,
OgreReal timeStep, int threadIndex ) //Codes
for collision detection // you can use the
ContactJoint to iterate through //contact-points
35Step 4
- Implement contactsProcess()
void FishBarrelContactCallbackconstactsProcess(
OgreNewtContactJoint contactJoint,
OgreReal timeStep, int threadIndex) mProje
ct-gtstartBubbles()
The function startBubbles() should have been
implemented In EIE360Project() that will
generate bubbles around the barrel