use of com.jme3.scene.debug.Grid in project jmonkeyengine by jMonkeyEngine.
the class TerrainGridLodControl method updateLOD.
@Override
protected void updateLOD(List<Vector3f> locations, LodCalculator lodCalculator) {
TerrainGrid terrainGrid = (TerrainGrid) getSpatial();
// for now, only the first camera is handled.
// to accept more, there are two ways:
// 1: every camera has an associated grid, then the location is not enough to identify which camera location has changed
// 2: grids are associated with locations, and no incremental update is done, we load new grids for new locations, and unload those that are not needed anymore
Vector3f cam = locations.isEmpty() ? Vector3f.ZERO.clone() : locations.get(0);
// get the grid index value of where the camera is (ie. 2,1)
Vector3f camCell = terrainGrid.getCamCell(cam);
if (terrainGrid.cellsLoaded > 1) {
// Check if cells are updated before updating gridoffset.
terrainGrid.gridOffset[0] = Math.round(camCell.x * (terrainGrid.size / 2));
terrainGrid.gridOffset[1] = Math.round(camCell.z * (terrainGrid.size / 2));
terrainGrid.cellsLoaded = 0;
}
if (camCell.x != terrainGrid.currentCamCell.x || camCell.z != terrainGrid.currentCamCell.z || !terrainGrid.runOnce) {
// if the camera has moved into a new cell, load new terrain into the visible 4 center quads
terrainGrid.updateChildren(camCell);
for (TerrainGridListener l : terrainGrid.listeners) {
l.gridMoved(camCell);
}
}
terrainGrid.runOnce = true;
super.updateLOD(locations, lodCalculator);
}
use of com.jme3.scene.debug.Grid in project jmonkeyengine by jMonkeyEngine.
the class AssetTileLoader method getTerrainQuadAt.
public TerrainQuad getTerrainQuadAt(Vector3f location) {
String modelName = assetPath + "/" + name + "_" + Math.round(location.x) + "_" + Math.round(location.y) + "_" + Math.round(location.z) + ".j3o";
Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Load terrain grid tile: {0}", modelName);
TerrainQuad quad = null;
try {
quad = (TerrainQuad) manager.loadModel(modelName);
} catch (Exception e) {
// e.printStackTrace();
}
if (quad == null) {
Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Could not load terrain grid tile: {0}", modelName);
quad = createNewQuad(location);
} else {
Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Loaded terrain grid tile: {0}", modelName);
}
return quad;
}
use of com.jme3.scene.debug.Grid 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;
}
use of com.jme3.scene.debug.Grid in project jmonkeyengine by jMonkeyEngine.
the class BresenhamYUpGridTracer method startWalk.
public void startWalk(final Ray walkRay) {
// store ray
this.walkRay.set(walkRay);
// simplify access to direction
Vector3f direction = this.walkRay.getDirection();
// Move start point to grid space
Vector3f start = this.walkRay.getOrigin().subtract(gridOrigin);
gridLocation.x = (int) (start.x / gridSpacing.x);
gridLocation.y = (int) (start.z / gridSpacing.z);
Vector3f ooDirection = new Vector3f(1.0f / direction.x, 1, 1.0f / direction.z);
// Check which direction on the X world axis we are moving.
if (direction.x > TOLERANCE) {
distToNextXIntersection = ((gridLocation.x + 1) * gridSpacing.x - start.x) * ooDirection.x;
distBetweenXIntersections = gridSpacing.x * ooDirection.x;
stepXDirection = 1;
} else if (direction.x < -TOLERANCE) {
distToNextXIntersection = (start.x - (gridLocation.x * gridSpacing.x)) * -direction.x;
distBetweenXIntersections = -gridSpacing.x * ooDirection.x;
stepXDirection = -1;
} else {
distToNextXIntersection = Float.MAX_VALUE;
distBetweenXIntersections = Float.MAX_VALUE;
stepXDirection = 0;
}
// Check which direction on the Z world axis we are moving.
if (direction.z > TOLERANCE) {
distToNextZIntersection = ((gridLocation.y + 1) * gridSpacing.z - start.z) * ooDirection.z;
distBetweenZIntersections = gridSpacing.z * ooDirection.z;
stepZDirection = 1;
} else if (direction.z < -TOLERANCE) {
distToNextZIntersection = (start.z - (gridLocation.y * gridSpacing.z)) * -direction.z;
distBetweenZIntersections = -gridSpacing.z * ooDirection.z;
stepZDirection = -1;
} else {
distToNextZIntersection = Float.MAX_VALUE;
distBetweenZIntersections = Float.MAX_VALUE;
stepZDirection = 0;
}
// Reset some variables
rayLocation.set(start);
rayLength = 0.0f;
stepDirection = Direction.None;
}
Aggregations