From d68fba1fef2ba1c402569ee99ceabeb57e974ea1 Mon Sep 17 00:00:00 2001 From: "bre..om" Date: Sat, 10 Nov 2012 20:34:35 +0000 Subject: [PATCH] fixed TerrainGrid.getTerrainAtCell() and TerrainGrid.getTerrainAt(), updated TerrainGridTileLoaderTest git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9982 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../com/jme3/terrain/geomipmap/TerrainGrid.java | 11 ++++++----- .../jme3test/terrain/TerrainGridTileLoaderTest.java | 9 +++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java b/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java index 04cc4549f..50e1f9f73 100644 --- a/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java +++ b/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java @@ -317,7 +317,8 @@ public class TerrainGrid extends TerrainQuad { public Terrain getTerrainAt(Vector3f worldLocation) { if (worldLocation == null) return null; - Vector3f tileCell = getTileCell(worldLocation); + Vector3f tileCell = getTileCell(worldLocation.setY(0)); + tileCell = new Vector3f(Math.round(tileCell.x), tileCell.y, Math.round(tileCell.z)); return cache.get(tileCell); } @@ -327,16 +328,16 @@ public class TerrainGrid extends TerrainQuad { * @return the terrain tile at that location */ public Terrain getTerrainAtCell(Vector3f cellCoordinate) { - Vector3f gridLocation = cellCoordinate.mult(getLocalScale()).multLocal(quadSize - 1); - Vector3f tileCell = getTileCell(gridLocation); - return cache.get(tileCell); + return cache.get(cellCoordinate); } /** * Convert the world location into a cell location (integer coordinates) */ public Vector3f toCellSpace(Vector3f worldLocation) { - return getTileCell(worldLocation); + Vector3f tileCell = getTileCell(worldLocation); + tileCell = new Vector3f(Math.round(tileCell.x), tileCell.y, Math.round(tileCell.z)); + return tileCell; } /** diff --git a/engine/src/test/jme3test/terrain/TerrainGridTileLoaderTest.java b/engine/src/test/jme3test/terrain/TerrainGridTileLoaderTest.java index 96f7f423e..3485e4dc8 100644 --- a/engine/src/test/jme3test/terrain/TerrainGridTileLoaderTest.java +++ b/engine/src/test/jme3test/terrain/TerrainGridTileLoaderTest.java @@ -10,11 +10,14 @@ import com.jme3.bullet.collision.shapes.HeightfieldCollisionShape; import com.jme3.bullet.control.CharacterControl; import com.jme3.bullet.control.RigidBodyControl; import com.jme3.input.KeyInput; +import com.jme3.input.MouseInput; import com.jme3.input.controls.ActionListener; import com.jme3.input.controls.KeyTrigger; +import com.jme3.input.controls.MouseButtonTrigger; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.math.Vector3f; +import com.jme3.terrain.Terrain; import com.jme3.terrain.geomipmap.TerrainGrid; import com.jme3.terrain.geomipmap.TerrainGridListener; import com.jme3.terrain.geomipmap.TerrainGridLodControl; @@ -164,11 +167,13 @@ public class TerrainGridTileLoaderTest extends SimpleApplication { this.inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W)); this.inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S)); this.inputManager.addMapping("Jumps", new KeyTrigger(KeyInput.KEY_SPACE)); + this.inputManager.addMapping("pick", new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); this.inputManager.addListener(this.actionListener, "Lefts"); this.inputManager.addListener(this.actionListener, "Rights"); this.inputManager.addListener(this.actionListener, "Ups"); this.inputManager.addListener(this.actionListener, "Downs"); this.inputManager.addListener(this.actionListener, "Jumps"); + this.inputManager.addListener(this.actionListener, "pick"); } private boolean left; private boolean right; @@ -204,6 +209,10 @@ public class TerrainGridTileLoaderTest extends SimpleApplication { } } else if (name.equals("Jumps")) { TerrainGridTileLoaderTest.this.player3.jump(); + } else if (name.equals("pick") && keyPressed) { + //Terrain picked = terrain.getTerrainAt(player3.getPhysicsLocation()); + Terrain picked = terrain.getTerrainAtCell(terrain.getCurrentCell()); + System.out.println("** cell "+player3.getPhysicsLocation()+" picked terrain: "+picked); } } };