Trying to use quadtrees for collision detection in a game

dwnenr

I'm currently implementing a collision detection system using quadtrees. I was able to implement the quadtree, but I had a question regarding a particular situation. Lets say my initial quadtree has a boundary of 200x200. So my 1st level of sub-quadtrees will have a boundary of:

NW: (0, 0) ~ (100,100)

SW: (0, 100) ~ (100, 200)

NE: (100, 0) ~ (200, 100)

SE: (100, 100) ~ (200, 200)

Lets say there is an square object at (98, 98), and its width and height are 5. I take the center position when inserting them into the sub-trees, so this object will be placed in the NW quadtree. However, since its width and height are 5, they are technically also on all other 3 quadtrees. Will I have to check if the object's boundary spans to the other side of the quadtree, and if yes, add multiple instance into the quadtree?

user4842163

Yes -- either that or, upon insertion, split your square up into 4 separate ones in this case. Easier is just allow duplicates to be stored at the leaves, and make that cheap (pointer/index to the square, e.g.)

You can also make a loose-fitting tree where you just pick one of the children to insert this square, but expand its bounding box to fit. In those cases, you have overlapping partitions and have to recursively descend down the tree to search, so it has its pros and cons.

Another thing you can do is allow elements to be stored in the branches and not just the leaves. In that case, if an element wants to go into multiple children, just store it at the branch instead of descending further. For this, you have to check for elements at each branch during traversal and not just at the leaves.

I'd suggest just starting out with duplicates. The other methods can be a real hassle, and no need to bother with such hassle (until you measure a need for it).

These kinds of elements that have an area at the dead center of any branch can want to make your tree really deep and imbalanced, so you typically want a healthy stop gap to prevent that from recursing forever. It's typically not a big issue unless you have a boatload of elements like this.

Also, for 2D cases, you can sometimes get away with a fixed grid. It can actually work better sometimes to just have an NxM grid than a quad-tree since it's so simple to build (which makes it easier to update quickly for very dynamic content where you have, say, sprites moving around on the screen every frame), and doesn't have tricky worst-case scenarios since it doesn't have this notion of "depth", just cells containing elements.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Trying to use quadtrees for collision detection in a game

From Dev

Collision detection in javascript game

From Dev

Javascript Canvas Game - Collision Detection

From Dev

Collision detection in java 2d game

From Dev

Collision detection in javascript canvas tanks game

From Dev

Java Game Physics - Determine intersection and collision detection

From Dev

Game score via collision detection increment

From Dev

Collision Detection for fast moving game object in Unity

From Dev

Problem with collision detection is JS canvas game

From Dev

Use PNG as Background Map with collision detection

From Dev

Use PNG as Background Map with collision detection

From Dev

HTML Canvas game: 2D collision detection

From Dev

game viewport focus vs collision detection on y axis against gravity

From Dev

Collision detection in 2D game without tiles XNA

From Dev

Collision Detection in FPS Game using three.js

From Dev

if statement in JavaScript game not working as desired to allow for collision detection

From Dev

Phonegap game development collision detection regarding different screen sizes

From Dev

Collision detection in 2D game without tiles XNA

From Dev

game viewport focus vs collision detection on y axis against gravity

From Dev

Collision Detection in FPS Game using three.js

From Dev

Collision detection in tile-base game only applies to the file tile in the array

From Dev

XNA - Farseer Physics 3.5 - Collision Detection Issues - No/Zero Gravity - Space Game

From Dev

Python collision detection

From Dev

Moving platform collision detection

From Dev

Concave collision detection in Bullet

From Java

Collision detection in pygame not working

From Dev

Rectangle and Circle collision detection

From Dev

Python collision detection, NOT in pygame

From Dev

Collision detection in pygame

Related Related

  1. 1

    Trying to use quadtrees for collision detection in a game

  2. 2

    Collision detection in javascript game

  3. 3

    Javascript Canvas Game - Collision Detection

  4. 4

    Collision detection in java 2d game

  5. 5

    Collision detection in javascript canvas tanks game

  6. 6

    Java Game Physics - Determine intersection and collision detection

  7. 7

    Game score via collision detection increment

  8. 8

    Collision Detection for fast moving game object in Unity

  9. 9

    Problem with collision detection is JS canvas game

  10. 10

    Use PNG as Background Map with collision detection

  11. 11

    Use PNG as Background Map with collision detection

  12. 12

    HTML Canvas game: 2D collision detection

  13. 13

    game viewport focus vs collision detection on y axis against gravity

  14. 14

    Collision detection in 2D game without tiles XNA

  15. 15

    Collision Detection in FPS Game using three.js

  16. 16

    if statement in JavaScript game not working as desired to allow for collision detection

  17. 17

    Phonegap game development collision detection regarding different screen sizes

  18. 18

    Collision detection in 2D game without tiles XNA

  19. 19

    game viewport focus vs collision detection on y axis against gravity

  20. 20

    Collision Detection in FPS Game using three.js

  21. 21

    Collision detection in tile-base game only applies to the file tile in the array

  22. 22

    XNA - Farseer Physics 3.5 - Collision Detection Issues - No/Zero Gravity - Space Game

  23. 23

    Python collision detection

  24. 24

    Moving platform collision detection

  25. 25

    Concave collision detection in Bullet

  26. 26

    Collision detection in pygame not working

  27. 27

    Rectangle and Circle collision detection

  28. 28

    Python collision detection, NOT in pygame

  29. 29

    Collision detection in pygame

HotTag

Archive