use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.
the class TestDepthOfField method simpleUpdate.
@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);
}
}
use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.
the class BIHTree method collideWithRay.
private int collideWithRay(Ray r, Matrix4f worldMatrix, BoundingVolume worldBound, CollisionResults results) {
TempVars vars = TempVars.get();
try {
CollisionResults boundResults = vars.collisionResults;
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());
if (tMin > tMax) {
return 0;
}
}
// return root.intersectBrute(r, worldMatrix, this, tMin, tMax, results);
return root.intersectWhere(r, worldMatrix, this, tMin, tMax, results);
}
return 0;
} finally {
vars.release();
}
}
use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.
the class BIHNode method intersectWhere.
public final int intersectWhere(Collidable col, BoundingBox box, Matrix4f worldMatrix, BIHTree tree, CollisionResults results) {
TempVars vars = TempVars.get();
ArrayList<BIHStackData> stack = vars.bihStack;
stack.clear();
float[] minExts = { box.getCenter().x - box.getXExtent(), box.getCenter().y - box.getYExtent(), box.getCenter().z - box.getZExtent() };
float[] maxExts = { box.getCenter().x + box.getXExtent(), box.getCenter().y + box.getYExtent(), box.getCenter().z + box.getZExtent() };
stack.add(new BIHStackData(this, 0, 0));
Triangle t = new Triangle();
int cols = 0;
stackloop: while (stack.size() > 0) {
BIHNode node = stack.remove(stack.size() - 1).node;
while (node.axis != 3) {
int a = node.axis;
float maxExt = maxExts[a];
float minExt = minExts[a];
if (node.leftPlane < node.rightPlane) {
// if the box is in that gap, we stop there
if (minExt > node.leftPlane && maxExt < node.rightPlane) {
continue stackloop;
}
}
if (maxExt < node.rightPlane) {
node = node.left;
} else if (minExt > node.leftPlane) {
node = node.right;
} else {
stack.add(new BIHStackData(node.right, 0, 0));
node = node.left;
}
// if (maxExt < node.leftPlane
// && maxExt < node.rightPlane){
// node = node.left;
// }else if (minExt > node.leftPlane
// && minExt > node.rightPlane){
// node = node.right;
// }else{
// }
}
for (int i = node.leftIndex; i <= node.rightIndex; i++) {
tree.getTriangle(i, t.get1(), t.get2(), t.get3());
if (worldMatrix != null) {
worldMatrix.mult(t.get1(), t.get1());
worldMatrix.mult(t.get2(), t.get2());
worldMatrix.mult(t.get3(), t.get3());
}
int added = col.collideWith(t, results);
if (added > 0) {
int index = tree.getTriangleIndex(i);
int start = results.size() - added;
for (int j = start; j < results.size(); j++) {
CollisionResult cr = results.getCollisionDirect(j);
cr.setTriangleIndex(index);
}
cols += added;
}
}
}
vars.release();
return cols;
}
use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.
the class BIHTree method collideWithBoundingVolume.
private int collideWithBoundingVolume(BoundingVolume bv, Matrix4f worldMatrix, CollisionResults results) {
BoundingBox bbox;
if (bv instanceof BoundingSphere) {
BoundingSphere sphere = (BoundingSphere) bv;
bbox = new BoundingBox(bv.getCenter().clone(), sphere.getRadius(), sphere.getRadius(), sphere.getRadius());
} else if (bv instanceof BoundingBox) {
bbox = new BoundingBox((BoundingBox) bv);
} else {
throw new UnsupportedCollisionException("BoundingVolume:" + bv);
}
bbox.transform(worldMatrix.invert(), bbox);
return root.intersectWhere(bv, bbox, worldMatrix, this, results);
}
use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.
the class TestRayCollision method main.
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);
}
}
Aggregations