Understanding 2D Collision Detection in Game Development

Slide Note
Embed
Share

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.



Uploaded on Apr 20, 2024 | 5 Views


Presentation Transcript


  1. 2D Collision Detection CSE 3902 BY: MATT BOGGUS

  2. Underlying assumption about speed of sprites Live example in class

  3. Collision tests Distance check Bounding shapes Pixel based Outline Iterating on collidables Exhaustive comparison Optimizations

  4. Collision detection tests

  5. Distance check (point vs. sprite)

  6. Distance check (point vs. circle)

  7. (Axis aligned) Square-square test

  8. Square-square test

  9. Square-square left-right side collision

  10. Square-square top-bottom side collision

  11. Rectangle collision?

  12. (Axis-aligned) Rectangle testing

  13. Rectangle testing On your own, compare to complexity of example on http://jeffreythompson.org/collision-detection/rect-rect.php

  14. 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

  15. Rectangle Intersect left-right

  16. Rectangle Intersect top-bottom

  17. Velocity vectors as another tool for side determination

  18. Bounds checking, position vs. environment min and max values

  19. Bounds checking, position vs. environment min and max values

  20. Or reuse rectangle logic

  21. Or reuse rectangle logic

  22. 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)

  23. Iterating on collidables

  24. 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] } }

  25. 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]

  26. 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]

  27. 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

  28. Optimization: sort and sweep

  29. Optimization data structure: quadtrees

Related


More Related Content