480 likes | 596 Vues
This lecture explores essential physics concepts for game programming, focusing on efficient collision detection methods and the importance of simplifications in simulations. Presented by Wesley Kerr, it highlights key strategies such as when to cheat in simulations, the use of convex shapes, and various primitive collision detection techniques like Axis-Aligned Bounding Boxes (AABB) and Oriented Bounding Boxes (OBB). The discussion also addresses the challenges of tunneling and provides insight into maintaining real-time performance while ensuring realistic interactions and predictable results in gaming environments.
E N D
Lecture 17 – Physics 4 Wesley Kerr CS 490/590
Lecture Acknowledgements • Physics for Game Programmers: Problem Overview • GDC 2008 Physics Tutorial • Squirrel Eiserloh • Physics for Game Programmers: Reframing the Problem • GDC 2008 Physics Tutorial • Squirrel Eiserloh • Both Available • http://www.essentialmath.com/tutorial.html
Types of Problems • Knowing when to cheat • Simplifying things • Giving shape to things • Moving things around • Discussed on Monday • Simulation Baggage • Detecting (and resolving) collisions • Sustained interactions • Dealing with the impossible • Making it fast enough
Knowing When to Cheat • “Real” physics is prohibitively expensive… • … so we cheat • Keeps the computation running in real time • Cheats have to be carefully selected • Cannot break in jarring noticeable ways • Cannot result in unrecoverable physics • The challenge is really knowing how and when to cheat
Knowing When to Cheat • Ask the following questions: • “Will the player notice?” • “Will the player care?” • “Will the results be predictable?” • “Are we at least cheating in a consistent way?” • “Will the simulation break?” • If the simulation breaks, they will notice and they will care • Some crimes are greater than others
Simplifying Things • Simplified bodies • Collisions require geometry • Moving point masses is not enough • Must know where objects meet • Typically use convex shapes • Lines remain inside shape • Mathematically the easiest shapes to compute with • Assumed by most algorithms • Historical research performed on convex shapes • What do you do if the shape is not convex? • Break it up into several primitives
Primitives • Axis Aligned Bounding-Box (AABB) • Intersection is very cheap • Project rectangle onto axes • Two intervals per box • Check both intervals overlap • Poor fit for most objects • Super Mario Bros. – works great • Diagonal boxes – not so great • False positives are likely
Primitives • Axis Aligned Bounding-Box (AABB) • Intersection is very cheap • Project rectangle onto axes • Two intervals per box • Check both intervals overlap • Poor fit for most objects • Super Mario Bros. – works great • Diagonal boxes – not so great • False positives are likely
Primitives • Axis Aligned Bounding-Box (AABB) • Oriented Bounding Box (OBB) • Tighter fit than AABB • Intersection more expensive • Must check 4 edges for overlap
Primitives • Axis Aligned Bounding-Box (AABB) • Oriented Bounding Box (OBB) • Discrete Oriented Polytope (DOP) • Captures almost all convex shapes • Intersection is more expensive • O(n2), n = number of edges
Primitives • Axis Aligned Bounding-Box (AABB) • Oriented Bounding Box (OBB) • Discrete Oriented Polytope (DOP) • Capsule • An OBB with rounded ends • Intersection cost is roughly the same • Efficient for swept shapes (circles)
Primitives • Axis Aligned Bounding-Box (AABB) • Oriented Bounding Box (OBB) • Discrete Oriented Polytope (DOP) • Capsule • Circles/Disks • Not cheaper than AABB • Poor fit for approximating anything other than a circle
Collision Detection • Question: • Do objects A and B intersect
Collision Detection • Question: • Do objects A and B intersect • Challenge: • Can be in motion
Collision Detection • Question: • Do objects A and B intersect • Challenge: • Can be in motion • Bigger Challenge: • Need to know when they collided. • Realistic physics needs the exact point of collision • Necessary for contact resolution
Tunneling • Small objects tunnel more easily
Tunneling • Possible solutions • Minimum size requirement? • Inadequate; fast objects can still tunnel
Tunneling • Possible solutions • Minimum size requirement? • Inadequate; fast objects can still tunnel • Maximum speed limit? • Inadequate; speed limit is a function of object size, so small and fast objects (bullets) wouldn’t be allowed • Smaller time step? • Helpful but inadequate; essentially the same as a speed limit
Tunneling • Why this is important! • Things can fall through the world • Bullets pass through people or walls • Players getting places they shouldn’t • Players missing trigger boundaries • Tunneling is a false negative • So what can we do about it?
Movement Bounds • Disc / Sphere
Movement Bounds • Disc / Sphere • AABB
Movement Bounds • Disc / Sphere • AABB • OBB
Movement Bounds • Question: • Could A and B have collided during the frame? • Better than our earlier question because it handles tunneling, but…
Movement Bounds • Question: • Could A and B have collided during the frame? • Better than our earlier question because it handles tunneling, but… • … even if the answer is “yes”, we still don’t know for sure • false positives
Movement Bounds • Conclusion • Good: they prevent tunneling • Bad: they produce false positives • Good: cheap, effective early rejection test
Swept Shapes • Swept disc: capsule
Swept Shapes • Swept disc: capsule • Swept AABB: polygon
Swept Shapes • Swept disc: capsule • Swept AABB: polygon • Swept triangle: polygon
Swept Shapes • Swept disc: capsule • Swept AABB: polygon • Swept triangle: polygon • Swept polytope: polygon
Swept Shapes • Better fit than movement bounds
Swept Shapes • Better fit than movement bounds • No false negatives (tunneling)
Swept Shapes • Better fit than movement bounds • No false negatives (tunneling) • No false positives either!
Swept Shapes • Better fit than movement bounds • No false negatives (tunneling) • No false positives either! • Nope! False positives still exist
Swept Shapes • Better fit than movement bounds • No false negatives (tunneling) • No false positives either! • Nope! False positives still exist
Collision Detection • Occurs in three phases • Broad Phase • Determine all pairs of objects that potentially collide • Sweep and Prune or Spatial Hashing • Mid phase • Determine potentially colliding primitives • Movement Bounds using AABBs • Narrow phase • Determine exact contact between two shapes • The Gilbert-Johnson-Keerthi (GJK) algorithm
Collision Resolution • What happens once we finally find the collision? • Two types of collisions elastic and inelastic • Inelastic collisions • No energy preserved in collision • Ensure objects do not overlap • Extremely easy to implement • Elastic collisions • 100% energy preserved • More complicated to implement
Simple Case: Circles • Simple collisions • Single point of contact • Impact coordinates • Point of contact is origin
Simple Case: Circles • Simple collisions • Single point of contact • Impact coordinates • Point of contact is origin
Simple Case: Circles • Simple collisions • Single point of contact • Impact coordinates • Point of contact is origin • Perpendicular component • Parallel component
Simple Case: Circles • Simple collisions • Single point of contact • Impact coordinates • Point of contact is origin • Perpendicular component • Parallel component • Change in motion happens along the perpendicular axis
Simple Case: Circles • Simple collisions • Single point of contact • Impact coordinates • Point of contact is origin • Perpendicular component • Parallel component • Exchange in energy happens along the perpendicular axis
Complex Case • With complex shapes, the point of contact is more complex • Could be a single point • Could be an edge • Solution: • Break object into several points • Connect the points by constraints • Apply forces to each of the points • Transfer forces to other points while obeying constraints • Requires constraint solver
Complex Case • With complex shapes, the point of contact is more complex • Could be a single point • Could be an edge • Solution: • Break object into several points • Connect the points by constraints • Apply forces to each of the points • Transfer forces to other points while obeying constraints • Requires constraint solver
Summary • Accurate physics is challenging • We didn’t even cover rotational motion! • Accurate collision detection is challenging • We’ve seen the problems that a physics engine must solve • Tips for moving ahead • Structure game to make collision detection easy • Use circles, AABBs, grids, etc. • Kinematic dynamics is straightforward and may produce “good enough” results • Use a physics engine rather than writing your own.