2d collision detection for cse 3902 by: matt boggus
TRANSCRIPT
![Page 1: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/1.jpg)
2D Collision Detection
For CSE 3902By: Matt Boggus
![Page 2: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/2.jpg)
Outline
• Collision tests– Pixel-pixel– Square-square– Axis-aligned bounding box (rectangle) testing
• Collision detection loops– Exhaustive comparison– Static objects vs. dynamic objects
![Page 3: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/3.jpg)
COLLISION DETECTION TESTS
![Page 4: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/4.jpg)
Pixel-pixel test
• Given sprite pixel data and position on screen, make a binary mask for each sprite
• Detection test– (XOR == 1)
• Not intersecting
– (XOR == 0) AND (OR == 1)• Intersecting
• Pro: Accurate• Con: Slow; requires working
with image dataRaster graphic sprites (left) and masks (right)
![Page 5: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/5.jpg)
Square-square test
![Page 6: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/6.jpg)
Square-square test
![Page 7: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/7.jpg)
Square-square top-bottom collision
![Page 8: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/8.jpg)
Square-square left-right collision
![Page 9: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/9.jpg)
Non-square collision example (values indicate top-bottom)
![Page 10: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/10.jpg)
Axis-aligned bounding box testing
![Page 11: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/11.jpg)
Axis-aligned bounding box testing
![Page 12: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/12.jpg)
XNA rectangle and methods
• Given two rectangles rectangleA and rectangleB
• Rectangle.Intersects(Rectangle)– Returns true if the rectangles are intersecting, otherwise
false– Example call: rectangleA.Intersects(rectangleB);
• Rectangle.Intersect Method (Rectangle, Rectangle)– Returns the area where the two rectangles overlap, an
empty rectangle if there is no overlap– Example call: Rectangle.Intersect(rectangleA,rectangleB);
![Page 13: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/13.jpg)
Rectangle Intersect left-right
![Page 14: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/14.jpg)
Rectangle Intersect top-bottom
![Page 15: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/15.jpg)
TYPES OF COLLISION LOOPS
![Page 16: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/16.jpg)
Exhaustive comparison
• Given mario, enemyList (length n), and blockList (length m), test
– mario vs. enemyList[0] through [n-1]– mario vs. blockList[0] through [m-1]– enemyList[0] vs. enemyList[1] through [n-1]– enemyList[1] vs. enemyList[2] through [n-1]– …– blockList[0] vs. blockList[1] through [m-1] ?
![Page 17: 2D Collision Detection For CSE 3902 By: Matt Boggus](https://reader035.vdocuments.mx/reader035/viewer/2022072011/56649e0b5503460f94af27c9/html5/thumbnails/17.jpg)
Static vs. Dynamic
• Non-moving, or static, objects only need to be compared against dynamics, not other static objects
• foreach static object, test against all dynamic objects
• foreach dynamic object, test against all other dynamic objects, taking care not to duplicate tests