/** * Gets a {@link CollisionResults} between a {@link Collidable} and a {@link Ray}. Collidables * like Spatials and Nodes can be tested for collisions with Rays (usually from {@link * #getCrosshairRay()} and {@link #getCursorRay()}. * * @param collidable The object we are trying to hit. * @param ray A ray that is being cast at the collidable. * @return A CollisionResults for the collision between the collidable object and the ray. */ public CollisionResults getCollision(Collidable collidable, Ray ray) { // Create a new results list. CollisionResults results = new CollisionResults(); // See if the ray collides. if (collidable != null && ray != null) { collidable.collideWith(ray, results); } return results; }
public final int intersectWhere( Collidable col, BoundingBox box, Matrix4f worldMatrix, BIHTree tree, CollisionResults results) { TempVars vars = TempVars.get(); ArrayList<BIHStackData> stack = vars.bihStack; stack.clear(); float[] minExts = { box.getCenter().x - box.getXExtent(), box.getCenter().y - box.getYExtent(), box.getCenter().z - box.getZExtent() }; float[] maxExts = { box.getCenter().x + box.getXExtent(), box.getCenter().y + box.getYExtent(), box.getCenter().z + box.getZExtent() }; // stack.add(new BIHStackData(this, 0, 0)); vars.addStackData(this, 0f, 0f); Triangle t = new Triangle(); int cols = 0; stackloop: while (stack.size() > 0) { BIHNode node = stack.remove(stack.size() - 1).node; while (node.axis != 3) { int a = node.axis; float maxExt = maxExts[a]; float minExt = minExts[a]; if (node.leftPlane < node.rightPlane) { // means there's a gap in the middle // if the box is in that gap, we stop there if (minExt > node.leftPlane && maxExt < node.rightPlane) { continue stackloop; } } if (maxExt < node.rightPlane) { node = node.left; } else if (minExt > node.leftPlane) { node = node.right; } else { // stack.add(new BIHStackData(node.right, 0, 0)); vars.addStackData(node.right, 0f, 0f); node = node.left; } // if (maxExt < node.leftPlane // && maxExt < node.rightPlane){ // node = node.left; // }else if (minExt > node.leftPlane // && minExt > node.rightPlane){ // node = node.right; // }else{ // } } for (int i = node.leftIndex; i <= node.rightIndex; i++) { tree.getTriangle(i, t.get1(), t.get2(), t.get3()); if (worldMatrix != null) { worldMatrix.mult(t.get1(), t.get1()); worldMatrix.mult(t.get2(), t.get2()); worldMatrix.mult(t.get3(), t.get3()); } int added = col.collideWith(t, results); if (added > 0) { int index = tree.getTriangleIndex(i); int start = results.size() - added; for (int j = start; j < results.size(); j++) { CollisionResult cr = results.getCollisionDirect(j); cr.setTriangleIndex(index); } cols += added; } } } vars.release(); return cols; }