public boolean intersectsBoundingBox(BoundingBox bb) { assert Vector3f.isValidVector(center) && Vector3f.isValidVector(bb.center); if (FastMath.abs(bb.center.x - center.x) < getRadius() + bb.xExtent && FastMath.abs(bb.center.y - center.y) < getRadius() + bb.yExtent && FastMath.abs(bb.center.z - center.z) < getRadius() + bb.zExtent) { return true; } return false; }
/* * (non-Javadoc) * @see com.jme.bounding.BoundingVolume#intersectsWhere(com.jme.math.Ray) */ public int collideWithRay(Ray ray, CollisionResults results) { Vector3f vect1 = Vector3f.newInstance(); Vector3f diff = vect1.set(ray.getOrigin()).subtractLocal(center); float a = diff.dot(diff) - (getRadius() * getRadius()); float a1, discr, root; if (a <= 0.0) { // inside sphere a1 = ray.direction.dot(diff); discr = (a1 * a1) - a; root = FastMath.sqrt(discr); float distance = root - a1; Vector3f point = new Vector3f(ray.direction).multLocal(distance).addLocal(ray.origin); CollisionResult result = new CollisionResult(point, distance); results.addCollision(result); Vector3f.recycle(vect1); return 1; } a1 = ray.direction.dot(diff); if (a1 >= 0.0) { Vector3f.recycle(vect1); return 0; } discr = a1 * a1 - a; if (discr < 0.0) { Vector3f.recycle(vect1); return 0; } else if (discr >= FastMath.ZERO_TOLERANCE) { root = FastMath.sqrt(discr); float dist = -a1 - root; Vector3f point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin); results.addCollision(new CollisionResult(point, dist)); dist = -a1 + root; point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin); results.addCollision(new CollisionResult(point, dist)); Vector3f.recycle(vect1); return 2; } else { float dist = -a1; Vector3f point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin); results.addCollision(new CollisionResult(point, dist)); Vector3f.recycle(vect1); return 1; } }
private float getMaxAxis(Vector3f scale) { float x = FastMath.abs(scale.x); float y = FastMath.abs(scale.y); float z = FastMath.abs(scale.z); if (x >= y) { if (x >= z) { return x; } return z; } if (y >= z) { return y; } return z; }
/** * Calculates the minimum bounding sphere of 2 points. Used in welzl's algorithm. * * @param O The 1st point inside the sphere. * @param A The 2nd point inside the sphere. * @see #calcWelzl(java.nio.FloatBuffer) */ private void setSphere(Vector3f O, Vector3f A) { radius = FastMath.sqrt( ((A.x - O.x) * (A.x - O.x) + (A.y - O.y) * (A.y - O.y) + (A.z - O.z) * (A.z - O.z)) / 4f) + RADIUS_EPSILON - 1f; center.interpolate(O, A, .5f); }
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; }