11 Lecture 4 Introducing Physics to Ogre References: [1] Gregory Junker, Pro OGRE 3D Programming,...
-
Upload
aubrie-barnett -
Category
Documents
-
view
258 -
download
4
Transcript of 11 Lecture 4 Introducing Physics to Ogre References: [1] Gregory Junker, Pro OGRE 3D Programming,...
11
Lecture 4
Introducing Physics to OgreReferences:[1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki
http://www.ogre3d.org/wiki/index.php/Ogre_Tutorials[3] Microsoft MSDN, C++ reference[4] Newton Game Dynamics http://newtondynamics.com[5] OgreNewt http://walaber.com/index.php?action=showitem&id=9[6] OgreNewt Source
https://svn.ogre3d.org/svnroot/ogreaddons/branches/ogrenewt/newton20/src/
EIE360 Integrated Project
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
22
Architecture of the Interactive Virtual Aquarium System
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
USB port
3D GraphicsSystem
3D Graphics
Your program
Your program
Network
Computer A
Computer B
Kinect Sensor Device
3
Game Physics
3
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 Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
4
Newton 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 400:1) 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
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
5
OgreNewt 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 1:1 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]
5Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
6
Newton Basic Elements
6
Basic Elements
World Collisions JointsRigid Bodies Materials Others
Primities
Convex Hulls
TreeCollisions MaterialID
MaterialPair
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
7
Newton Basic Elements: World WORLD (OgreNewt::World)
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
7
OgreNewt::World *mWorld = new OgreNewt::World();mWorld->setWorldSize( … , …); //specify a box with 2 3-dimensional points
//as the min and max
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
Newton 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
8Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
Ogre::FrameListener *mNewtonListener = new OgreNewt::BasicFrameListener(mWindow,
mWorld);mRoot->addFrameListener(mNewtonListener);
//One more framelistener is added
9
Newton Basic Elements: Rigid Body
RIGID BODY (OgreNewt::Body) 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
9Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
Newton Basic Elements: Rigid Body – Example
10Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
Body for the barrel
Body for the fish
Body for the floor
11
Newton Basic Elements:Collision COLLISION (OgreNewt::Collision)
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
11Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
12
Newton Basic Elements:Collision - 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.
12Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
13
Newton Basic Elements:Collision - TreeCollisions
Assume the entity of the ground groundEnt has been created
13
OgreNewt::CollisionPtr col( new OgreNewt::CollisionPrimitives::TreeCollision( mWorld, groundNode, false, 1 ));// Create the TreeCollision object
OgreNewt::Body *ground_body = new OgreNewt::Body( mWorld, col );// Create the rigid body
• In the example, we would like to create a TreeCollision object for the ground, since it will not move
mean we don’t want newton to try to optimize the object for us
An id for the object
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
14
Newton Basic Elements:Collision – 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
14Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
Newton Basic Elements:Collision – Convex Hulls
In the example, the collision objects for the barrel and the fish are of the primitive type “convex hulls”
15Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
16
Newton Basic Elements:Collision – Convex Hulls
Assume the entity for the Barrel has been created and pointed by barrelEnt
16
OgreNewt::ConvexCollisionPtr col_convex( newOgreNewt::CollisionPrimitives::ConvexHull(mWorld, barrelEnt, 2));// Create the convex hull object
OgreNewt::Body *barrel_body = new OgreNewt::Body( mWorld, col_convex );// Create the Body
An id for the object
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
17
How 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
17
ground_body->attachToNode( groundNode );ground_body->setPositionOrientation( … );
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
18
Mass, 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
18Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
19
Mass and MOI
OgreNewt provides standard functions to define the mass and inertia of objects
19
Ogre::Real mass = 1.0; Ogre::Vector3 inertia = OgreNewt:: MomentOfInertia::CalcSphereSolid(mass, 10);
//Compute the MOI of a sphere of size 10//Check OgreNewt reference for other shapes//such as cylinder, box, etc.
bod->setMassMatrix( mass, inertia ); //Assume bod is the Body we want to define
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
20
Mass 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.
20
Ogre::Vector3 inertia, centre_of_mass;col_convex->calculateInertialMatrix(inertia,
centre_of_mass);//Assume the collision object is col_convex
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
21
Force
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/sec^2 to bodies
21
barrel_body-> setStandardForceCallback();//So the barrel will fall //onto the floor
Gravity forceDepartment of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
22
Force (cont) For custom force, one needs to add a special callback
function to apply to a body
22
EIE360Project::createScene(…) {mFish_body->setCustomForceAndTorqueCallback
<EIE360ProjectApp>(&EIE360ProjectApp:: customSwimCallback, this);// register the callback function
}EIE360Project::customSwimCallback(OgreNewt::Body* body, float timestep, int threadIndex) {//This callback function will be called when newton// wants to apply force to the body
body->addForce(…); body->setForce(…); }
Time since last call Id of the thread calling this function
23
Example – Fish Swimming
23
EIE360Project:: customSwimCallback(OgreNewt::Body* body, float timestep, int threadIndex) { Ogre::Real mass; Ogre::Vector3 inertia; body->getMassMatrix(mass, inertia); Ogre::Vector3 velocity =
(mFishLastPosition - pos)/ timeStep; Ogre::Vector3 accel = (velocity –
body->getVelocity()) / timeStep; Ogre::Vector3 force = mass * accel; body->addForce(force);}
Current pos
Pos it wants to go
F = M*A!!!Velocity needed to go the target pos
Current velocity
24
Example – Changing Orientation
24
Ogre::Vector3 direction = mFishLastPosition - newPos;direction.normalise();Ogre::Vector3 pos = mFish_body->getPosition();
mFish_body->setPositionOrientation(pos, Ogre::Vector3::UNIT_X.getRotationTo(direction));
The default orientation of the fish model
Return the orientation of the fish that heads to the required direction
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
25
Collision Detection
25
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
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
26
Newton Basic Elements:Material
MATERIAL (OgreNewt::MaterialID && OgreNewt::MaterialPair) 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
26Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
27
OgreNewt’s 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 OgreNewt::World::update()
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) Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
28
OgreNewt’s Collision Detection System (cont)
AABB overlap?
Update()
End
Move all bodies
onAABBOverlap()
Bodies actually contact? contactsProcess()
Y
YN
N
A simplified flow diagram(assume bodies only contact once and all functions return 1)
A simplified flow diagram(assume bodies only contact once and all functions return 1)
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
29
OgreNewt’s 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
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
30
A 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
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
31
Step 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
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
OgreNewt::MaterialID* fish_material_id = new OgreNewt::MaterialID(mWorld); OgreNewt::MaterialID* barrel_material_id = new OgreNewt::MaterialID(mWorld);
//Group them into a pairOgreNewt::MaterialPair* pair = new OgreNewt::
MaterialPair(mWorld, fish_material_id, barrel_material_id );
mFish_body->setMaterialGroupID(fish_material_id);barrel_body->setMaterialGroupID(barrel_material_id);
Relationship between classes
32
EIE360Project
FishBarrelContactCallback
mProject = this
contactsProcess(){ mProject->
startBubbles();}
new FishBarrelContactCallback(this);
pair-> setContactCallback(fishBarrelCallback);
:startBubbles(){ ...}
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
Call if there is contact between fish and barrel
33
Step 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 FishBarrelContactCallback
In createScene(), add the following statements:
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
FishBarrelContactCallback* fishBarrelCallback = new FishBarrelContactCallback(this);
//Instantiate an object of that classpair->setContactCallback(fishBarrelCallback);//Tell the system the callback functions of //the materialPair pair can be found in that//object
34
Step 3 Design the class FishBarrelContactCallback FishBarrelContactCallback::FishBarrelContactCallback
(EIE360Project *project){ mProject = project;
//mProject is a member variable of the class}FishBarrelContactCallback::
~FishBarrelContactCallback(void) { }void FishBarrelContactCallback::contactsProcess( OgreNewt::ContactJoint &contactJoint, Ogre::Real timeStep, int threadIndex ){ //Codes for collision detection
// you can use the ContactJoint to iterate through//contact-points
} Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
35
Step 4 Implement contactsProcess()
void FishBarrelContactCallback::constactsProcess( OgreNewt::ContactJoint &contactJoint, Ogre::Real timeStep,int threadIndex)
{mProject->startBubbles();
}
The function startBubbles() should have been implemented In EIE360Project() that will generate bubbles around the barrel
Department of ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun