Physics - Department of Electronic and Information Engineering

advertisement
EIE360 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/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/
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
1
Architecture 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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
2
Game 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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
3
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
4
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]
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
5
Newton Basic Elements
Basic Elements
World Rigid Bodies
Primities
Collisions Joints
TreeCollisions
Convex Hulls
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
Materials Others
MaterialID
MaterialPair
6
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 coexist
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
7
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
Ogre::FrameListener *mNewtonListener =
new OgreNewt::BasicFrameListener(mWindow,
mWorld);
mRoot->addFrameListener(mNewtonListener);
//One more framelistener is added
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
8
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
9
Newton Basic Elements:
Rigid Body – Example
Body for the barrel
Body for the floor
Body for the fish
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
10
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
11
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.
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
12
Newton Basic Elements:
Collision - TreeCollisions

Assume the entity of the ground groundEnt has been
created
An id for the object
OgreNewt::CollisionPtr col( new
OgreNewt::CollisionPrimitives::TreeCollision
( mWorld, groundNode, false, 1 ));
// Create the TreeCollision object mean we don’t
OgreNewt::Body *ground_body = new
want newton to
OgreNewt::Body( mWorld, col );
try to optimize the
object for us
// Create the rigid body
• In the example, we would like to
create a TreeCollision object for
the ground, since it will not move
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
13
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
14
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”
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
15
Newton Basic Elements:
Collision – Convex Hulls

Assume the entity for the Barrel has been created and
pointed by barrelEnt
An id for the object
OgreNewt::ConvexCollisionPtr col_convex( new
OgreNewt::CollisionPrimitives::ConvexHull
(mWorld, barrelEnt, 2));
// Create the convex hull object
OgreNewt::Body *barrel_body = new
OgreNewt::Body( mWorld, col_convex );
// Create the Body
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
16
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
ground_body->attachToNode( groundNode );
ground_body->setPositionOrientation( … );
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
17
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
18
Mass and MOI

OgreNewt provides standard functions to define the
mass and inertia of objects
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
19
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.


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
20
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
barrel_body->
setStandardForceCallback();
//So the barrel will fall
//onto the floor
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
Gravity force
21
Force (cont)

For custom force, one needs to add a special callback
function to apply to a body
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(…);
}
22
Time since last call
Id of the thread calling this function
Example – Fish Swimming
EIE360Project:: customSwimCallback(OgreNewt::Body*
body, float timestep, int threadIndex) {
Ogre::Real mass;
Current pos
Ogre::Vector3 inertia;
body->getMassMatrix(mass, inertia); Pos it wants to go
Ogre::Vector3 velocity =
(mFishLastPosition - pos)/ timeStep;
Ogre::Vector3 accel = (velocity –
body->getVelocity()) / timeStep;
Ogre::Vector3 force = mass * accel;
body->addForce(force);
}
F = M*A!!!
Velocity needed to go
the target pos
Current velocity
23
Example – Changing
Orientation
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
24
Collision 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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
25
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
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
26
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
27
OgreNewt’s Collision
Detection System (cont)
Update()
A simplified flow diagram
(assume bodies only contact
once and all functions return 1)
Move all
bodies
AABB overlap?
N
Y
onAABBOverlap()
Bodies actually Y
contactsProcess()
contact?
N
End
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
28
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
29
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
30
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
OgreNewt::MaterialID* fish_material_id = new
OgreNewt::MaterialID(mWorld);
OgreNewt::MaterialID* barrel_material_id = new
OgreNewt::MaterialID(mWorld);
//Group them into a pair
OgreNewt::MaterialPair* pair = new OgreNewt::
MaterialPair(mWorld,
fish_material_id, barrel_material_id );
mFish_body->setMaterialGroupID(fish_material_id);
Department of
31
ELECTRONIC AND INFORMATION ENGINEERING
barrel_body->setMaterialGroupID(barrel_material_id);
4. Introducing Physics to Ogre by Dr Daniel Lun
Relationship between classes
EIE360Project
FishBarrelContactCallback
new
FishBarrelContactCal
lback(this);
mProject = this
pair->
setContactCallback
(fishBarrelCallback);
contactsProcess()
{ mProject->
startBubbles();
}
:
startBubbles()
{ ...
}
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
Call if there is contact
between fish and
32
barrel
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:
FishBarrelContactCallback* fishBarrelCallback = new
FishBarrelContactCallback(this);
//Instantiate an object of that class
pair->setContactCallback(fishBarrelCallback);
//Tell the system the callback functions of
//the materialPair pair can be found in that
//object
Department of
33
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
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
34
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
35
Department of
ELECTRONIC AND INFORMATION ENGINEERING
4. Introducing Physics to Ogre by Dr Daniel Lun
Download