![Page 1: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/1.jpg)
CS 4730
Collision Detection
CS 4730 – Computer Game Design
![Page 2: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/2.jpg)
CS 47302
Back to Physics• Remember what we have here:
– Objects have position, velocity, acceleration– In a physics routine
• Collisions are determined• Forces collected• Numeric integration performed• Constraints resolved• Frame update and do the whole thing again
![Page 3: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/3.jpg)
CS 47303
Back to Physics• If there are no collisions, this is easy• Pick your forces:
– Gravity– Air resistance– “The Force”
• Figure out how they affect acceleration• Do the math• Update the frame
![Page 4: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/4.jpg)
CS 47304
But With Collisions…• Consider force• Force directly changes acceleration• Bigger mass => Bigger force• Force puts things in motion, but also can bring
things to a halt
![Page 5: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/5.jpg)
CS 47305
Momentum• Objects stay in motion 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
![Page 6: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/6.jpg)
CS 47306
Conservation of Momentum• When objects collide, momentum changes
– … well, the magnitude is the same, it just goes in another direction
• That’s the Law of Conservation of Momentum• At the point of impact, ignoring other forces,
the total momentum of all objects involved does not change
![Page 7: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/7.jpg)
CS 47307
Conservation of Momentum• Whatever momentum one object loses, the
other gains• This is a transfer of kinetic energy• How objects react to the kinetic energy is the
object’s elasticity• The coefficient of restitution defines how
velocity changes before and after impact based on elasticity
![Page 8: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/8.jpg)
CS 47308
Coefficient of Restitution• If the coefficient is 0.0, then the object is totally
inelastic and it absorbed the entire hit• If the coefficient is 1.0, then the objects is
totally elastic and all momentum will still be evident
• The sum of the kinetic energy will be the same
![Page 9: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/9.jpg)
CS 47309
Putting It All Together• So our final formula is:• v1f =
((e + 1)*m2*v2 + v1*(m1 – e*m2)) / (m1 + m2)
• v2f = ((e + 1)*m1*v1 + v2*(m1 – e*m2)) / (m1 + m2)
![Page 10: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/10.jpg)
CS 473010
Okay… Great!• Math and physics are great and all…• … but how do you know if two things collided?
![Page 11: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/11.jpg)
CS 473011
Who’s Colliding?• Okay, how would you do it?
![Page 12: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/12.jpg)
CS 473012
Who’s Colliding?• Compare everything• Check only around the player• Check only in a particular quadrant• Check only around moving objects (i.e. not
atRest())• Remember: “perfect is the enemy of good
enough”• Don’t go for perfection! Go for “looks right”
![Page 13: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/13.jpg)
CS 473013
How about the actual collision?• Overlap testing 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 optimizations from the previous slide)
![Page 14: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/14.jpg)
CS 473014
Overlap Testing
B B
t1
t0.375
t0.25
B
t0
I te r a t io n 1F o r w ar d 1 /2
I te r a t io n 2Bac k w ar d 1 /4
I te r a t io n 3F o r w ar d 1 /8
I te r a t io n 4F o r w ar d 1 /1 6
I ter a t io n 5Bac k w ar d 1 /3 2
I n it ia l O v er lapT es t
t0.5t0.4375 t0.40625
BB B
A
A
A
AA A
![Page 15: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/15.jpg)
CS 473015
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
![Page 16: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/16.jpg)
CS 473016
Overlap Testing• Fails with objects that move too fast
– Unlikely to catch time slice during overlap• Possible solutions
– Design constraint on speed of objects– Reduce simulation step size
t0t -1 t1 t2
b u lle t
w in d o w
![Page 17: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/17.jpg)
CS 473017
Intersection Testing• Predict future collisions• When predicted:
– Move simulation to time of collision– Resolve collision– Simulate remaining time step
![Page 18: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/18.jpg)
CS 473018
Swept Geometry• Extrude geometry in direction of movement• Swept sphere turns into a “capsule” shape
t0
t1
![Page 19: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/19.jpg)
CS 473019
Limitations• Issue with networked games
– 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
![Page 20: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/20.jpg)
CS 473020
Introducing the Hit Box!• All of our normal characters in early games
were rectangles (or squares)• Sprite sheets / tile 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
interesting hit boxes (often a set of hit boxes)
![Page 21: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/21.jpg)
CS 473021
MDA of Hit Boxes• The mechanic of the hit box is essential to
having a game that runs at a reasonable speed• How can the player exploit the hit box?• What is the end aesthetic result?• How can we balance between hit box accuracy
and game play?
![Page 22: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/22.jpg)
CS 473022
Hit Boxes• Go for “good enough”• Efficiency hacks/cheats
– Fewer tests: Exploit spatial coherence• Use bounding boxes/spheres• Hierarchies of bounding boxes/spheres
![Page 23: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/23.jpg)
CS 4730
Bounding Boxes• Axis-aligned vs. Object-aligned
• Axis-aligned BBox change as object moves• Approximate by rotating BBox
![Page 24: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/24.jpg)
CS 473024
Collision Spheres• Another option is to put everything in a
“bubble”• Think Super Monkey Ball gone wild• Sphere collision is cheap to detect!
![Page 25: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/25.jpg)
CS 473025
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 intersection with object j with polygons q• For polyhedral objects, test if object i
penetrates surface of j– Test if vertices of i straddle polygon q of j
![Page 26: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/26.jpg)
CS 473026
Speed Up• To go faster
– Sort on one dimension• Bucket sort (i.e. discretize in 1 dimension)
– Exploit temporal coherence• Maintain a list of object pairs that are close to each
other• Use current speeds to estimate likely collisions
– Use cheaper tests
![Page 27: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/27.jpg)
CS 473027
Cheaper Distance Tests
• Cheaper distance calculation:– Compare against
• Approximation for comparison:
– Manhattan distance
![Page 28: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/28.jpg)
CS 473028
Sprite Collision Detection
![Page 29: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/29.jpg)
CS 473029
Achieving O(n) Time Complexity
One solution is to partition space
![Page 30: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/30.jpg)
CS 473030
Achieving O(n) Time Complexity• The box collides with the
level 1 node – but there are no objects in level 1
• The box collides with two level 2 nodes – but there are no objects in them either. However, their child nodes need to be checked now.
![Page 31: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/31.jpg)
CS 473031
Achieving O(n) Time Complexity• The box collides with four
level 3 nodes, and there is one object in them, which is added to the return list. Note that there are no level 3 nodes in the top-right level 2 node, so it is not queried any further.
![Page 32: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/32.jpg)
CS 473032
Achieving O(n) Time Complexity• Finally, the box is
colliding with six level 4 nodes, one of which contains another object. Note that the object we just returned was on an edge, so it was contained within the level 3 node instead of a level 4 node.
• Credit: http://www.kyleschouviller.com/wsuxna/quadtree-source-included/
![Page 33: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/33.jpg)
CS 473033
Achieving O(n) Time ComplexityAnother solution is the plane sweep algorithm
C
B
R
A
x
y
A 0 A 1 R 0 B0 R 1 C 0 C 1B1
B0
B1A 1
A 0
R 1
R 0
C 1
C 0
![Page 34: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/34.jpg)
CS 473034
Collision Resolution• Two billiard balls strike
– Calculate ball positions at time of impact– Impart new velocities 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 velocities affected
![Page 35: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/35.jpg)
CS 473035
Collision Resolution• Resolution has three parts
1. Prologue2. Collision3. Epilogue
![Page 36: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/36.jpg)
CS 473036
Prologue• Collision known to have occurred• Check if collision should be ignored• Other events might be triggered
– Sound effects– Send collision notification messages
![Page 37: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/37.jpg)
CS 473037
Collision• Place objects at point of impact• Assign new velocities
– Using physics or– Using some other decision logic
![Page 38: CS 4730 Collision Detection CS 4730 – Computer Game Design](https://reader036.vdocument.in/reader036/viewer/2022062308/56649db55503460f94aa62d9/html5/thumbnails/38.jpg)
CS 473038
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