450 likes | 627 Views
Physics simulation in a 3D environment. Sylvain EUDIER MSCS Candidate. Union College May 28 th , Spring 2004. Agenda. Why Physics Simulation? Where am I, where am I going? Start Coding Entry point: The Spring Model Extension to the Flag / Cloth simulation
E N D
Physics simulation in a 3D environment Sylvain EUDIER MSCS Candidate Union College May 28th, Spring 2004
Agenda • Why Physics Simulation? • Where am I, where am I going? • Start Coding • Entry point: The Spring Model • Extension to the Flag / Cloth simulation • Introduction to Collision Detection • Collision improvement: an example • Conclusion
Why Physics Simulation? • Getting more and more interest from the game industry • How does it work behind the scenes? • Combines physics and CS
Where am I? • Physics are used in many programs (CAD, games, simulators…) • Commercial physics libraries exist • As well as open source • Evolution of the simulation models up to now
Where am I going? • How precisely do we want to simulate the world • How do we want to represent it • For what expected quality / expense
Start Coding – Define the rules • Use of C++ • Representation using the OpenGL API • Game-like precision • Find a model for this problem (classes) • Starting point: Write a stable and easy-to-use CVector3D class
The CVector3D class • 3 constructors : Default, copy, by components • Overloading of operators: +, -, *, /, +=, -=, *=, /= • Methods: length, normalize, unit, crossProduct and dotProduct
What can I start with? • The spring model Simulate the behavior of a deformable object under certain constraints. • Easy to implement (as a beginning) • Gives convincing results rapidly • Allows me to test the architecture of my program
The Spring Model • To the basic formula, we add the inner friction (to stabilize it):
The Spring Model • These properties are the basics we can give to a mass. • Considered as a dot
The Spring Model • For the computation: L: steady length x: actual length of the spring u: unit vector between mass1 and mass2
Application to a rope • The rope is made of several masses that interact with each other • By changing the variables, the rope may be: stiffer, more / less extendable • We can create different kinds of extensible material
Demonstration • Rope simulation 1 • Rope simulation 2
Spring Model : First impressions • (+) The result looks good enough for such a simple simulation. • (-) The rope behaved differently on different machines (different speeds) • (-) The rope cannot be very stiff
Spring Model : Speed problem • Need for a time regulation algorithm • Why? • How? • After the first try, I had a slow and fast behavior… • Due to the GetTickCount() function • Use of the QueryPerformanceCounter()
Spring Model : Stiffness issue • The stiffness problem: • Due to the Euler’s approximation method
Spring Model : Extensions • The rope does not include any bending information: • Can be solved using interleaved springs (explained later, cf. Flag) • Stiffness problem: • Regarding the sources I found, the Runge-Kutta algorithm should solve the problem
The Runge-Kutta Algorithm Runge-Kutta 4 (55, 200000)
Spring Model : Flag simulation • A flag is just a patch of springs • Create n*m masses • Create (n-1)*(m-1) springs • Connect the springs to the masses • Possibility to add a wind effect
Spring Model : Flag simulation • Flag simulation
Flag simulation : Results • (+) The mesh reacts well to the wind and gravity • (-) The flag is harder to simulate because of the stiffness problem and the lack of bending factor
Flag simulation : Extensions • Can simulate a flag flexibility with interleaved springs… • …and add a universal repulsive force to every node • More complex and realistic simulation
High quality flag simulation • Demonstration
Collision Detection • Why? • How? • Dependencies: • A strong math library: vectors, matrices, plane-point collision, face-face collision… • Possibility to work on predefined meshes
On the way to the collision • Math library: • Matrices: • Overloading of arithmetic operators (+, -, *, +=…) • Overloading of input / output operators ([], <<) • Matrices functions : determinant, multiplications, inversions, transposition, identity • Matrix-related functions : rotate, scale, translate… • Vectors • Collision functions: PointToPlaneDistance, IsPointInPolygon…
Importing 3DS files • 3DS is a standard in the industry • I already had an importing class for 3DS files • .3DS files have several advantages: • Face defined clockwise, • Texture information, • Normals information, • And a lot more…
Into the collision • Brute force algorithm: CheckForCollisions(): MakePreselection(Scene, Collisions) For all objects in the Collision List if(thisobject collides with another one) Find the collision point Apply the physics on the objects, at that point • But this will never work!!!
Buggy Collision • Demonstration
Into the collision (2) • New algorithm: Do Do ComputePhysics(NextTimeChunk); CheckForCollisions(Scene, Collisions); if(MaxPenetrationInAnObject < Limit) Problem is solved; if(Problem NOT solved) NextTimeChunk = PreviousTime / 2; CancelTheComputations(); else ValidateTheComputations(); While(Problem is NOT solved); proceed to the next time chunk; While(TimeChunknotSimulated);
Collision improvement • We can extend the sphere collision test to a more general one. • Add a real collision and motion behavior (friction, rotation…) • The MakePreselection function can improve a lot the computation time
Improvements and trade-offs • The vast majority of the program use an aggressive MakePreselection algorithm to be able to deal with a lot of objects • Optimization without loss of information AABB = Axis Aligned Bounding Box OBB = Oriented Bounding Box 6-dop = Discrete Orientation Polytope Convex Hull
Example of an approximation algorithm Approximation: Based on some assumptions over “insignificant” constraints of objects (=has to look good enough) • The Opposing Face Geometry algorithm: • Algorithm in 8 steps, • The pro… • …And cons
Opposing Face Geometry algorithm • 1. Preselection: check collision between object A's bounding sphere and object B's bounding sphere. • 2. Findthe closest k faces of object A relative to object B.
O.F.G. algorithm • 3. Calculate the geometric center of the new selection and the bounding sphere radius. • 4. Find the closest k faces of object B relative to object A's new selection of k faces. • 5. Calculate the geometric center of object B's new selection of faces and their maximal bounding sphere radius.
The O.F.G. algorithm • 6. PreSelection: check collision between spheres to determine if there is even a chance for face collisions. • 7. Sort the two sets of faces by increasing distance • 8. Test the two sets of faces against each other, starting with the closest pairs of faces.
Pro / Cons of such this algorithm • (+) This is a lot faster. Runtime of O(k2) Where k is usually between 4 and 8 (k is a variable representing the number of faces we want to work on) Brute force approach would be O(n*m) n and m could be 1000 of faces • (-) Cannot really work on concave polygons This is TRUE for most of today’s engines
The discrete Time problem • Due to the intrinsic nature of the simulation : Time-discrete based • If the dt variation is too big, an object might be “teleported” through another one • Solution: Extrude the silhouette of the object. Test this polygon for collisions
Summary • Springs are the basis of a lot of models and can be used for powerful simulations (i.e. any kind of elastic models) • Collision detection needs a robust design and math support. There is a lot to do about optimization and trade-offs • Physics simulation is a vast field where a lot of techniques are to be discovered
Selection of References • “Computer Graphics with OpenGL”, third Edition by Hearn-Baker, Prentice Hall • Huge source of information for game programming: www.gamedev.net • Chris Hecker’s famous columns about physics: http://www.d6.com/users/checker/dynamics.htm#articles • Everything you need to know about geometry: http://astronomy.swin.edu.au/~pbourke/geometry/ • A lot about everything, from physics to light computations: http://freespace.virgin.net/hugo.elias/
Conclusion - Discussion • Questions? Need Explanations? What kind of extensions could we add to a physics simulator?
References • Collision detection • Advanced • Gamasutra - Features - "Advanced Collision Detection Techniques" [03.30.00] • Advanced Collision Detection Techniques • Advanced Collision Detection Techniques • Chris Hecker's Rigid Body Dynamics Information • DDJ • GameDev.net - Opposing Face Geometry • GameDev.net - Simple Bounding-Sphere Collision Detection • GameDev.net - Practical Collision Detection • GameDev.net - General Collision Detection for Games Using Ellipsoids • Collision Response: Bouncy, Trouncy, Fun • Gamasutra - Features - "Crashing into the New Year" [02.10.00] • Snooker simulation (+Formulaes) • Rotation computation • HyperPhysics • MODEL-BASED ANIMATION • SIGGRAPH - Collision Detection ('88) • AIWisdom.com - Game Articles & Research • Geometry • Cours de Mécanique - Index • The good-looking textured light-sourced bouncy fun smart and stretchy page • Deformation • GameDev.net - Real time deformation of solids, Part 1 • GameDev.net - Real time deformation of solids, Part 2 • Gamasutra - Features - "Exploring Spring Models" [10.05.01] • Cloths • Awesome paper on cloth simulation • ch06.pdf (application/pdf Object) • Rotational Motion