SharedMesh question

I just want to make sure I understand how SharedMesh is supposed to work.

I created a Box:

sharedBox = new Box("unit", new Vector3f(30, 30, 50f), 2.5f, 1.5f, 2.5f);

It's located at 30,30,50 but as this is only shared and not rendered, I'm not concerned about that.

I then create a new SharedMesh using this:

SharedMesh mesh = new SharedMesh("unit",sharedBox);

I want to place the new object on the terrain using getHeight:

        TerrainBlock terrain = LevelLoader.get().getTerrainBlock();
        Vector3f newPos = pos;
        newPos.y = terrain.getHeight(pos.x, pos.z);

The mesh is always located floated in the air. Is my understanding of SharedMesh correct?


The center of a box is not it's location but part of the geometry. That's why even a shared mesh at (0,0,0) renderes the box at (30,30,50).

Check it out - you should have the same effect without a shared mesh when you do


So create your box with center (0,0,0).

Ok I understand about the centre. But can I create one mesh and then place multiple instances of that mesh at different locations using SharedMesh? Is that how it works? I guess this is the question I should have asked originally.


Yes you can. That's exacly the intention of SharedMesh.  :slight_smile:

Cool, then I'm just doing something wrong. :slight_smile:


Like irrisor said, create your box with a center of (0,0,0). Set up this box like you would any other trimesh (set up the boundings, etc). Then create as many Sharedmesh objects as you need. Then use each individual shared mesh to set its location. Attach the Sharedmesh to the scene, but not the original.

SharedMesh's share vertex data and it will obtain any states assigned to the original mesh. However, you can change the states of the sharedmesh individually if you want. See TestSharedMesh.

I think I'm on the right track. My  problems bould be that the TerrainBlock getHeight method is returning odd values that are clearly much higher than the terrain height.

Haha, I guess it would work if I didn't re-position the terrain to y=-100 and then wonder why it looks odd when getHeight returns y=95!!  ://