com.jme3.collision.CollisionResult Java Examples
The following examples show how to use
com.jme3.collision.CollisionResult.
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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #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: 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 #10
Source File: GeomUtils.java From jmonkeybuilder with Apache License 2.0 | 6 votes |
/** * Get a collision on spatial from screen position. * * @param spatial the spatial. * @param camera the camera. * @param screenX the screen X coord. * @param screenY the screen Y coord. * @return the collision or null. */ @FromAnyThread public static @Nullable CollisionResult getCollisionFromScreenPos( @NotNull Spatial spatial, @NotNull Camera camera, float screenX, float screenY ) { var results = getCollisionsFromScreenPos(spatial, camera, screenX, screenY); if (results.size() < 1) { return null; } return results.getClosestCollision(); }
Example #11
Source File: AbstractTransformControl.java From jmonkeybuilder with Apache License 2.0 | 6 votes |
@Override @JmeThread public void setCollisionPlane(@NotNull final CollisionResult collisionResult) { final EditorTransformSupport editorControl = getEditorControl(); final Transform transform = editorControl.getTransformCenter(); if (transform == null) { LOGGER.warning(this, "not found transform center for the " + editorControl); return; } detectPickedAxis(editorControl, collisionResult); // set the collision Plane location and rotation final Node collisionPlane = getCollisionPlane(); collisionPlane.setLocalTranslation(transform.getTranslation()); collisionPlane.setLocalRotation(Quaternion.IDENTITY); }
Example #12
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 #13
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 #14
Source File: TerrainTestModifyHeight.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
private Vector3f getWorldIntersection() { Vector3f origin = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.0f); Vector3f direction = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.3f); direction.subtractLocal(origin).normalizeLocal(); Ray ray = new Ray(origin, direction); CollisionResults results = new CollisionResults(); int numCollisions = terrain.collideWith(ray, results); if (numCollisions > 0) { CollisionResult hit = results.getClosestCollision(); return hit.getContactPoint(); } return null; }
Example #15
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 #16
Source File: TestDepthOfField.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void simpleUpdate(float tpf) { Vector3f origin = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.0f); Vector3f direction = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.3f); direction.subtractLocal(origin).normalizeLocal(); Ray ray = new Ray(origin, direction); CollisionResults results = new CollisionResults(); int numCollisions = terrain.collideWith(ray, results); if (numCollisions > 0) { CollisionResult hit = results.getClosestCollision(); fpsText.setText(""+hit.getDistance()); dofFilter.setFocusDistance(hit.getDistance()/10.0f); } }
Example #17
Source File: SceneEditTool.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
private CollisionResult pick(Camera cam, Vector2f mouseLoc, Node node) { CollisionResults results = new CollisionResults(); Ray ray = new Ray(); Vector3f pos = cam.getWorldCoordinates(mouseLoc, 0).clone(); Vector3f dir = cam.getWorldCoordinates(mouseLoc, 0.1f).clone(); dir.subtractLocal(pos).normalizeLocal(); ray.setOrigin(pos); ray.setDirection(dir); node.collideWith(ray, results); CollisionResult result = results.getClosestCollision(); return result; }
Example #18
Source File: TestJoystick.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 5 votes |
private void pickGamePad(Vector2f mouseLoc){ if (lastButton != null) { CollisionResults cresults = pick(cam, mouseLoc, gamepad); for (CollisionResult cr : cresults) { Node n = cr.getGeometry().getParent(); if (n != null && (n instanceof ButtonView)) { String b = n.getName().substring("Button:".length()); String name = lastButton.getJoystick().getName().replaceAll(" ", "\\\\ "); String id = lastButton.getLogicalId().replaceAll(" ", "\\\\ "); System.out.println(name + "." + id + "=" + b); return; } } } }
Example #19
Source File: SceneEditTool.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
protected Vector3f pickWorldLocation(Camera cam, Vector2f mouseLoc, Node rootNode) { CollisionResult cr = pick(cam, mouseLoc, rootNode); if (cr != null) { return cr.getContactPoint(); } else { return null; } }
Example #20
Source File: PointUtil.java From OpenRTS with MIT License | 5 votes |
public static Geometry getPointedGeometry(Node n, Ray r) { CollisionResult collision = getCollision(n, r); if (collision == null) { return null; } return collision.getGeometry(); }
Example #21
Source File: PointUtil.java From OpenRTS with MIT License | 5 votes |
public static Point2D getPointedCoord(Node n, Ray r) { CollisionResult collision = getCollision(n, r); if (collision == null) { return null; } // return Translator.toPoint2D(collision.getContactPoint()); Vector3f p = collision.getContactPoint(); return new Point2D(p.x, p.y); }
Example #22
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 #23
Source File: SceneEditTool.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
/** * Given the mouse coordinates, pick the geometry that is closest to the camera. * @param jmeRootNode to pick from * @return the selected spatial, or null if nothing */ protected Spatial pickWorldSpatial(Camera cam, Vector2f mouseLoc, JmeNode jmeRootNode) { Node rootNode = jmeRootNode.getLookup().lookup(Node.class); CollisionResult cr = pick(cam, mouseLoc, rootNode); if (cr != null) { return cr.getGeometry(); } else { return null; } }
Example #24
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 #25
Source File: TerrainTestModifyHeight.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
private Vector3f getWorldIntersection() { Vector3f origin = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.0f); Vector3f direction = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.3f); direction.subtractLocal(origin).normalizeLocal(); Ray ray = new Ray(origin, direction); CollisionResults results = new CollisionResults(); int numCollisions = terrain.collideWith(ray, results); if (numCollisions > 0) { CollisionResult hit = results.getClosestCollision(); return hit.getContactPoint(); } return null; }
Example #26
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 #27
Source File: TestDepthOfField.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
@Override public void simpleUpdate(float tpf) { Vector3f origin = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.0f); Vector3f direction = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.3f); direction.subtractLocal(origin).normalizeLocal(); Ray ray = new Ray(origin, direction); CollisionResults results = new CollisionResults(); int numCollisions = terrain.collideWith(ray, results); if (numCollisions > 0) { CollisionResult hit = results.getClosestCollision(); fpsText.setText(""+hit.getDistance()); dofFilter.setFocusDistance(hit.getDistance()/10.0f); } }
Example #28
Source File: SelectionState.java From Lemur with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void update( float tpf ) { super.update(tpf); long time = System.nanoTime(); if( time - lastSample < sampleFrequency ) return; lastSample = time; Vector2f cursor = getApplication().getInputManager().getCursorPosition(); CollisionResults collisions = getCollisions(cursor); for( CollisionResult cr : collisions ) { Geometry geom = cr.getGeometry(); if( geom == selected ) { // If the hover is already the selection then // don't bother changinge if( geom == hover ) { return; } } if( isIgnored(geom) ) { continue; } setHover(geom); return; } // Else clear the hover setHover(null); }
Example #29
Source File: SphereWanderBehavior.java From MonkeyBrains with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Metod for changing target position. * * @param tpf time per frame */ protected void changeTargetPosition(float tpf) { time -= tpf; Vector3f forward; if (this.agent.getVelocity() != null) { forward = this.agent.getVelocity().normalize(); } else { forward = this.agent.fordwardVector(); } if (forward.equals(Vector3f.UNIT_Y)) { forward = forward.add(new Vector3f(0, 0, SphereWanderBehavior.SIDE_REFERENCE_OFFSET)); } //Update sphere position this.wanderSphere.setCenter(this.agent.getLocalTranslation().add(forward.mult(SphereWanderBehavior.OFFSET_DISTANCE + this.agent.getRadius() + this.sphereRadius))); if (time <= 0) { this.calculateNewRandomDir(); time = timeInterval; } Vector3f sideVector = forward.cross(Vector3f.UNIT_Y).normalize(); Vector3f rayDir = (this.agent.offset(wanderSphere.getCenter())).add(sideVector.mult(this.randomDirection.x));//.add(Vector3f.UNIT_Y.mult(this.randomDirection.y)); Ray ray = new Ray(this.agent.getLocalTranslation(), rayDir); CollisionResults results = new CollisionResults(); this.wanderSphere.collideWith(ray, results); CollisionResult collisionResult = results.getCollision(1); //The collision with the second hemisphere this.targetPosition = collisionResult.getContactPoint(); }
Example #30
Source File: CursorButtonEvent.java From Lemur with BSD 3-Clause "New" or "Revised" License | 5 votes |
public CursorButtonEvent( int buttonIndex, boolean pressed, ViewPort view, Spatial target, float x, float y, CollisionResult collision ) { super(view, target, x, y, collision); this.buttonIndex = buttonIndex; this.pressed = pressed; }