com.jme3.collision.CollisionResults Java Examples
The following examples show how to use
com.jme3.collision.CollisionResults.
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: Octnode.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
public final void intersectWhere(Ray r, Geometry[] geoms, float sceneMin, float sceneMax, CollisionResults results){ for (OCTTriangle t : tris){ float d = r.intersects(t.get1(), t.get2(), t.get3()); if (Float.isInfinite(d)) continue; Vector3f contactPoint = new Vector3f(r.getDirection()).multLocal(d).addLocal(r.getOrigin()); CollisionResult result = new CollisionResult(geoms[t.getGeometryIndex()], contactPoint, d, t.getTriangleIndex()); results.addCollision(result); } for (int i = 0; i < 8; i++){ Octnode child = children[i]; if (child == null) continue; if (child.bbox.intersects(r)){ child.intersectWhere(r, geoms, sceneMin, sceneMax, results); } } }
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: 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 #4
Source File: TerrainPatch.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public int collideWith(Collidable other, CollisionResults results) throws UnsupportedCollisionException { if (refreshFlags != 0) throw new IllegalStateException("Scene graph must be updated" + " before checking collision"); if (other instanceof BoundingVolume) if (!getWorldBound().intersects((BoundingVolume)other)) return 0; if(other instanceof Ray) return collideWithRay((Ray)other, results); else if (other instanceof BoundingVolume) return collideWithBoundingVolume((BoundingVolume)other, results); else { throw new UnsupportedCollisionException("TerrainPatch cannot collide with "+other.getClass().getName()); } }
Example #5
Source File: Ray.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
public int collideWith(Collidable other, CollisionResults results) { if (other instanceof BoundingVolume) { BoundingVolume bv = (BoundingVolume) other; return bv.collideWith(this, results); } else if (other instanceof AbstractTriangle) { AbstractTriangle tri = (AbstractTriangle) other; float d = intersects(tri.get1(), tri.get2(), tri.get3()); if (Float.isInfinite(d) || Float.isNaN(d)) { return 0; } Vector3f point = new Vector3f(direction).multLocal(d).addLocal(origin); results.addCollision(new CollisionResult(point, d)); return 1; } else { throw new UnsupportedCollisionException(); } }
Example #6
Source File: TerrainCameraController.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
/** * Find where on the terrain the mouse intersects. */ protected Vector3f getTerrainCollisionPoint() { if (editorController.getTerrain(null) == null) { return null; } CollisionResults results = new CollisionResults(); Ray ray = new Ray(); Vector3f pos = cam.getWorldCoordinates(new Vector2f(mouseX, mouseY), 0).clone(); Vector3f dir = cam.getWorldCoordinates(new Vector2f(mouseX, mouseY), 0.3f).clone(); dir.subtractLocal(pos).normalizeLocal(); ray.setOrigin(pos); ray.setDirection(dir); editorController.getTerrain(null).collideWith(ray, results); if (results == null) { return null; } final CollisionResult result = results.getClosestCollision(); if (result == null) { return null; } return result.getContactPoint(); }
Example #7
Source File: Mesh.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
/** * Handles collision detection, internal use only. * User code should only use collideWith() on scene * graph elements such as {@link Spatial}s. */ public int collideWith(Collidable other, Matrix4f worldMatrix, BoundingVolume worldBound, CollisionResults results){ if (getVertexCount() == 0) { return 0; } if (collisionTree == null){ createCollisionData(); } return collisionTree.collideWith(other, worldMatrix, worldBound, results); }
Example #8
Source File: BoundingBox.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
public int collideWith(Collidable other, CollisionResults results) { if (other instanceof Ray) { Ray ray = (Ray) other; return collideWithRay(ray, results); } else if (other instanceof Triangle) { Triangle t = (Triangle) other; if (intersects(t.get1(), t.get2(), t.get3())) { CollisionResult r = new CollisionResult(); results.addCollision(r); return 1; } return 0; } else { throw new UnsupportedCollisionException("With: " + other.getClass().getSimpleName()); } }
Example #9
Source File: TerrainQuad.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public int collideWith(Collidable other, CollisionResults results){ int total = 0; if (other instanceof Ray) return collideWithRay((Ray)other, results); // if it didn't collide with this bbox, return if (other instanceof BoundingVolume) if (!this.getWorldBound().intersects((BoundingVolume)other)) return total; for (Spatial child : children){ total += child.collideWith(other, results); } return total; }
Example #10
Source File: TerrainQuad.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Gather the terrain patches that intersect the given ray (toTest). * This only tests the bounding boxes * @param toTest * @param results */ public void findPick(Ray toTest, List<TerrainPickData> results) { if (getWorldBound() != null) { if (getWorldBound().intersects(toTest)) { // further checking needed. for (int i = 0; i < getQuantity(); i++) { if (children.get(i) instanceof TerrainPatch) { TerrainPatch tp = (TerrainPatch) children.get(i); tp.ensurePositiveVolumeBBox(); if (tp.getWorldBound().intersects(toTest)) { CollisionResults cr = new CollisionResults(); toTest.collideWith(tp.getWorldBound(), cr); if (cr.getClosestCollision() != null) { cr.getClosestCollision().getDistance(); results.add(new TerrainPickData(tp, cr.getClosestCollision())); } } } else if (children.get(i) instanceof TerrainQuad) { ((TerrainQuad) children.get(i)).findPick(toTest, results); } } } } }
Example #11
Source File: TerrainQuad.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
/** * Gather the terrain patches that intersect the given ray (toTest). * This only tests the bounding boxes * @param toTest * @param results */ public void findPick(Ray toTest, List<TerrainPickData> results) { if (getWorldBound() != null) { if (getWorldBound().intersects(toTest)) { // further checking needed. for (int i = 0; i < getQuantity(); i++) { if (children.get(i) instanceof TerrainPatch) { TerrainPatch tp = (TerrainPatch) children.get(i); tp.ensurePositiveVolumeBBox(); if (tp.getWorldBound().intersects(toTest)) { CollisionResults cr = new CollisionResults(); toTest.collideWith(tp.getWorldBound(), cr); if (cr != null && cr.getClosestCollision() != null) { cr.getClosestCollision().getDistance(); results.add(new TerrainPickData(tp, cr.getClosestCollision())); } } } else if (children.get(i) instanceof TerrainQuad) { ((TerrainQuad) children.get(i)).findPick(toTest, results); } } } } }
Example #12
Source File: TerrainQuad.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
@Override public int collideWith(Collidable other, CollisionResults results){ int total = 0; if (other instanceof Ray) return collideWithRay((Ray)other, results); // if it didn't collide with this bbox, return if (other instanceof BoundingVolume) if (!this.getWorldBound().intersects((BoundingVolume)other)) return total; for (Spatial child : children){ total += child.collideWith(other, results); } return total; }
Example #13
Source File: TerrainQuad.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
private int collideWithRay(Ray ray, CollisionResults results) { if (picker == null) picker = new BresenhamTerrainPicker(this); Vector3f intersection = picker.getTerrainIntersection(ray, results); if (intersection != null) { if (ray.getLimit() < Float.POSITIVE_INFINITY) { if (results.getClosestCollision().getDistance() <= ray.getLimit()) return 1; // in range else return 0; // out of range } else return 1; } else return 0; }
Example #14
Source File: TerrainPatch.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
@Override public int collideWith(Collidable other, CollisionResults results) throws UnsupportedCollisionException { if (refreshFlags != 0) throw new IllegalStateException("Scene graph must be updated" + " before checking collision"); if (other instanceof BoundingVolume) if (!getWorldBound().intersects((BoundingVolume)other)) return 0; if(other instanceof Ray) return collideWithRay((Ray)other, results); else if (other instanceof BoundingVolume) return collideWithBoundingVolume((BoundingVolume)other, results); else { throw new UnsupportedCollisionException("TerrainPatch cannnot collide with "+other.getClass().getName()); } }
Example #15
Source File: BoundingSphere.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public int collideWith(Collidable other, CollisionResults results) { if (other instanceof Ray) { Ray ray = (Ray) other; return collideWithRay(ray, results); } else if (other instanceof Triangle){ Triangle t = (Triangle) other; return collideWithTri(t, results); } else if (other instanceof BoundingVolume) { if (intersects((BoundingVolume)other)) { CollisionResult result = new CollisionResult(); results.addCollision(result); return 1; } return 0; } else if (other instanceof Spatial) { return other.collideWith(this, results); } else { throw new UnsupportedCollisionException(); } }
Example #16
Source File: TestObjGroupsLoading.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void simpleUpdate(final float tpf) { // ray to the center of the screen from the camera Ray ray = new Ray(cam.getLocation(), cam.getDirection()); // find object at the center of the screen final CollisionResults results = new CollisionResults(); rootNode.collideWith(ray, results); CollisionResult result = results.getClosestCollision(); if (result == null) { pointerDisplay.setText(""); } else { // display pointed geometry and it's parents names StringBuilder sb = new StringBuilder(); for (Spatial node = result.getGeometry(); node != null; node = node.getParent()) { if (sb.length() > 0) { sb.append(" < "); } sb.append(node.getName()); } pointerDisplay.setText(sb); } }
Example #17
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 #18
Source File: Geometry.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public int collideWith(Collidable other, CollisionResults results) { // Force bound to update checkDoBoundUpdate(); // Update transform, and compute cached world matrix computeWorldMatrix(); assert (refreshFlags & (RF_BOUND | RF_TRANSFORM)) == 0; if (mesh != null) { // NOTE: BIHTree in mesh already checks collision with the // mesh's bound int prevSize = results.size(); int added = mesh.collideWith(other, cachedWorldMat, worldBound, results); int newSize = results.size(); for (int i = prevSize; i < newSize; i++) { results.getCollisionDirect(i).setGeometry(this); } return added; } return 0; }
Example #19
Source File: BIHTree.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public int collideWith(Collidable other, Matrix4f worldMatrix, BoundingVolume worldBound, CollisionResults results) { if (other instanceof Ray) { Ray ray = (Ray) other; return collideWithRay(ray, worldMatrix, worldBound, results); } else if (other instanceof BoundingVolume) { BoundingVolume bv = (BoundingVolume) other; return collideWithBoundingVolume(bv, worldMatrix, results); } else { throw new UnsupportedCollisionException("Collidable:" + other); } }
Example #20
Source File: Ray.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public int collideWith(Collidable other, CollisionResults results) { if (other instanceof BoundingVolume) { BoundingVolume bv = (BoundingVolume) other; return bv.collideWith(this, results); } else if (other instanceof AbstractTriangle) { AbstractTriangle tri = (AbstractTriangle) other; float d = intersects(tri.get1(), tri.get2(), tri.get3()); if (Float.isInfinite(d) || Float.isNaN(d)) { return 0; } Vector3f point = new Vector3f(direction).multLocal(d).addLocal(origin); results.addCollision(new CollisionResult(point, d)); return 1; } else { throw new UnsupportedCollisionException(); } }
Example #21
Source File: BoundingSphere.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
public int collideWith(Collidable other, CollisionResults results) { if (other instanceof Ray) { Ray ray = (Ray) other; return collideWithRay(ray, results); } else if (other instanceof Triangle){ Triangle t = (Triangle) other; float r2 = radius * radius; float d1 = center.distanceSquared(t.get1()); float d2 = center.distanceSquared(t.get2()); float d3 = center.distanceSquared(t.get3()); if (d1 <= r2 || d2 <= r2 || d3 <= r2) { CollisionResult r = new CollisionResult(); r.setDistance(FastMath.sqrt(Math.min(Math.min(d1, d2), d3)) - radius); results.addCollision(r); return 1; } return 0; } else { throw new UnsupportedCollisionException(); } }
Example #22
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 #23
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 #24
Source File: BIHTree.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
private int collideWithRay(Ray r, Matrix4f worldMatrix, BoundingVolume worldBound, CollisionResults results) { 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()); } // return root.intersectBrute(r, worldMatrix, this, tMin, tMax, results); return root.intersectWhere(r, worldMatrix, this, tMin, tMax, results); } return 0; }
Example #25
Source File: BIHTree.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
public int collideWith(Collidable other, Matrix4f worldMatrix, BoundingVolume worldBound, CollisionResults results) { if (other instanceof Ray) { Ray ray = (Ray) other; return collideWithRay(ray, worldMatrix, worldBound, results); } else if (other instanceof BoundingVolume) { BoundingVolume bv = (BoundingVolume) other; return collideWithBoundingVolume(bv, worldMatrix, results); } else { throw new UnsupportedCollisionException(); } }
Example #26
Source File: BoundingVolume.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
public int collideWith(Collidable other) { TempVars tempVars = TempVars.get(); try { CollisionResults tempResults = tempVars.collisionResults; tempResults.clear(); return collideWith(other, tempResults); } finally { tempVars.release(); } }
Example #27
Source File: Mesh.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Handles collision detection, internal use only. * User code should only use collideWith() on scene * graph elements such as {@link Spatial}s. */ public int collideWith(Collidable other, Matrix4f worldMatrix, BoundingVolume worldBound, CollisionResults results) { switch (mode) { case Points: case Lines: case LineStrip: case LineLoop: /* * Collisions can be detected only with triangles, * and there are no triangles in this mesh. */ return 0; } if (getVertexCount() == 0) { return 0; } if (collisionTree == null) { createCollisionData(); } return collisionTree.collideWith(other, worldMatrix, worldBound, results); }
Example #28
Source File: Node.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
public int collideWith(Collidable other, CollisionResults results){ int total = 0; for (Spatial child : children.getArray()){ total += child.collideWith(other, results); } return total; }
Example #29
Source File: BatchNode.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public int collideWith(Collidable other, CollisionResults results) { int total = 0; for (Spatial child : children.getArray()) { if (!isBatch(child)) { total += child.collideWith(other, results); } } return total; }
Example #30
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(); }