Collision Detec,on CS 2501 Intro to Game Programming and - - PowerPoint PPT Presentation

collision detec on
SMART_READER_LITE
LIVE PREVIEW

Collision Detec,on CS 2501 Intro to Game Programming and - - PowerPoint PPT Presentation

Collision Detec,on CS 2501 Intro to Game Programming and Design CS 2501 Back to Physics Remember what we have here: Objects have


slide-1
SLIDE 1

CS ¡2501 ¡

Collision ¡Detec,on ¡

CS ¡2501 ¡– ¡Intro ¡to ¡Game ¡Programming ¡and ¡Design ¡ ¡

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

CS ¡2501 ¡

Conserva@on ¡of ¡Momentum ¡

  • Whatever ¡momentum ¡one ¡object ¡loses, ¡the ¡
  • ther ¡gains ¡
  • This ¡is ¡a ¡transfer ¡of ¡kine@c ¡energy ¡
  • How ¡objects ¡react ¡to ¡the ¡kine@c ¡energy ¡is ¡the ¡
  • bject’s ¡elas@city ¡
  • The ¡coefficient ¡of ¡res@tu@on ¡defines ¡how ¡

velocity ¡changes ¡before ¡and ¡aber ¡impact ¡based ¡

  • n ¡elas@city ¡

7

slide-8
SLIDE 8

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

slide-9
SLIDE 9

CS ¡2501 ¡

Okay… ¡Great! ¡

  • Math ¡and ¡physics ¡are ¡great ¡and ¡all… ¡
  • … ¡but ¡how ¡do ¡you ¡know ¡if ¡two ¡things ¡collided? ¡

9

slide-10
SLIDE 10

CS ¡2501 ¡

Who’s ¡Colliding? ¡

  • Okay, ¡how ¡would ¡you ¡do ¡it? ¡

10

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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 ¡
  • verlapping ¡(using ¡some ¡of ¡the ¡op@miza@ons ¡

from ¡the ¡previous ¡slide) ¡

12

slide-13
SLIDE 13

CS ¡2501 ¡

Overlap ¡Tes@ng ¡

13

B B

t1 t0.375 t0.25

B

t0

Iteration 1 Forward 1/2 Iteration 2 Backward 1/4 Iteration 3 Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test

t0.5 t0.4375 t0.40625

B B B A A A A A A

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

t0 t-1 t1 t2 bullet window

slide-16
SLIDE 16

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 ¡

slide-17
SLIDE 17

CS ¡2501 ¡ 17

Swept ¡Geometry ¡

  • Extrude ¡geometry ¡in ¡direc@on ¡of ¡movement ¡
  • Swept ¡sphere ¡turns ¡into ¡a ¡“capsule” ¡shape ¡

t0 t1

slide-18
SLIDE 18

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 ¡

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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

slide-22
SLIDE 22

CS ¡2501 ¡

Bounding ¡Boxes ¡

  • Axis-­‑aligned ¡vs. ¡Object-­‑aligned ¡
  • Axis-­‑aligned ¡BBox ¡change ¡as ¡object ¡moves ¡
  • Approximate ¡by ¡rota@ng ¡BBox ¡
slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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 ¡
  • ther ¡
  • Use ¡current ¡speeds ¡to ¡es@mate ¡likely ¡collisions ¡

– Use ¡cheaper ¡tests ¡

25

slide-26
SLIDE 26

CS ¡2501 ¡

Cheaper ¡Distance ¡Tests ¡

  • Cheaper ¡distance ¡calcula@on: ¡

– Compare ¡against ¡ ¡

  • Approxima@on ¡for ¡comparison: ¡

¡ – Manhanan ¡distance ¡ ¡

26

) ) ( ) ((

2 2 1 2 2 1

y y x x sqrt d − + − = d 2

d 2 = (x1 − x2)2 +(y1 − y2)2

slide-27
SLIDE 27

CS ¡2501 ¡

Sprite ¡Collision ¡Detec@on ¡

27

slide-28
SLIDE 28

CS ¡2501 ¡ 28

Achieving ¡O(n) ¡Time ¡Complexity ¡

One ¡solu@on ¡is ¡to ¡par@@on ¡space ¡

slide-29
SLIDE 29

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 B

0 R1C0

C1 B

1

B B

1

A1 A0 R1 R0 C1 C0

slide-30
SLIDE 30

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 ¡

slide-31
SLIDE 31

CS ¡2501 ¡ 31

Collision ¡Resolu@on ¡

  • Resolu@on ¡has ¡three ¡parts ¡
  • 1. ¡Prologue ¡
  • 2. ¡Collision ¡
  • 3. ¡Epilogue ¡
slide-32
SLIDE 32

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 ¡

slide-33
SLIDE 33

CS ¡2501 ¡ 33

Collision ¡

  • Place ¡objects ¡at ¡point ¡of ¡impact ¡
  • Assign ¡new ¡veloci@es ¡

– Using ¡physics ¡or ¡ – Using ¡some ¡other ¡decision ¡logic ¡

slide-34
SLIDE 34

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 ¡