private static float calcScreenArea(BoundingSphere bound, float distance, float screenWidth) { // Where is the center point and a radius point that lies in a plan parallel to the view plane? // // Calc radius based on these two points and plug into circle area formula. // Vector2f centerSP = null; // Vector2f outerSP = null; // float radiusSq = centerSP.subtract(outerSP).lengthSquared(); float radius = (bound.getRadius() * screenWidth) / (distance * 2); return radius * radius * FastMath.PI; }
float temp_radius = sphere.getRadius(); Vector3f temp_center = sphere.center; BoundingSphere rVal = new BoundingSphere(); return merge(temp_radius, temp_center, rVal); box.zExtent); Vector3f temp_center = box.center; BoundingSphere rVal = new BoundingSphere(); return merge(radVect.length(), temp_center, rVal);
/** * This method returns the bounding box of the given geometries. * * @param geometries * the list of geometries * @return bounding box of the given geometries */ public static BoundingBox getBoundingBox(Geometry... geometries) { BoundingBox result = null; for (Geometry geometry : geometries) { geometry.updateModelBound(); BoundingVolume bv = geometry.getModelBound(); if (bv instanceof BoundingBox) { return (BoundingBox) bv; } else if (bv instanceof BoundingSphere) { BoundingSphere bs = (BoundingSphere) bv; float r = bs.getRadius(); return new BoundingBox(bs.getCenter(), r, r, r); } else { throw new IllegalStateException("Unknown bounding volume type: " + bv.getClass().getName()); } } return result; }
@Override public boolean intersectsSphere(BoundingSphere sphere, TempVars vars) { Vector3f closestPoint = getClosestPoint(vars, sphere.getCenter()); // check if the point intersects with the sphere bound if (sphere.intersects(closestPoint)) { return true; } return false; }
@Override public int collideWith(Collidable other) { if (other instanceof Ray) { Ray ray = (Ray) other; return collideWithRay(ray); } else if (other instanceof Triangle){ return super.collideWith(other); } else if (other instanceof BoundingVolume) { return intersects((BoundingVolume)other) ? 1 : 0; } else { throw new UnsupportedCollisionException(); } }
/** * returns the bounding volume of this LightProbe * @return a bounding volume. * @deprecated use {@link LightProbe#getArea()} */ @Deprecated public BoundingVolume getBounds() { return new BoundingSphere(((SphereProbeArea)area).getRadius(), ((SphereProbeArea)area).getCenter()); }
private void build() { BoundingSphere bs = new BoundingSphere(); bs.computeFromPoints(mesh.getFloatBuffer(VertexBuffer.Type.Position)); meshBoundingSphereRadius = bs.getRadius(); List<Vertex> vertexLookup = new ArrayList<Vertex>(); initialize(); gatherVertexData(mesh, vertexLookup); gatherIndexData(mesh, vertexLookup); computeCosts(); // assert (assertValidMesh()); }
public BoundingVolume transform(Matrix4f trans, BoundingVolume store) { BoundingSphere sphere; if (store == null || store.getType() != BoundingVolume.Type.Sphere) { sphere = new BoundingSphere(1, new Vector3f(0, 0, 0)); } else { sphere = (BoundingSphere) store; } trans.mult(center, sphere.center); Vector3f axes = new Vector3f(1, 1, 1); trans.mult(axes, axes); float ax = getMaxAxis(axes); sphere.radius = FastMath.abs(ax * radius) + RADIUS_EPSILON - 1f; return sphere; }
@Override public void read(JmeImporter im) throws IOException { super.read(im); InputCapsule ic = im.getCapsule(this); prefilteredEnvMap = (TextureCubeMap) ic.readSavable("prefilteredEnvMap", null); position = (Vector3f) ic.readSavable("position", null); area = (ProbeArea)ic.readSavable("area", null); if(area == null) { // retro compat BoundingSphere bounds = (BoundingSphere) ic.readSavable("bounds", new BoundingSphere(1.0f, Vector3f.ZERO)); area = new SphereProbeArea(bounds.getCenter(), bounds.getRadius()); } area.setCenter(position); nbMipMaps = ic.readInt("nbMipMaps", 0); ready = ic.readBoolean("ready", false); Savable[] coeffs = ic.readSavableArray("shCoeffs", null); if (coeffs == null) { ready = false; logger.log(Level.WARNING, "LightProbe is missing parameters, it should be recomputed. Please use lightProbeFactory.updateProbe()"); } else { shCoeffs = new Vector3f[coeffs.length]; for (int i = 0; i < coeffs.length; i++) { shCoeffs[i] = (Vector3f) coeffs[i]; } } }
@Test public void testBoxSphereCollision() { BoundingBox box1 = new BoundingBox(Vector3f.ZERO, 1, 1, 1); BoundingSphere sphere2 = new BoundingSphere(1, Vector3f.ZERO); checkCollision(box1, sphere2, 1); // Put it at the very edge - for sphere vs. box, it will not intersect sphere2.setCenter(new Vector3f(2f, 0f, 0f)); checkCollision(box1, sphere2, 0); // Put it a wee bit closer - should intersect. sphere2.setCenter(new Vector3f(2f - FastMath.ZERO_TOLERANCE, 0, 0)); checkCollision(box1, sphere2, 1); // Test if the algorithm converts the sphere // to a box before testing the collision (incorrect) float sqrt3 = FastMath.sqrt(3); sphere2.setCenter(Vector3f.UNIT_XYZ.mult(2)); sphere2.setRadius(sqrt3); checkCollision(box1, sphere2, 0); // Make it a wee bit larger. sphere2.setRadius(sqrt3 + FastMath.ZERO_TOLERANCE); checkCollision(box1, sphere2, 1); }
@Test public void testSphereSphereCollision() { BoundingSphere sphere1 = new BoundingSphere(1, Vector3f.ZERO); BoundingSphere sphere2 = new BoundingSphere(1, Vector3f.ZERO); checkCollision(sphere1, sphere2, 1); // Put it at the very edge - should still intersect. sphere2.setCenter(new Vector3f(2f, 0f, 0f)); checkCollision(sphere1, sphere2, 1); // Put it a wee bit farther - no intersection expected sphere2.setCenter(new Vector3f(2f + FastMath.ZERO_TOLERANCE, 0, 0)); checkCollision(sphere1, sphere2, 0); }
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 ((Spatial)other).collideWith(this, results); } else { throw new UnsupportedCollisionException(); } }
/** * <code>computeFromPoints</code> creates a new Bounding Sphere from a * given set of points. It uses the <code>calcWelzl</code> method as * default. * * @param points * the points to contain. */ public void computeFromPoints(FloatBuffer points) { calcWelzl(points); }
/** * <code>computeFromTris</code> creates a new Bounding Box from a given * set of triangles. It is used in OBBTree calculations. * * @param tris * @param start * @param end */ public void computeFromTris(Triangle[] tris, int start, int end) { if (end - start <= 0) { return; } Vector3f[] vertList = new Vector3f[(end - start) * 3]; int count = 0; for (int i = start; i < end; i++) { vertList[count++] = tris[i].get(0); vertList[count++] = tris[i].get(1); vertList[count++] = tris[i].get(2); } averagePoints(vertList); } //
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(); } }
/** * This method returns the bounding sphere of the given geometries. * * @param geometries * the list of geometries * @return bounding sphere of the given geometries */ /* package */static BoundingSphere getBoundingSphere(Geometry... geometries) { BoundingSphere result = null; for (Geometry geometry : geometries) { geometry.updateModelBound(); BoundingVolume bv = geometry.getModelBound(); if (bv instanceof BoundingBox) { BoundingBox bb = (BoundingBox) bv; float r = Math.max(bb.getXExtent(), bb.getYExtent()); r = Math.max(r, bb.getZExtent()); return new BoundingSphere(r, bb.getCenter()); } else if (bv instanceof BoundingSphere) { return (BoundingSphere) bv; } else { throw new IllegalStateException("Unknown bounding volume type: " + bv.getClass().getName()); } } return result; }
private void build() { BoundingSphere bs = new BoundingSphere(); bs.computeFromPoints(mesh.getFloatBuffer(VertexBuffer.Type.Position)); meshBoundingSphereRadius = bs.getRadius(); List<Vertex> vertexLookup = new ArrayList<Vertex>(); initialize(); gatherVertexData(mesh, vertexLookup); gatherIndexData(mesh, vertexLookup); computeCosts(); // assert (assertValidMesh()); }