Java Code Examples for com.jme3.collision.CollisionResults#size()
The following examples show how to use
com.jme3.collision.CollisionResults#size() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BresenhamTerrainPicker.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * This method adds the found Collision to an existing collisionResult. * @param results The results to add this collision to * @param patch The TerrainPatch which collided * @param intersection The actual intersection position * @param hit The hit triangle * @param distance The distance at which the hit occurred * @return Whether the collision was accepted to the list or whether it has been deduplicated */ private boolean addCollision(CollisionResults results, TerrainPatch patch, Vector3f intersection, Triangle hit, float distance) { CollisionResult cr = new CollisionResult(intersection.clone(), distance); cr.setGeometry(patch); cr.setContactNormal(hit.getNormal()); cr.setTriangleIndex(hit.getIndex()); // this will probably always be 0 for (int i = 0; i < results.size(); i++) { CollisionResult compare = results.getCollision(i); if (compare.getDistance() == cr.getDistance() && compare.getGeometry() == cr.getGeometry() && compare.getContactPoint().equals(cr.getContactPoint()) && compare.getContactNormal().equals(cr.getContactNormal())) { return false; // Collision already available, deduplicate. } } results.addCollision(cr); return true; }
Example 2
Source File: TestRayCollision.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
public static void main(String[] args){ Ray r = new Ray(Vector3f.ZERO, Vector3f.UNIT_X); BoundingBox bbox = new BoundingBox(new Vector3f(5, 0, 0), 1, 1, 1); CollisionResults res = new CollisionResults(); bbox.collideWith(r, res); System.out.println("Bounding:" +bbox); System.out.println("Ray: "+r); System.out.println("Num collisions: "+res.size()); for (int i = 0; i < res.size(); i++){ System.out.println("--- Collision #"+i+" ---"); float dist = res.getCollision(i).getDistance(); Vector3f pt = res.getCollision(i).getContactPoint(); System.out.println("distance: "+dist); System.out.println("point: "+pt); } }
Example 3
Source File: TestRayCollision.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 6 votes |
public static void main(String[] args){ Ray r = new Ray(Vector3f.ZERO, Vector3f.UNIT_X); BoundingBox bbox = new BoundingBox(new Vector3f(5, 0, 0), 1, 1, 1); CollisionResults res = new CollisionResults(); bbox.collideWith(r, res); System.out.println("Bounding:" +bbox); System.out.println("Ray: "+r); System.out.println("Num collisions: "+res.size()); for (int i = 0; i < res.size(); i++){ System.out.println("--- Collision #"+i+" ---"); float dist = res.getCollision(i).getDistance(); Vector3f pt = res.getCollision(i).getContactPoint(); System.out.println("distance: "+dist); System.out.println("point: "+pt); } }
Example 4
Source File: BIHTree.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
private int collideWithRay(Ray r, Matrix4f worldMatrix, BoundingVolume worldBound, CollisionResults results) { TempVars vars = TempVars.get(); try { CollisionResults boundResults = vars.collisionResults; boundResults.clear(); worldBound.collideWith(r, boundResults); if (boundResults.size() > 0) { float tMin = boundResults.getClosestCollision().getDistance(); float tMax = boundResults.getFarthestCollision().getDistance(); if (tMax <= 0) { tMax = Float.POSITIVE_INFINITY; } else if (tMin == tMax) { tMin = 0; } if (tMin <= 0) { tMin = 0; } if (r.getLimit() < Float.POSITIVE_INFINITY) { tMax = Math.min(tMax, r.getLimit()); if (tMin > tMax){ return 0; } } // return root.intersectBrute(r, worldMatrix, this, tMin, tMax, results); return root.intersectWhere(r, worldMatrix, this, tMin, tMax, results); } return 0; } finally { vars.release(); } }
Example 5
Source File: HelloPicking.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
public void onAction(String name, boolean keyPressed, float tpf) { if (name.equals("Shoot") && !keyPressed) { // 1. Reset results list. CollisionResults results = new CollisionResults(); // 2. Aim the ray from cam loc to cam direction. Ray ray = new Ray(cam.getLocation(), cam.getDirection()); // 3. Collect intersections between Ray and Shootables in results list. shootables.collideWith(ray, results); // 4. Print the results System.out.println("----- Collisions? " + results.size() + "-----"); for (int i = 0; i < results.size(); i++) { // For each hit, we know distance, impact point, name of geometry. float dist = results.getCollision(i).getDistance(); Vector3f pt = results.getCollision(i).getContactPoint(); String hit = results.getCollision(i).getGeometry().getName(); System.out.println("* Collision #" + i); System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away."); } // 5. Use the results (we mark the hit object) if (results.size() > 0) { // The closest collision point is what was truly hit: CollisionResult closest = results.getClosestCollision(); // Let's interact - we mark the hit with a red dot. mark.setLocalTranslation(closest.getContactPoint()); rootNode.attachChild(mark); } else { // No hits? Then remove the red mark. rootNode.detachChild(mark); } } }
Example 6
Source File: TestTriangleCollision.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void simpleUpdate(float tpf) { CollisionResults results = new CollisionResults(); BoundingVolume bv = geom1.getWorldBound(); golem.collideWith(bv, results); if (results.size() > 0) { geom1.getMaterial().setColor("Color", ColorRGBA.Red); }else{ geom1.getMaterial().setColor("Color", ColorRGBA.Blue); } }
Example 7
Source File: TestMousePick.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void simpleUpdate(float tpf){ Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f); Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f); direction.subtractLocal(origin).normalizeLocal(); Ray ray = new Ray(origin, direction); CollisionResults results = new CollisionResults(); shootables.collideWith(ray, results); // System.out.println("----- Collisions? " + results.size() + "-----"); // for (int i = 0; i < results.size(); i++) { // // For each hit, we know distance, impact point, name of geometry. // float dist = results.getCollision(i).getDistance(); // Vector3f pt = results.getCollision(i).getWorldContactPoint(); // String hit = results.getCollision(i).getGeometry().getName(); // System.out.println("* Collision #" + i); // System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away."); // } if (results.size() > 0) { CollisionResult closest = results.getClosestCollision(); mark.setLocalTranslation(closest.getContactPoint()); Quaternion q = new Quaternion(); q.lookAt(closest.getContactNormal(), Vector3f.UNIT_Y); mark.setLocalRotation(q); rootNode.attachChild(mark); } else { rootNode.detachChild(mark); } }
Example 8
Source File: PointUtil.java From OpenRTS with MIT License | 5 votes |
private static CollisionResult getCollision(Node n, Ray r) { CollisionResults results = new CollisionResults(); n.collideWith(r, results); if (results.size() == 0) { return null; } return results.getClosestCollision(); }
Example 9
Source File: HelloPicking.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void onAction(String name, boolean keyPressed, float tpf) { if (name.equals("Shoot") && !keyPressed) { // 1. Reset results list. CollisionResults results = new CollisionResults(); // 2. Aim the ray from cam loc to cam direction. Ray ray = new Ray(cam.getLocation(), cam.getDirection()); // 3. Collect intersections between Ray and Shootables in results list. shootables.collideWith(ray, results); // 4. Print the results System.out.println("----- Collisions? " + results.size() + "-----"); for (int i = 0; i < results.size(); i++) { // For each hit, we know distance, impact point, name of geometry. float dist = results.getCollision(i).getDistance(); Vector3f pt = results.getCollision(i).getContactPoint(); String hit = results.getCollision(i).getGeometry().getName(); System.out.println("* Collision #" + i); System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away."); } // 5. Use the results (we mark the hit object) if (results.size() > 0) { // The closest collision point is what was truly hit: CollisionResult closest = results.getClosestCollision(); // Let's interact - we mark the hit with a red dot. mark.setLocalTranslation(closest.getContactPoint()); rootNode.attachChild(mark); } else { // No hits? Then remove the red mark. rootNode.detachChild(mark); } } }
Example 10
Source File: TerrainTestCollision.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void printCollisions(CollisionResults cr) { System.out.println("================ Collision Results ================"); for (int i = 0; i < cr.size(); i++) { CollisionResult res = cr.getCollision(i); System.out.println("Result " + i); System.out.println("\t\t" + res.toString()); } System.out.println("================ END Collision Results ================"); }
Example 11
Source File: TestTriangleCollision.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void simpleUpdate(float tpf) { CollisionResults results = new CollisionResults(); BoundingVolume bv = geom1.getWorldBound(); golem.collideWith(bv, results); if (results.size() > 0) { geom1.getMaterial().setColor("Color", ColorRGBA.Red); }else{ geom1.getMaterial().setColor("Color", ColorRGBA.Blue); } }
Example 12
Source File: TestMousePick.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void simpleUpdate(float tpf){ Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f); Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f); direction.subtractLocal(origin).normalizeLocal(); Ray ray = new Ray(origin, direction); CollisionResults results = new CollisionResults(); shootables.collideWith(ray, results); // System.out.println("----- Collisions? " + results.size() + "-----"); // for (int i = 0; i < results.size(); i++) { // // For each hit, we know distance, impact point, name of geometry. // float dist = results.getCollision(i).getDistance(); // Vector3f pt = results.getCollision(i).getWorldContactPoint(); // String hit = results.getCollision(i).getGeometry().getName(); // System.out.println("* Collision #" + i); // System.out.println(" You shot " + hit + " at " + pt + ", " + dist + " wu away."); // } if (results.size() > 0) { CollisionResult closest = results.getClosestCollision(); mark.setLocalTranslation(closest.getContactPoint()); Quaternion q = new Quaternion(); q.lookAt(closest.getContactNormal(), Vector3f.UNIT_Y); mark.setLocalRotation(q); rootNode.attachChild(mark); } else { rootNode.detachChild(mark); } }
Example 13
Source File: AbstractSceneEditor3DPart.java From jmonkeybuilder with Apache License 2.0 | 4 votes |
/** * Update editing nodes. */ @JmeThread private void updatePaintingNodes() { if (!isPaintingMode()) { return; } final Node cursorNode = getCursorNode(); final PaintingControl control = PaintingUtils.getPaintingControl(cursorNode); final Spatial paintedModel = PaintingUtils.getPaintedModel(control); if (paintedModel == null) { return; } final CollisionResults collisions = GeomUtils.getCollisionsFromCursor(paintedModel, getCamera()); if (collisions.size() < 1) { return; } CollisionResult result = null; for (final CollisionResult collision : collisions) { final Geometry geometry = collision.getGeometry(); final Object parent = NodeUtils.findParent(geometry, spatial -> spatial.getUserData(KEY_IGNORE_RAY_CAST) == Boolean.TRUE); if (parent == null) { result = collision; break; } } if (result == null) { result = collisions.getClosestCollision(); } final Vector3f contactPoint = result.getContactPoint(); final Vector3f contactNormal = result.getContactNormal(); final LocalObjects local = LocalObjects.get(); final Quaternion rotation = local.nextRotation(); rotation.lookAt(contactNormal, Vector3f.UNIT_Y); cursorNode.setLocalRotation(rotation); cursorNode.setLocalTranslation(contactPoint); }
Example 14
Source File: BIHNode.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 4 votes |
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)); 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)); 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; }
Example 15
Source File: EntropyComputeUtil.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 4 votes |
public static float computeLodEntropy(Mesh terrainBlock, Buffer lodIndices){ // Bounding box for the terrain block BoundingBox bbox = (BoundingBox) terrainBlock.getBound(); // Vertex positions for the block FloatBuffer positions = terrainBlock.getFloatBuffer(Type.Position); // Prepare to cast rays Vector3f pos = new Vector3f(); Vector3f dir = new Vector3f(0, -1, 0); Ray ray = new Ray(pos, dir); // Prepare collision results CollisionResults results = new CollisionResults(); // Set the LOD indices on the block VertexBuffer originalIndices = terrainBlock.getBuffer(Type.Index); terrainBlock.clearBuffer(Type.Index); if (lodIndices instanceof IntBuffer) terrainBlock.setBuffer(Type.Index, 3, (IntBuffer)lodIndices); else if (lodIndices instanceof ShortBuffer) { terrainBlock.setBuffer(Type.Index, 3, (ShortBuffer) lodIndices); } // Recalculate collision mesh terrainBlock.createCollisionData(); float entropy = 0; for (int i = 0; i < positions.limit() / 3; i++){ BufferUtils.populateFromBuffer(pos, positions, i); float realHeight = pos.y; pos.addLocal(0, bbox.getYExtent(), 0); ray.setOrigin(pos); results.clear(); terrainBlock.collideWith(ray, Matrix4f.IDENTITY, bbox, results); if (results.size() > 0){ Vector3f contactPoint = results.getClosestCollision().getContactPoint(); float delta = Math.abs(realHeight - contactPoint.y); entropy = Math.max(delta, entropy); } } // Restore original indices terrainBlock.clearBuffer(Type.Index); terrainBlock.setBuffer(originalIndices); return entropy; }
Example 16
Source File: EntropyComputeUtil.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 4 votes |
public static float computeLodEntropy(Mesh terrainBlock, Buffer lodIndices){ // Bounding box for the terrain block BoundingBox bbox = (BoundingBox) terrainBlock.getBound(); // Vertex positions for the block FloatBuffer positions = terrainBlock.getFloatBuffer(Type.Position); // Prepare to cast rays Vector3f pos = new Vector3f(); Vector3f dir = new Vector3f(0, -1, 0); Ray ray = new Ray(pos, dir); // Prepare collision results CollisionResults results = new CollisionResults(); // Set the LOD indices on the block VertexBuffer originalIndices = terrainBlock.getBuffer(Type.Index); terrainBlock.clearBuffer(Type.Index); if (lodIndices instanceof IntBuffer) terrainBlock.setBuffer(Type.Index, 3, (IntBuffer)lodIndices); else if (lodIndices instanceof ShortBuffer) { terrainBlock.setBuffer(Type.Index, 3, (ShortBuffer) lodIndices); } else { terrainBlock.setBuffer(Type.Index, 3, (ByteBuffer) lodIndices); } // Recalculate collision mesh terrainBlock.createCollisionData(); float entropy = 0; for (int i = 0; i < positions.limit() / 3; i++){ BufferUtils.populateFromBuffer(pos, positions, i); float realHeight = pos.y; pos.addLocal(0, bbox.getYExtent(), 0); ray.setOrigin(pos); results.clear(); terrainBlock.collideWith(ray, Matrix4f.IDENTITY, bbox, results); if (results.size() > 0){ Vector3f contactPoint = results.getClosestCollision().getContactPoint(); float delta = Math.abs(realHeight - contactPoint.y); entropy = Math.max(delta, entropy); } } // Restore original indices terrainBlock.clearBuffer(Type.Index); terrainBlock.setBuffer(originalIndices); return entropy; }
Example 17
Source File: BIHNode.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 4 votes |
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)); 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)); 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; }