public static void findCollisions( final Spatial spatial, final Spatial scene, final CollisionResults results) { if (spatial == scene || spatial.getWorldBound() == null || !spatial.getSceneHints().isPickingHintEnabled(PickingHint.Collidable) || !scene.getSceneHints().isPickingHintEnabled(PickingHint.Collidable)) { return; } if (spatial instanceof Node) { final Node node = (Node) spatial; if (node.getWorldBound().intersects(scene.getWorldBound())) { // further checking needed. for (int i = 0; i < node.getNumberOfChildren(); i++) { PickingUtil.findCollisions(node.getChild(i), scene, results); } } } else if (spatial instanceof Mesh) { final Mesh mesh = (Mesh) spatial; if (mesh.getWorldBound().intersects(scene.getWorldBound())) { if (scene instanceof Node) { final Node parent = (Node) scene; for (int i = 0; i < parent.getNumberOfChildren(); i++) { PickingUtil.findCollisions(mesh, parent.getChild(i), results); } } else { results.addCollision(mesh, (Mesh) scene); } } } }
public static boolean hasCollision( final Spatial spatial, final Spatial scene, final boolean checkPrimitives) { if (spatial == scene || spatial.getWorldBound() == null || !spatial.getSceneHints().isPickingHintEnabled(PickingHint.Collidable) || !scene.getSceneHints().isPickingHintEnabled(PickingHint.Collidable)) { return false; } if (spatial instanceof Node) { final Node node = (Node) spatial; if (node.getWorldBound().intersects(scene.getWorldBound())) { if (node.getNumberOfChildren() == 0 && !checkPrimitives) { return true; } // further checking needed. for (int i = 0; i < node.getNumberOfChildren(); i++) { if (PickingUtil.hasCollision(node.getChild(i), scene, checkPrimitives)) { return true; } } } } else if (spatial instanceof Mesh) { final Mesh mesh = (Mesh) spatial; if (mesh.getWorldBound().intersects(scene.getWorldBound())) { if (scene instanceof Node) { final Node parent = (Node) scene; for (int i = 0; i < parent.getNumberOfChildren(); i++) { if (PickingUtil.hasCollision(mesh, parent.getChild(i), checkPrimitives)) { return true; } } return false; } if (!checkPrimitives) { return true; } return PickingUtil.hasTriangleCollision(mesh, (Mesh) scene); } return false; } return false; }
public Object getChild(final Object parent, final int index) { if (parent instanceof Node) { final Node parentNode = (Node) parent; return parentNode.getChild(index); } return null; }
public static void findPick(final Spatial spatial, final Ray3 ray, final PickResults results) { if (!spatial.getSceneHints().isPickingHintEnabled(PickingHint.Pickable)) { return; } if (spatial instanceof Node) { final Node node = (Node) spatial; if (node.getNumberOfChildren() == 0 || node.getWorldBound() == null) { return; } if (node.getWorldBound().intersects(ray)) { // further checking needed. for (int i = 0; i < node.getNumberOfChildren(); i++) { PickingUtil.findPick(node.getChild(i), ray, results); } } } else if (spatial instanceof Mesh) { final Mesh mesh = (Mesh) spatial; if (mesh.getWorldBound() == null) { return; } if (mesh.getWorldBound().intersects(ray)) { // find the primitive that is being hit. // add this node and the primitive to the PickResults list. results.addPick(ray, mesh); } } }
/** * Recreate this Collision Tree for the given Node and child index. * * @param childIndex the index of the child to generate the tree for. * @param parent The Node that this tree should represent. * @param doSort true to sort primitives during creation, false otherwise */ public void construct( final int childIndex, final int section, final Node parent, final boolean doSort) { final Spatial spat = parent.getChild(childIndex); if (spat instanceof Mesh) { _mesh = makeRef((Mesh) spat); _primitiveIndices = new int[((Mesh) spat).getMeshData().getPrimitiveCount(section)]; for (int i = 0; i < _primitiveIndices.length; i++) { _primitiveIndices[i] = i; } createTree(section, 0, _primitiveIndices.length, doSort); } }