physics and collision detection - alan hazelden · physics and collision detection alan hazelden ...

34
Physics and Collision Detection Alan Hazelden http://www.draknek.org/

Upload: lecong

Post on 10-Apr-2018

237 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden  Required knowledge ... Based on SUVAT equations

Physics and Collision Detection

Alan Hazelden

http://www.draknek.org/

Page 2: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden  Required knowledge ... Based on SUVAT equations

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

Definitions

ConcaveConvex

Page 4: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden  Required knowledge ... Based on SUVAT equations

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

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

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

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

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

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

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

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

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

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

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

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

Axis-aligned boxes

Page 17: Physics and Collision Detection - Alan Hazelden · Physics and Collision Detection Alan Hazelden  Required knowledge ... Based on SUVAT equations

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Questions?