use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.
the class TerrainQuad method findPick.
/**
* 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);
}
}
}
}
}
use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.
the class BresenhamTerrainPicker method getTerrainIntersection.
public Vector3f getTerrainIntersection(Ray worldPick, CollisionResults results) {
worldPickRay.set(worldPick);
List<TerrainPickData> pickData = new ArrayList<TerrainPickData>();
root.findPick(worldPick.clone(), pickData);
Collections.sort(pickData);
if (pickData.isEmpty())
return null;
workRay.set(worldPick);
for (TerrainPickData pd : pickData) {
TerrainPatch patch = pd.targetPatch;
tracer.getGridSpacing().set(patch.getWorldScale());
tracer.setGridOrigin(patch.getWorldTranslation());
workRay.getOrigin().set(worldPick.getDirection()).multLocal(pd.cr.getDistance() - .1f).addLocal(worldPick.getOrigin());
tracer.startWalk(workRay);
final Vector3f intersection = new Vector3f();
final Vector2f loc = tracer.getGridLocation();
if (tracer.isRayPerpendicularToGrid()) {
Triangle hit = new Triangle();
checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit);
float distance = worldPickRay.origin.distance(intersection);
CollisionResult cr = new CollisionResult(intersection, distance);
cr.setGeometry(patch);
cr.setContactNormal(hit.getNormal());
results.addCollision(cr);
return intersection;
}
while (loc.x >= -1 && loc.x <= patch.getSize() && loc.y >= -1 && loc.y <= patch.getSize()) {
//System.out.print(loc.x+","+loc.y+" : ");
// check the triangles of main square for intersection.
Triangle hit = new Triangle();
if (checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit)) {
// we found an intersection, so return that!
float distance = worldPickRay.origin.distance(intersection);
CollisionResult cr = new CollisionResult(intersection, distance);
cr.setGeometry(patch);
results.addCollision(cr);
cr.setContactNormal(hit.getNormal());
return intersection;
}
// because of how we get our height coords, we will
// sometimes be off by a grid spot, so we check the next
// grid space up.
int dx = 0, dz = 0;
Direction d = tracer.getLastStepDirection();
switch(d) {
case PositiveX:
case NegativeX:
dx = 0;
dz = 1;
break;
case PositiveZ:
case NegativeZ:
dx = 1;
dz = 0;
break;
}
if (checkTriangles(loc.x + dx, loc.y + dz, workRay, intersection, patch, hit)) {
// we found an intersection, so return that!
float distance = worldPickRay.origin.distance(intersection);
CollisionResult cr = new CollisionResult(intersection, distance);
results.addCollision(cr);
cr.setGeometry(patch);
cr.setContactNormal(hit.getNormal());
return intersection;
}
tracer.next();
}
}
return null;
}
Aggregations