collision detec on
play

Collision Detec,on CS 4730 Computer Game Design CS - PowerPoint PPT Presentation

Collision Detec,on CS 4730 Computer Game Design CS 4730 Back to Physics Remember what we have here: Objects have posi@on, velocity,


  1. Collision ¡Detec,on ¡ CS ¡4730 ¡– ¡Computer ¡Game ¡Design ¡ ¡ CS ¡4730 ¡

  2. 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 ¡ CS ¡4730 ¡ 2

  3. 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 ¡ CS ¡4730 ¡ 3

  4. 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 ¡ CS ¡4730 ¡ 4

  5. 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 ¡ CS ¡4730 ¡ 5

  6. 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 ¡ CS ¡4730 ¡ 6

  7. 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 ¡ CS ¡4730 ¡ 7

  8. 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 ¡ CS ¡4730 ¡ 8

  9. Pueng ¡It ¡All ¡Together ¡ • So ¡our ¡final ¡formula ¡is: ¡ • v 1f ¡ = ¡ ¡ ((e ¡+ ¡1)*m 2 *v 2 ¡+ ¡v 1 *(m 1 ¡– ¡e*m 2 )) ¡/ ¡(m 1 ¡ + ¡m 2 ) ¡ • v 2f ¡ = ¡ ¡ ((e ¡+ ¡1)*m 1 *v 1 ¡+ ¡v 2 *(m 1 ¡– ¡e*m 2 )) ¡/ ¡(m 1 ¡ + ¡m 2 ) ¡ ¡ CS ¡4730 ¡ 9

  10. Okay… ¡Great! ¡ • Math ¡and ¡physics ¡are ¡great ¡and ¡all… ¡ • … ¡but ¡how ¡do ¡you ¡know ¡if ¡two ¡things ¡collided? ¡ CS ¡4730 ¡ 10

  11. Who’s ¡Colliding? ¡ • Okay, ¡how ¡would ¡you ¡do ¡it? ¡ CS ¡4730 ¡ 11

  12. 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” ¡ CS ¡4730 ¡ 12

  13. 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) ¡ CS ¡4730 ¡ 13

  14. Overlap ¡Tes@ng ¡ A t 0 A A A A t 0.25 A t 0.375 t 0.40625 t 0.4375 t 0.5 t 1 B B B B B B Initial Overlap Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 Test Forward 1/2 Backward 1/4 Forward 1/8 Forward 1/16 Backward 1/32 CS ¡4730 ¡ 14

  15. 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 ¡ CS ¡4730 ¡ 15

  16. 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 ¡ window t -1 t 0 t 1 t 2 bullet CS ¡4730 ¡ 16

  17. Intersec@on ¡Tes@ng ¡ • Predict ¡future ¡collisions ¡ • When ¡predicted: ¡ – Move ¡simula@on ¡to ¡@me ¡of ¡collision ¡ – Resolve ¡collision ¡ – Simulate ¡remaining ¡@me ¡step ¡ CS ¡4730 ¡ 17

  18. Swept ¡Geometry ¡ • Extrude ¡geometry ¡in ¡direc@on ¡of ¡movement ¡ • Swept ¡sphere ¡turns ¡into ¡a ¡ “ capsule ” ¡shape ¡ t 0 t 1 CS ¡4730 ¡ 18

  19. 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 ¡4730 ¡ 19

  20. 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) ¡ CS ¡4730 ¡ 20

  21. 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? ¡ CS ¡4730 ¡ 21

  22. Hit ¡Boxes ¡ • Go ¡for ¡“good ¡enough” ¡ • Efficiency ¡hacks/cheats ¡ – Fewer ¡tests: ¡Exploit ¡spa@al ¡coherence ¡ • Use ¡bounding ¡boxes/spheres ¡ • Hierarchies ¡of ¡bounding ¡boxes/spheres ¡ CS ¡4730 ¡ 22

  23. Bounding ¡Boxes ¡ • Axis-­‑aligned ¡vs. ¡Object-­‑aligned ¡ • Axis-­‑aligned ¡BBox ¡change ¡as ¡object ¡moves ¡ • Approximate ¡by ¡rota@ng ¡BBox ¡ CS ¡4730 ¡

  24. Collision ¡Spheres ¡ • Another ¡op@on ¡is ¡to ¡put ¡everything ¡in ¡a ¡ “bubble” ¡ • Think ¡Super ¡Monkey ¡Ball ¡gone ¡wild ¡ • Sphere ¡collision ¡is ¡cheap ¡to ¡detect! ¡ CS ¡4730 ¡ 24

  25. 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 ¡ ¡ CS ¡4730 ¡ 25

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend