collisiondetecon’ -...
TRANSCRIPT
CS 2501
Collision Detec,on
CS 2501 – Intro to Game Programming and Design
CS 2501
Back to Physics • Remember what we have here:
– Objects have posi@on, velocity, accelera@on – In a physics rou@ne
• Collisions are determined • Forces collected • Numeric integra@on performed • Constraints resolved • Frame update and do the whole thing again
2
CS 2501
Back to Physics • If there are no collisions, this is easy • Pick your forces:
– Gravity – Air resistance – “The Force”
• Figure out how they affect accelera@on • Do the math • Update the frame
3
CS 2501
But With Collisions… • Consider force • Force directly changes accelera@on • Bigger mass => Bigger force • Force puts things in mo@on, but also can bring things to a halt
4
CS 2501
Momentum • Objects stay in mo@on due to momentum • Momentum = mass * velocity • If we do some fancy math:
– F = ma = m * (Δv / Δt) – FΔt = mΔv
• FΔt is called an impulse • An impulse is a change in momentum
5
CS 2501
Conserva@on of Momentum • When objects collide, momentum changes
– … well, the magnitude is the same, it just goes in another direc@on
• That’s the Law of Conserva@on of Momentum • At the point of impact, ignoring other forces, the total momentum of all objects involved does not change
6
CS 2501
Conserva@on of Momentum • Whatever momentum one object loses, the other gains
• This is a transfer of kine@c energy • How objects react to the kine@c energy is the object’s elas@city
• The coefficient of res@tu@on defines how velocity changes before and aber impact based on elas@city
7
CS 2501
Coefficient of Res@tu@on • If the coefficient is 0.0, then the object is totally inelas@c and it absorbed the en@re hit
• If the coefficient is 1.0, then the objects is totally elas@c and all momentum will s@ll be evident
• The sum of the kine@c energy will be the same
8
CS 2501
Okay… Great! • Math and physics are great and all… • … but how do you know if two things collided?
9
CS 2501
Who’s Colliding? • Okay, how would you do it?
10
CS 2501
Who’s Colliding? • Compare everything • Check only around the player • Check only in a par@cular quadrant • Check only around moving objects (i.e. not atRest())
• Remember: “perfect is the enemy of good enough”
• Don’t go for perfec@on! Go for “looks right”
11
CS 2501
How about the actual collision? • Overlap tes@ng is probably most common / easiest method
• Does have a bit of error • For each Δt, check to see if anything is overlapping (using some of the op@miza@ons from the previous slide)
12
CS 2501
Overlap Tes@ng
13
B B
t1
t0.375
t0.25
B
t0
Iteration 1Forward 1/2
Iteration 2Backward 1/4
Iteration 3Forward 1/8
Iteration 4Forward 1/16
Iteration 5Backward 1/32
Initial OverlapTest
t0.5t0.4375 t0.40625
BB B
A
A
A
A A A
CS 2501
Problems With Overlaps • What if your Δt is too big?
– Well, you can fly right through an object before any collision is actually registered
• Kinda hard to do with complex shapes – Picture any game sprite – None of them are actually simple geometric shapes
14
CS 2501
Overlap Tes@ng • Fails with objects that move too fast
– Unlikely to catch @me slice during overlap
• Possible solu@ons – Design constraint on speed of objects – Reduce simula@on step size
15
t0t-1 t1 t2bullet
window
CS 2501 16
Intersec@on Tes@ng • Predict future collisions • When predicted:
– Move simula@on to @me of collision – Resolve collision – Simulate remaining @me step
CS 2501 17
Swept Geometry • Extrude geometry in direc@on of movement • Swept sphere turns into a “capsule” shape
t0
t1
CS 2501 18
Limita@ons • Issue with networked games
– Future predic@ons rely on exact state of world at present @me
– Due to packet latency, current state not always coherent
• Assumes constant velocity and zero accelera@on over simula@on step – Has implica@ons for physics model and choice of integrator
CS 2501
Introducing the Hit Box! • All of our normal characters in early games were rectangles (or squares)
• Sprite sheets / @le sheets were easy to code and easy to read from
• Thus, characters were broken up into easy-‐to-‐render (and check) chunks
• More complex modern games have many more interes@ng hit boxes (oben a set of hit boxes)
19
CS 2501
MDA of Hit Boxes • The mechanic of the hit box is essen@al to having a game that runs at a reasonable speed
• How can the player exploit the hit box? • What is the end aesthe@c result? • How can we balance between hit box accuracy and game play?
20
CS 2501
Hit Boxes • Go for “good enough” • Efficiency hacks/cheats
– Fewer tests: Exploit spa@al coherence • Use bounding boxes/spheres • Hierarchies of bounding boxes/spheres
21
CS 2501
Bounding Boxes • Axis-‐aligned vs. Object-‐aligned
• Axis-‐aligned BBox change as object moves • Approximate by rota@ng BBox
CS 2501
Collision Spheres • Another op@on is to put everything in a “bubble”
• Think Super Monkey Ball gone wild • Sphere collision is cheap to detect!
23
CS 2501
How Many Hit Boxes? • In the worst case (with complex objects) this is really a hard problem! O(n^2)
• For each object i containing polygons p – Test for intersec@on with object j with polygons q
• For polyhedral objects, test if object i penetrates surface of j – Test if ver@ces of i straddle polygon q of j
24
CS 2501
Speed Up • To go faster
– Sort on one dimension • Bucket sort (i.e. discre@ze in 1 dimension)
– Exploit temporal coherence • Maintain a list of object pairs that are close to each other
• Use current speeds to es@mate likely collisions
– Use cheaper tests
25
CS 2501
Cheaper Distance Tests
• Cheaper distance calcula@on: – Compare against
• Approxima@on for comparison: – Manhanan distance
26
))()(( 221
221 yyxxsqrtd −+−=
d 2
d 2 = (x1 − x2 )2 + (y1 − y2 )
2
CS 2501
Sprite Collision Detec@on
27
CS 2501 28
Achieving O(n) Time Complexity One solu@on is to par@@on space
CS 2501 29
Achieving O(n) Time Complexity Another solu@on is the plane sweep algorithm
C
B
R
A
x
y
A0 A1 R0 B0 R1C0 C1B1
B0
B1A1
A0
R1
R0C1
C0
CS 2501 30
Collision Resolu@on
• Two billiard balls strike – Calculate ball posi@ons at @me of impact – Impart new veloci@es on balls – Play “clinking” sound effect
• Rocket slams into wall – 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 veloci@es affected
CS 2501 31
Collision Resolu@on • Resolu@on has three parts
1. Prologue 2. Collision 3. Epilogue
CS 2501 32
Prologue • Collision known to have occurred • Check if collision should be ignored • Other events might be triggered
– Sound effects – Send collision no@fica@on messages
CS 2501 33
Collision • Place objects at point of impact • Assign new veloci@es
– Using physics or – Using some other decision logic
CS 2501 34
Epilogue • Propagate post-‐collision effects • Possible effects
– Destroy one or both objects – Play sound effect – Inflict damage
• Many effects can be done either in the prologue or epilogue