Giter Club home page Giter Club logo

dynamicaabbtree's Introduction

Dynamic AABB tree

A very simple implementation of a dynamic AABB tree in Java, which can be used, for instance, for broadphase collision detection or finding objects in proximity. It was written with minimal object allocation in mind, but it could probably be optimized more in a few places, but its overall performance and memory usage should be acceptable (didn't test it, though.)

The only dependency the library has is JOML, which is a very nice 3D math library for Java, but if there's enough demand, I could try to remove the dependency.

The code has some unit tests written and I encourage you to fiddle with the source code. If you find any bugs/suggestions, feel free to create an issue. I'll do my best to work on them in my spare time. There's also some frustum culling code in the tree, but I didn't have time to test it.

Usage

The tree was designed to be easily used with any code that you might have. The only requirement is to implement two interfaces - Identifiable, which should return a unique identifier for each object that you add to the tree, and Boundable which should return an axis-aligned box of the object that you add to the tree. Have a look at pl.pateman.dynamicaabbtree.TestEntity to see an example implementation.

A single instance of AABBTree should not be used across multiple threads.

Credits and kudos

Portions of the code were inspired by Dyn4J's DynamicAABBTree class. Here are a few articles that helped me understand the whole structure:

http://www.randygaul.net/2013/08/06/dynamic-aabb-tree/

https://www.azurefromthetrenches.com/introductory-guide-to-aabb-tree-collision-detection/

http://allenchou.net/2014/02/game-physics-broadphase-dynamic-aabb-tree/

dynamicaabbtree's People

Contributors

pateman avatar smartcmd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dynamicaabbtree's Issues

Crash on update of an Entity

The following test case crashes:

 @Test
   public void shouldDetectRayIntersection() {
      // Given
      AABBTree<TestEntity> tree = givenTree();
      Rayf ray = new Rayf(0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f);
      TestEntity entity1 = new TestEntity(1, 2.0f, 0.0f, 3.0f, 3.0f);
      TestEntity entity2 = new TestEntity(2, -5.0f, 0.0f, 3.0f, 2.0f);
      tree.add(entity1);
      tree.add(entity2);

      // When
      List<TestEntity> intersecting = new ArrayList<>();
      tree.detectRayIntersection(ray, intersecting);

      tree.update(entity1);
      tree.update(entity2);
      tree.update(entity2);
      tree.update(entity2);

      // Then
      assertEquals(1, intersecting.size());
      assertEquals(1L, intersecting.get(0).getID());
   }

Stacktrace:
java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 3
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:458)
at pl.pateman.dynamicaabbtree.AABBTree.getNodeAt(AABBTree.java:102)
at pl.pateman.dynamicaabbtree.AABBTree.balanceLeft(AABBTree.java:146)
at pl.pateman.dynamicaabbtree.AABBTree.balanceNode(AABBTree.java:199)
at pl.pateman.dynamicaabbtree.AABBTree.syncUpHierarchy(AABBTree.java:207)
at pl.pateman.dynamicaabbtree.AABBTree.insertNode(AABBTree.java:246)
at pl.pateman.dynamicaabbtree.AABBTree.add(AABBTree.java:292)
at pl.pateman.dynamicaabbtree.AABBTree.update(AABBTree.java:312)
at pl.pateman.dynamicaabbtree.AABBTreeTest.shouldDetectRayIntersection(AABBTreeTest.java:367)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.