physics and collision detection - alan hazelden · physics and collision detection alan hazelden ...
TRANSCRIPT
![Page 2: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/2.jpg)
Required knowledge
Basic physical concepts Velocity Acceleration Mass Forces
Basic geometrical concepts Vectors Matrices
![Page 3: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/3.jpg)
Definitions
ConcaveConvex
![Page 4: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/4.jpg)
Definitions
Impulse An instantaneous force
Moment Component of a force that affects rotation
Rotation ≠ Orientation Rotation = Angular velocity
![Page 5: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/5.jpg)
Games and physics
How do games use physics?
![Page 6: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/6.jpg)
Games and physics
How do games use physics? Preventing interpenetration of objects
One good approach: Detect that objects are colliding Do something about it
![Page 7: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/7.jpg)
Collision detection
Massively important Needs to be efficient Needs to be accurate
But only to a certain extent
![Page 8: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/8.jpg)
Simplification
We don't perform collision detection on the rendering geometry.
![Page 9: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/9.jpg)
2D primitives
Circle Line Rectangle Capsule Convex polygon
![Page 10: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/10.jpg)
3D primitives
Sphere Line Cuboid (aka box) Cylinder Capsule Convex polyhedra
![Page 11: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/11.jpg)
Containment
Does this shape contain this point?
Circles: test distance from centre Axis-aligned rectangles: check x/y coordinates Oriented rectangle: first convert point to local
coordinate system
![Page 12: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/12.jpg)
Containment within a polygon
Basic idea: draw an infinite line from the point in any direction Count the number of times it crosses an edge Two different ways of counting
Give different results for some polygons
![Page 13: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/13.jpg)
Convex polygons
Can be represented as the intersection of a set of half-spaces
To check containment, check against each half-space in turn If outside any, then outside the convex polygon If inside all, then inside the convex polygon
![Page 14: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/14.jpg)
Collision detection
Are these shapes overlapping? Easiest with circles/spheres:
Colliding if r1 + r
2 > d
Note: Equivalent to containment of a point within a circle of radius r
1 + r
2
d
r1
r2
d
r1
r2
![Page 15: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/15.jpg)
Collision of moving circles
We can represent this as the intersection of a line and a circle
We can even get the time of collision! Note: usually we don't need
this
![Page 16: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/16.jpg)
Axis-aligned boxes
![Page 17: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/17.jpg)
Separating Axis Theorem
If separated along some axis, not colliding If overlapping along all possible axes, colliding
![Page 18: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/18.jpg)
Separating Axis Theorem
Key observation: can enumerate the possible separating axes
For axis-aligned 2D boxes, only two (x and y) For oriented 2D boxes, only four (two per box) What about more complicated shapes?
![Page 19: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/19.jpg)
Collision culling
Brute force collision testing would take O(n2) comparisons
We can rule some collisions out very quickly Bounding boxes Exploiting temporal coherence
If we have a separating axis for two objects, it is likely to still be a separating axis in the next frame
![Page 20: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/20.jpg)
Broad-phase collision detection
Exploits spatial coherence Whatever that means
Regular grid Quadtree/Octree BSP tree (binary space partitioning) Hierarchy of bounding shapes
![Page 21: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/21.jpg)
Collision Resolution
Once we've found a collision, what do we do?
Information needed: Contact normal Contact point Penetration distance
This is all found by the Separating Axis test!
![Page 22: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/22.jpg)
Collisions without rotation
Impulse applied at the instant of collision In the direction of the collision normal
Change in velocity determined by: Coefficient of restitution Conservation of momentum
Better way: calculate magnitude of the impulse Only use coefficient of restitution Conservation of momentum automatically satisfied
by balanced impulses
![Page 23: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/23.jpg)
Collisions with rotation
Impulse is applied at the point of contact This will affect rotation
Also: rotation affects approach speed We want the approach speed of the contact points
How does an impulse affect rotation? Moment of inertia
![Page 24: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/24.jpg)
Friction
Perpendicular to the contact normal Opposes motion F=R
Friction
Con
tact
![Page 25: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/25.jpg)
Friction
Friction is a force, not an impulse But I treat it like one anyway Applied at the end of the frame, after contact force
![Page 26: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/26.jpg)
Penetration
Overlapping objects If we resolved contact forces at the instant of
collision, this shouldn't happen But we only ever apply these at the end of a frame
Need to nudge objects so they stop colliding But there could be many objects in a group One approach: keep collisions in a list
Sort list by penetration distance
![Page 27: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/27.jpg)
General overview
Every frame: All objects are moved simultaneously The collision detection system is run
Every pair of colliding objects is detected and stored The collision resolver is run
Velocities are updated Penetration is removed
All objects are drawn at their new positions
![Page 28: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/28.jpg)
Movement of objects
First-order Euler integration x += v * t;v += a * t;
Not good
Second-order Euler integration Based on SUVAT equations x += v * t + 0.5 * a * t * t;v += a * t;
Works, assuming constant acceleration Unfortunately, springs and other constraints don't fit this
![Page 29: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/29.jpg)
References
Real-Time Collision DetectionChrister Ericson
![Page 30: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/30.jpg)
References
Game PhysicsDavid Eberly
Game Physics Engine DevelopmentIan Millington
![Page 31: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/31.jpg)
Online resources
Erin Catto http://www.gphysics.com/
Glenn Fiedler http://www.gaffer.org/game-physics
Wikipedia
![Page 32: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/32.jpg)
Existing 2D physics engines
Box2D (C++) http://www.box2d.org/
Chipmunk (C) http://wiki.slembcke.net/main/published/Chipmunk
Farseer (XNA) http://www.codeplex.com/FarseerPhysics
My third year project (C++ and WGD-Lib) http://www.draknek.org/physics/
![Page 33: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/33.jpg)
Existing 3D physics engines
Bullet http://www.bulletphysics.com/
Open Dynamics Engine http://www.ode.org/
Havok http://www.havok.com/tryhavok
![Page 34: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden Required knowledge ... Based on SUVAT equations](https://reader034.vdocuments.mx/reader034/viewer/2022050814/5acc4b717f8b9ab10a8c5050/html5/thumbnails/34.jpg)
Questions?