11 Lecture 4 Introducing Physics to Ogre References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki

Slides:



Advertisements
Similar presentations
Intersection Testing Chapter 13 Tomas Akenine-Möller Department of Computer Engineering Chalmers University of Technology.
Advertisements

The robot structure model design 2 Curse 5. Modeling: the robot AcTrMStTk V(t) T(t)  (t) q(t) x(t)
Collision Detection CSCE /60 What is Collision Detection?  Given two geometric objects, determine if they overlap.  Typically, at least one of.
Collision Detection and Resolution Zhi Yuan Course: Introduction to Game Development 11/28/
Computer graphics & visualization Collisions. computer graphics & visualization Simulation and Animation – SS07 Jens Krüger – Computer Graphics and Visualization.
Week 14 - Monday.  What did we talk about last time?  Bounding volume/bounding volume intersections.
Move With Me S.W Graduation Project An Najah National University Engineering Faculty Computer Engineering Department Supervisor : Dr. Raed Al-Qadi Ghada.
Particle Systems 1 Adapted from: E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012.
Scenegraph. Scenegraph Nodes - all implement a run function Leaf/Geo Nodes - typedefs of sgNode superclass –Poly Mesh A geo node that stores a list of.
Game Physics Chris Miles. The Goal To learn how to create game objects with realistic physics models To learn how to simulate aspects of reality in order.
Introduction What is this ? What is this ? This project is a part of a scientific research in machine learning, whose objective is to develop a system,
Basic concept Technologies we have used The Design Problems, challenges & solutions Educational Gain.
University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology 3D Engines and Scene Graphs Spring 2012.
Computer graphics & visualization Collision Detection – Narrow Phase.
1 References: 1. J.M. Hart, Windows System Programming, 4th Ed., Addison-Wesley, 2010, Ch.12 2.Microsoft Kinect SDK for Developers,
CS 4730 Physical Simulation CS 4730 – Computer Game Design.
Vectors 1D kinematics 2D kinematics Newton’s laws of motion
UFCEKU-20-3Web Games Programming Unity 3D Physics Colliders and Object Collisions.
Lecture VII Rigid Body Dynamics CS274: Computer Animation and Simulation.
DYNAMICS Part I Physics Engine By Willis (The Magnificent) Louie Fei (The Coyote) Liao.
Game Physics – Part I Dan Fleck Coming up: Rigid Body Dynamics.
Free-body Diagrams To help us understand why something moves as it does (or why it remains at rest) it is helpful to draw a free-body diagram. The free-body.
A Spring 2005 CS 426 Senior Project By Group 15 John Studebaker, Justin Gerthoffer, David Colborne CSE Dept., University of Nevada, Reno Advisors (CSE.
CSE 380 – Computer Game Programming Box2D Box2D TestBed.
1 Lecture 2 Animation References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki
CSE 381 – Advanced Game Programming Quickhull and GJK.
Computer Animation Rick Parent Computer Animation Algorithms and Techniques Collisions & Contact.
Week 13 - Monday.  What did we talk about last time?  Exam 2!  Before that…  Polygonal techniques ▪ Tessellation and triangulation  Triangle strips,
Evolving Virtual Creatures & Evolving 3D Morphology and Behavior by Competition Papers by Karl Sims Presented by Sarah Waziruddin.
1 VR assignment#2 WTK SDK Introduction By Jin-Bey Yu 2001/11/12.
Chapter 4 The Laws of Motion. Classes of Forces Contact forces involve physical contact between two objects Field forces act through empty space No physical.
11 Lecture 3 Particle Effects References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki
Digital Media Lecture 8: Vector Graphics 3D Georgia Gwinnett College School of Science and Technology Dr. Jim Rowan.
Advanced Computer Graphics Rigid Body Simulation Spring 2002 Professor Brogan.
University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology Physics for Games Spring 2012.
Point Sprites Course Information CVG: Programming 4 My Name: Mark Walsh Website: Recommended.
Digital Media Dr. Jim Rowan ITEC 2110 Vector Graphics II.
Chipmunk Physics Remember that we talked about this a bit when we did collision handlers for the space ship integration task (SpritesActionsPhysicsSound).
Computer Game Design and Development
Games Development 1 Review / Revision CO2301 Games Development 1 Semester 2.
Ray Tracing Fall, Introduction Simple idea  Forward Mapping  Natural phenomenon infinite number of rays from light source to object to viewer.
Presented by Paul Phipps
CS274 Spring 01 Lecture 7 Copyright © Mark Meyer Lecture VII Rigid Body Dynamics CS274: Computer Animation and Simulation.
Rigid Body Dynamics CSE169: Computer Animation
Advanced Games Development Game Physics CO2301 Games Development 1 Week 19.
Wednesday, Nov. 10, 2004PHYS , Fall 2004 Dr. Jaehoon Yu 1 1.Moment of Inertia 2.Parallel Axis Theorem 3.Torque and Angular Acceleration 4.Rotational.
Havok Xtra Training
1 Chapter 4 The Laws of Motion Classes of Forces Contact forces involve physical contact between two objects Field forces act through empty.
Game Programming 13 Physics in Games (cont.) 2010 년 2 학기 디지털콘텐츠전공.
Animation Animation is about bringing things to life Technically: –Generate a sequence of images that, when played one after the other, make things move.
Computer Graphics Imaging Ying Zhu Georgia State University Lecture 29 Soft Bodies and Rigid Bodies.
Digital Media Dr. Jim Rowan ITEC 2110 Vector Graphics II.
Computer Animation Ying Zhu Georgia State University
Lecture Rigid Body Dynamics.
Computer Animation Ying Zhu Georgia State University
Computer Graphics Imaging Ying Zhu Georgia State University
Manipulator Dynamics 2 Instructor: Jacob Rosen
Useful Tools for Making Video Games
Week 6: Time and triggers!
Motion in Real and Virtual Worlds
Dr. Jim Rowan ITEC 2110 Vector Graphics II
Computer Animation Algorithms and Techniques
Collision Detection.
Step 1: Get Organized Draw a picture.
Advanced Games Development Game Physics
Computer Graphics Lecture 15.
Games Development 1 Review / Revision
Dr. Jim Rowan ITEC 2110 Vector Graphics II
GPAT – Chapter 7 Physics.
Presentation transcript:

11 Lecture 4 Introducing Physics to Ogre References: [1] Gregory Junker, Pro OGRE 3D Programming, Apress, 2006 [2] Ogre Tutorials – Ogre Wiki [3]Microsoft MSDN, C++ reference [4] Newton Game Dynamics [5] OgreNewt [6] OgreNewt Source 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 Graphics System 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] 5 Department of ELECTRONIC AND INFORMATION ENGINEERING 4. Introducing Physics to Ogre by Dr Daniel Lun

6 Newton Basic Elements 6 Basic Elements WorldCollisionsJointsRigid BodiesMaterialsOthers Primities Convex Hulls TreeCollisionsMaterialID 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 8 Department 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 9 Department of ELECTRONIC AND INFORMATION ENGINEERING 4. Introducing Physics to Ogre by Dr Daniel Lun

Newton Basic Elements: Rigid Body – Example 10 Department 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 11 Department 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. 12 Department 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 14 Department 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” 15 Department 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( 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 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 18 Department 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 force Department 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:: 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 callId 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 26 Department 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 Y N 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 pair OgreNewt::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 FishBarrelContactCal lback(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 class pair->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