use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.
the class TerrainTestModifyHeight method getWorldIntersection.
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;
}
use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.
the class EntropyComputeUtil method computeLodEntropy.
public static float computeLodEntropy(Mesh terrainBlock, Buffer lodIndices) {
// Bounding box for the terrain block
BoundingBox bbox = (BoundingBox) terrainBlock.getBound();
// Vertex positions for the block
FloatBuffer positions = terrainBlock.getFloatBuffer(Type.Position);
// Prepare to cast rays
Vector3f pos = new Vector3f();
Vector3f dir = new Vector3f(0, -1, 0);
Ray ray = new Ray(pos, dir);
// Prepare collision results
CollisionResults results = new CollisionResults();
// Set the LOD indices on the block
VertexBuffer originalIndices = terrainBlock.getBuffer(Type.Index);
terrainBlock.clearBuffer(Type.Index);
if (lodIndices instanceof IntBuffer)
terrainBlock.setBuffer(Type.Index, 3, (IntBuffer) lodIndices);
else if (lodIndices instanceof ShortBuffer) {
terrainBlock.setBuffer(Type.Index, 3, (ShortBuffer) lodIndices);
}
// Recalculate collision mesh
terrainBlock.createCollisionData();
float entropy = 0;
for (int i = 0; i < positions.limit() / 3; i++) {
BufferUtils.populateFromBuffer(pos, positions, i);
float realHeight = pos.y;
pos.addLocal(0, bbox.getYExtent(), 0);
ray.setOrigin(pos);
results.clear();
terrainBlock.collideWith(ray, Matrix4f.IDENTITY, bbox, results);
if (results.size() > 0) {
Vector3f contactPoint = results.getClosestCollision().getContactPoint();
float delta = Math.abs(realHeight - contactPoint.y);
entropy = Math.max(delta, entropy);
}
}
// Restore original indices
terrainBlock.clearBuffer(Type.Index);
terrainBlock.setBuffer(originalIndices);
return entropy;
}
use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.
the class TestTriangleCollision method simpleUpdate.
@Override
public void simpleUpdate(float tpf) {
CollisionResults results = new CollisionResults();
BoundingVolume bv = geom1.getWorldBound();
golem.collideWith(bv, results);
if (results.size() > 0) {
geom1.getMaterial().setColor("Color", ColorRGBA.Red);
} else {
geom1.getMaterial().setColor("Color", ColorRGBA.Blue);
}
}
use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.
the class TestMousePick method simpleUpdate.
@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);
// }
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);
}
}
use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.
the class CollisionUtil method checkCollisionBase.
private static void checkCollisionBase(Collidable a, Collidable b, int expected) {
// Test bounding volume methods
if (a instanceof BoundingVolume && b instanceof BoundingVolume) {
BoundingVolume bv1 = (BoundingVolume) a;
BoundingVolume bv2 = (BoundingVolume) b;
assert bv1.intersects(bv2) == (expected != 0);
}
// Test standard collideWith method
CollisionResults results = new CollisionResults();
int numCollisions = a.collideWith(b, results);
assert results.size() == numCollisions;
assert numCollisions == expected;
// force the results to be sorted here..
results.getClosestCollision();
if (results.size() > 0) {
assert results.getCollision(0) == results.getClosestCollision();
}
if (results.size() == 1) {
assert results.getClosestCollision() == results.getFarthestCollision();
}
}
Aggregations