Understanding 2D Collision Detection in Game Development
Explore the intricacies of 2D collision detection in game development through methods like distance checks, bounding shapes, and optimizing collision tests. Dive into concepts like sprite speed assumptions, square-square collisions, and rectangle testing complexities. Learn about useful Rectangle methods and their applications in MonoGame.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
2D Collision Detection CSE 3902 BY: MATT BOGGUS
Underlying assumption about speed of sprites Live example in class
Collision tests Distance check Bounding shapes Pixel based Outline Iterating on collidables Exhaustive comparison Optimizations
Collision detection tests
(Axis aligned) Square-square test
Square-square left-right side collision
Square-square top-bottom side collision
Rectangle testing On your own, compare to complexity of example on http://jeffreythompson.org/collision-detection/rect-rect.php
Useful Rectangle 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); MonoGame Rectangle reference page
Velocity vectors as another tool for side determination
Bounds checking, position vs. environment min and max values
Bounds checking, position vs. environment min and max values
Pixel-pixel collision detection As loops: Foreach (x pixel coordinate){ Foreach (y pixel coordinate){ /* Are there multiple sprites with a non- transparent color at (x,y) */ }} As bitwise operations (XOR == 1) Not intersecting (XOR == 0) AND (OR == 1) Intersecting Raster graphic sprites (left) and masks (right)
Iterating on collidables
Exhaustive comparison: first approach Given gameObjectList(length n), for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { // test gameObjectList[i] against gameObjectList[j] } }
Exhaustive comparison: avoiding duplicate tests Given gameObjectList(length n), test: gameObjectList[0] vs. gameObjectList[1] through [n-1] gameObjectList[1] vs. gameObjectList[2] through [n-1] gameObjectList[n-2] vs. gameObjectList[n-1]
Exhaustive comparison: more considerations Given enemyList (length n), blockList (length m), consider cases: Skip cases when (i == j), enemyList[0] vs. enemyList[1] through [n-1] enemyList[1] vs. enemyList[0] and enemyList[2] through [n-1] enemyList[2] vs. enemyList[0], enemyList[1], and enemyList[3] through [n-1] blockList[0] vs. blockList[1] through [m-1]
Optimization: Static and Dynamic Categories Non-moving, or static, objects only need to be compared against dynamics, not other static objects foreach dynamic object, test against all static objects all other dynamic objects, avoiding duplicate tests
Optimization data structure: quadtrees