@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()); } }
/** * Find what terrain patches need normal recalculations and update * their normals; */ protected void fixNormals(BoundingBox affectedArea) { if (children == null) return; // go through the children and see if they collide with the affectedAreaBBox // if they do, then update their normals for (int x = children.size(); --x >= 0;) { Spatial child = children.get(x); if (child instanceof TerrainQuad) { if (affectedArea != null && affectedArea.intersects(((TerrainQuad) child).getWorldBound()) ) ((TerrainQuad) child).fixNormals(affectedArea); } else if (child instanceof TerrainPatch) { if (affectedArea != null && affectedArea.intersects(((TerrainPatch) child).getWorldBound()) ) ((TerrainPatch) child).updateNormals(); // recalculate the patch's normals } } }
/** * 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); } } } } }
((TerrainQuad) child).fixNormalEdges(affectedArea); } else if (child instanceof TerrainPatch) { if (affectedArea != null && !affectedArea.intersects(((TerrainPatch) child).getWorldBound()) ) // if doesn't intersect, continue continue;
@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()); } }
/** * Find what terrain patches need normal recalculations and update * their normals; */ protected void fixNormals(BoundingBox affectedArea) { if (children == null) return; // go through the children and see if they collide with the affectedAreaBBox // if they do, then update their normals for (int x = children.size(); --x >= 0;) { Spatial child = children.get(x); if (child instanceof TerrainQuad) { if (affectedArea != null && affectedArea.intersects(((TerrainQuad) child).getWorldBound()) ) ((TerrainQuad) child).fixNormals(affectedArea); } else if (child instanceof TerrainPatch) { if (affectedArea != null && affectedArea.intersects(((TerrainPatch) child).getWorldBound()) ) ((TerrainPatch) child).updateNormals(); // recalculate the patch's normals } } }
/** * 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); } } } } }
((TerrainQuad) child).fixNormalEdges(affectedArea); } else if (child instanceof TerrainPatch) { if (affectedArea != null && !affectedArea.intersects(((TerrainPatch) child).getWorldBound()) ) // if doesn't intersect, continue continue;