Need help with terrain and ray collision

Hi guys,



I need some help, my Ray cast and my TerrainQuad do not collide.

I’ve got a class Map in which I’ve copied this method from TerrainTestModifyHeight :



[java]

private void createTerrain() {

// First, we load up our textures and the heightmap texture for the terrain



// TERRAIN TEXTURE material

Material matTerrain = new Material(assetManager, “Common/MatDefs/Terrain/TerrainLighting.j3md”);

matTerrain.setBoolean(“useTriPlanarMapping”, false);

matTerrain.setBoolean(“WardIso”, true);

matTerrain.setFloat(“Shininess”, 0);



// ALPHA map (for splat textures)

matTerrain.setTexture(“AlphaMap”, assetManager.loadTexture(“Textures/Terrain/splat/alphamap.png”));



// GRASS texture

Texture grass = assetManager.loadTexture(“Textures/Terrain/splat/grass.jpg”);

grass.setWrap(WrapMode.Repeat);

matTerrain.setTexture(“DiffuseMap”, grass);

matTerrain.setFloat(“DiffuseMap_0_scale”, tileScale);



// DIRT texture

Texture dirt = assetManager.loadTexture(“Textures/Terrain/splat/dirt.jpg”);

dirt.setWrap(WrapMode.Repeat);

matTerrain.setTexture(“DiffuseMap_1”, dirt);

matTerrain.setFloat(“DiffuseMap_1_scale”, tileScale);



// ROCK texture

Texture rock = assetManager.loadTexture(“Textures/Terrain/splat/road.jpg”);

rock.setWrap(WrapMode.Repeat);

matTerrain.setTexture(“DiffuseMap_2”, rock);

matTerrain.setFloat(“DiffuseMap_2_scale”, tileScale);



float [] floatMap = new float[size];



// CREATE THE TERRAIN

this.terrain = new TerrainQuad(“terrain”, 65, 513, floatMap);



this.terrain.setMaterial(matTerrain);

this.terrain.setLocalTranslation(0, 0, 0);

app.getRootNode().attachChild(this.terrain);



}

[/java]



And then i’ve got an AbstractAppState EditTerrainHeight in which I’m calling

[java]

update(float tpf) {

Vector3f intersection = getWorldIntersection(); }

[/java]



[java]

private Vector3f getWorldIntersection() {

Vector2f click2d = app.getInputManager().getCursorPosition();

Vector3f click3d = app.getCamera().getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();

Vector3f dir = app.getCamera().getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d);



Ray ray = new Ray(click3d, dir);

CollisionResults results = new CollisionResults();

int numCollisions = map.getTerrain().collideWith(ray, results);

if (numCollisions > 0) {

CollisionResult hit = results.getClosestCollision();

return hit.getContactPoint();

}

return null;

}

[/java]



This doesn’t work.

If i do app.getRootNode().collideWith(ray, results); it only collides with the sky.

If i put my getWorldIntersection() Method and all that stuff needed for mouspicking into TerrainTestModifyHeight everything works fine.



Any idea what I’m doing wrong?

normalize your ray

1 Like

Thanks.

http://i.imgur.com/Es98O.gif

:slight_smile: