CSCE 590 E Spring 2007 Collision Detection By
CSCE 590 E Spring 2007 Collision Detection By Jijun Tang
Announcements n n n Final game demo will be held at 2: 00 pm, Tuesday, May 8 th Homework 4 will be given today Second presentation will be held on April 16 th and 18 th ¡ ¡ April 16 th: Space Banditos, Slackers, Psychosoft April 18 th: Project Gnosis, Cheeze Puffs!, Team Swampus
Needs to do n n n Two groups need to send me the first presentation after the class Some people didn’t send me the members in their small project. Please send the membership of the small project to stahlj@cse. sc. edu
Real-time Physics in Game at Runtime: n n n Enables the emergent behavior that provides player a richer game experience Potential to provide full cost savings to developer/publisher Difficult ¡ ¡ ¡ n May require significant upgrade of game engine May require significant update of asset creation pipelines May require special training for modelers, animators, and level designers Licensing an existing engine may significantly increase third party middleware costs
Engines n Commercial ¡ ¡ ¡ n Game Dynamics SDK (Havok. com) Renderware Physics (renderware. com) Novode. X SDK (novodex. com) Free ¡ ¡ ¡ Open Dynamic Engine (ODE) (ode. org) Tokamak Game Physics SDK (tokamakphysics. com) Newton Game Dynamics SDK (newtondynamics. com)
Particle Physics n What is a Particle? ¡ ¡ n A sphere of finite radius with a perfectly smooth, frictionless surface Experiences no rotational motion (or assume the sphere has no size) Particle Kinematics ¡ ¡ Defines the basic properties of particle motion Position, Velocity, Acceleration
Particle Position n Location of Particle in World Space ¡ SI Units: meters (m) ¡ Changes over time when object moves
Particle Velocity and Acceleration n Velocity (SI units: m/s) ¡ n First time derivative of position: Acceleration (SI units: m/s 2) ¡ ¡ First time derivative of velocity Second time derivative of position
Newton’s 2 nd Law of Motion n n Paraphrased – “An object’s change in velocity is proportional to an applied force” The Classic Equation: ¡ ¡ m = mass (SI units: kilograms, kg) F(t) = force (SI units: Newtons)
What is Physics Simulation? n The Cycle of Motion: ¡ ¡ ¡ n Force, F(t), causes acceleration Acceleration, a(t), causes a change in velocity Velocity, V(t) causes a change in position Physics Simulation: ¡ Solving variations of the above equations over time to emulate the cycle of motion
Concrete Example: Target Practice Projectile Launch Position, pinit Target
Target Practice n Choose Vinit to Hit a Stationary Target ¡ ¡ ¡ ptarget is the stationary target location We would like to choose the initial velocity, Vinit, required to hit the target at some future time, thit. Here is our equation of motion at time thit: Solution in general is a bit tedious to derive… Infinite number of solutions! Hint: Specify the magnitude of Vinit, solve for its direction
Example 1 Vinit = 25 m/s Value of Radicand of tanf equation: 969. 31 Launch angle f: 19. 4 deg or 70. 6 deg
Finite Difference Methods n The Explicit Euler Integrator: n Properties of object are stored in a state vector, S Use the above integrator equation to incrementally update S over time as game progresses Must keep track of prior value of S in order to compute the new For Explicit Euler, one choice of state and state derivative for particle: n n n
Finite Difference Methods n The Verlet Integrator: n Must store state at two prior time steps, S(t) and S(t-Dt) Uses second derivative of state instead of the first Valid for constant time step only (as shown above) For Verlet, choice of state and state derivative for a particle: n n n
Generalized Rigid Bodies n Key Differences from Particles ¡ ¡ ¡ Not necessarily spherical in shape Position, p, represents object’s center-of-mass location Surface may not be perfectly smooth n ¡ Friction forces may be present Experience rotational motion in addition to translational (position only) motion
Additional forces n n n Linear Spring Viscous Damping Aerodynamic Drag Friction …
Linear Springs
Viscous Damping
Aerodynamic Drag S: projected front area CD: drag coefficient
Friction
Collision Detection and Resolution
What is Collision Detection n n A fundamental problem in computer games, computer animation, physically -based modeling, geometric modeling, and robotics. Including algorithms: ¡ ¡ To check for collision, i. e. intersection, of two given objects To calculate trajectories, impact times and impact points in a physical simulation.
Collision Detection n Complicated for two reasons ¡ ¡ n Geometry is typically very complex, potentially requiring expensive testing Naïve solution is O(n 2) time complexity, since every object can potentially collide with every other object Two basic techniques ¡ ¡ Overlap testing: Detects whether a collision has already occurred Intersection testing: Predicts whether a collision will occur in the future
Overlap Testing (a posteriori) n n Overlap testing: Detects whether a collision has already occurred, sometime is referred as a posteriori Facts ¡ ¡ n Most common technique used in games Exhibits more error than intersection testing Concept ¡ ¡ For every (small) simulation step, test every pair of objects to see if they overlap Easy for simple volumes like spheres, harder for polygonal models
Overlap Testing Results n Useful results of detected collision ¡ ¡ ¡ n Pairs of objects will have collision Time of collision to take place Collision normal vector Collision time calculated by moving object back in time until right before collision ¡ Bisection is an effective technique
Bisect Testing: collision detected
Bisect Testing: Iteration I
Bisect Testing: Iteration II
Bisect Testing: Iteration III
Bisect Testing: Iteration IV
Bisect Testing: Iteration V Time right before the collision
Overlap Testing: Limitations n Fails with objects that move too fast (compared to the object) ¡ ¡ Thin glass vs. bulltes Unlikely to catch time slice during overlap
Solution for This Limitation n n Speed of the fastest object multiplies the time step should be smaller than the smallest objects in the scene Possible solutions ¡ Design constraint on speed of objects n ¡ Hard to apply without affecting the play Reduce simulation step size n Too expensive
Intersection Testing (a priori) n n Predict future collisions When predicted: ¡ ¡ ¡ Move simulation to time of collision Resolve collision Simulate remaining time step
Intersection Testing: Swept Geometry n n Extrude geometry in direction of movement Swept sphere turns into a “capsule” shape
Intersection Testing: Sphere-Sphere Collision d
Special Cases n No collision: n B 2 = 0: both objects are stationary, or they are traveling at parallel When will collision occur? n
Intersection Testing: Sphere-Sphere Collision n Smallest distance ever separating two spheres: n If there is a collision
Intersection Testing: Limitations n Issue with networked games ¡ ¡ n Future predictions rely on exact state of world at present time Due to packet latency, current state not always coherent Assumes constant velocity and zero acceleration over simulation step ¡ Has implications for physics model and choice of integrator
Dealing with Complexity Two issues 1. Complex geometry must be simplified 2. Reduce number of object pair tests
Simplified Geometry n Approximate complex objects with simpler geometry, like this ellipsoid or bounding boxes
Minkowski Sum n By taking the Minkowski Sum of two complex volumes and creating a new volume, overlap can be found by testing if a single point is within the new volume
Minkowski Sum
Using Minkowski Sum
Bounding Volumes n Bounding volume is a simple geometric shape ¡ ¡ n Completely encapsulates object If no collision with bounding volume, no more testing is required Common bounding volumes ¡ ¡ Sphere Box
Box Bounding Volumes
More Examples
Using Bounding Box in Game n Complex objects can have multiple bounding boxes ¡ ¡ n Human object can have one big bounding box for the whole body Human object can have one bounding box per limb, head, etc Bounding box can be hierarchical: ¡ ¡ Test the big first if possible collision, test the smaller ones
Reduce Number of Detections O(n) Time Complexity can be achieved. One solution is to partition space
Achieving O(n) Time Complexity Another solution is the plane sweep algorithm Requires (re-)sorting in x (y) coordinate
Terrain Collision Detection: Height Field Landscape
Locate Triangle on Height Field Essentially a 2 D problem
Locate Point on Triangle n n Plane equation: A, B, C are the x, y, z components of the triangle plane’s normal vector Where with one of the triangles vertices being Giving:
Terrain Collision Detection: Locate Point on Triangle n The normal can be constructed by taking the cross product of two sides: n Solve for y and insert the x and z components of Q, giving the final equation for point within triangle:
Treating Nonuniform Polygon Mesh n n n Hard to detect the triangle where the point lies in Using Triangulated Irregular Networks (TINs) Barycentric Coordinates
Barycentric Coordinates n n Even with complex data structure, we still have to test each triangle (in a sub region) to see if the point lies in it Using Barycentric Coordinates to do the test
Terrain Collision Detection: Locate Point on Triangle n Calculate barycentric coordinates for point Q in a triangle’s plane n If any of the weights (w 0, w 1, w 2) are negative, then the point Q does not lie in the triangle
Collision Resolution: Examples n Two billiard balls strike ¡ ¡ ¡ n Rocket slams into wall ¡ ¡ ¡ n Calculate ball positions at time of impact Impart new velocities on balls Play “clinking” sound effect Rocket disappears Explosion spawned and explosion sound effect Wall charred and area damage inflicted on nearby characters Character walks through wall ¡ ¡ Magical sound effect triggered No trajectories or velocities affected
Collision Resolution: Parts n Resolution has three parts 1. Prologue 2. Collision 3. Epilogue
Prologue n n n Collision known to have occurred Check if collision should be ignored Other events might be triggered ¡ ¡ Sound effects Send collision notification messages
Collision n n Place objects at point of impact Assign new velocities ¡ ¡ Using physics or Using some other decision logic
Epilogue n n Propagate post-collision effects Possible effects ¡ ¡ ¡ n Destroy one or both objects Play sound effect Inflict damage Many effects can be done either in the prologue or epilogue
Collision Resolution: Resolving Overlap Testing 1. Extract collision normal 2. Extract penetration depth 3. Move the two objects apart 4. Compute new velocities
Extract Collision Normal n n Find position of objects before impact Use two closest points to construct the collision normal vector
Extract Collision Normal for Sphere Collision n Sphere collision normal vector ¡ Difference between centers at point of collision
Depth n Gilbert-Johnson-keerthi (GJK) bisection to extract the depth of convex objects Minkowski supporting lines
Collision Resolution: Resolving Intersection Testing n Simpler than resolving overlap testing ¡ n No need to find penetration depth or move objects apart Simply 1. Extract collision normal 2. Compute new velocities
Collision Response n Why? ¡ ¡ n Performed to keep objects from interpenetrating To ensure behavior similar to real-world objects Two Basic Approaches ¡ ¡ Approach 1: Instantaneous change of velocity at time of collision Approach 2: Gradual change of velocity and position over time, following collision
Approach 1 n Approach 1: Instantaneous change of velocity at time of collision ¡ Benefits: n n ¡ Visually the objects never interpenetrate Result is generated via closed-form equations, and is perfectly stable Difficulties: n n Precise detection of time and location of collision can be prohibitively expensive (frame rate killer) Logic to manage state is complex
Approaches 2 n Approach 2: Gradual change of velocity and position over time, following collision ¡ Benefits n n n ¡ Does not require precise detection of time and location of collision State management is easy Potential to be more realistic, if meshes are adjusted to deform according to predicted interpenetration Difficulties n n Object interpenetration is likely, and parameters must be tweaked to manage this Simulation can be subject to numerical instabilities, often requiring the use of implicit finite difference methods
Final Comments n Instantaneous Collision Response ¡ Classical approach: Impulse-momentum equations n n See text for full details Gradual Collision Response ¡ Classical approach: Penalty force methods n n Resolve interpenetration over the course of a few integration steps Penalty forces can wreak havoc on numerical integration ¡ n Implicit finite difference equations can handle it ¡ ¡ Instabilities galore But more difficult to code Geometric approach: Ignore physical response equations n Enforce purely geometric constraints once interpenetration has occurred
Final Comments n Simple Games ¡ ¡ ¡ n Closed-form particle equations may be all you need Numerical particle simulation adds flexibility without much coding effort Collision detection is probably the most difficult part of this Generalized Rigid Body Simulation ¡ ¡ Includes rotational effects and interesting (nonconstant) forces See text for details on how to get started
Final Comments n Full-Up Simulation ¡ ¡ The text and this presentation just barely touch the surface Additional considerations n n n n n Multiple simultaneous collision points Articulating rigid body chains, with joints Friction, rolling friction, friction during collision Mechanically applied forces (motors, etc. ) Resting contact/stacking Breakable objects Soft bodies Smoke, clouds, and other gases Water, oil, and other fluids
Homework 4 n n Page 390 problem 1 and 5 Handwriting or printing using Word/Latex ¡ ¡ n n Turn in before class No need to upload to dropbox 5 points total (2. 5 each) Due on this Wednesday
- Slides: 75