Why will my geometry not appear? (jMonkeyEngine)

118 views Asked by At

I'm just now picking up jMonkeyEngine and I've encountered an issue I can't seem to solve.

In the simpleInitApp method in the main class, I can use the following code to successfully render a box:

    Box playerBase = new Box(Vector3f.ZERO,1f,1f,1f);
    Geometry playerBaseGeom = new Geometry("playerBase", playerBase);
    Transform fixBaseHeight = new Transform(
            new Vector3f(0f,(0.5f * 2f),0f));
    playerBaseGeom.setLocalTransform(fixBaseHeight);
    Material playerBaseMaterial = new Material(assetManager,
            "Common/MatDefs/Misc/Unshaded.j3md");
    playerBaseMaterial.setColor("Color", ColorRGBA.Yellow);
    playerBaseGeom.setMaterial(playerBaseMaterial);
    rootNode.attachChild(playerBaseGeom);

I tried to use a class called Tower to be able to spawn several boxes representing towers (for a simple tower defense game). The tower class looks like this:

public class Tower {

    private static final float HEIGHT = 0.5f;
    private static final float WIDTH = 0.2f;
    private static final float DEPTH = 0.2f;

    private Geometry towerGeom;
    private Material towerMaterial;
    private Box tower;

    public Tower(AssetManager assetManager, float x_coord, float z_coord) {

        tower = new Box();
        towerGeom = new Geometry("tower", tower);
        towerMaterial = new Material(assetManager,
                "Common/MatDefs/Misc/Unshaded.j3md");
        towerMaterial.setColor("Color", ColorRGBA.Green);
        towerGeom.setMaterial(towerMaterial);

        towerGeom.setLocalTranslation(x_coord, (0.5f * .5f),z_coord);
        towerGeom.setLocalScale(WIDTH, HEIGHT, DEPTH);
    }

    public Geometry getGeometry() {
        return towerGeom;
    }
}

In the main class, in the simpleInitApp method, I tried to use my new Tower class like this:

    List <Tower> towers = new ArrayList<Tower>();
    towers.add(new Tower(assetManager, 10f,8f));
    for(Tower t:towers) {
        rootNode.attachChild(t.getGeometry());
    }

However, no cube is rendered. Why? I used the exact same procedure shown in the beginning, which worked.

1

There are 1 answers

0
1000ml On BEST ANSWER

The Box() constructor is meant for serialization only and doesn't initialize the mesh. The constructor in your upper example is deprecated. Use:

tower = new Box(0.5f, 0.5f, 0.5f);

This will create a cube of the size 1x1x1 centered at [0, 0, 0].

Also, make sure you look at the tower. With the default camera position and the tower at [10, 0, 8], it will be placed behind you.

getCamera().lookAt( new Vector3f(10f, 0, 8f), Vector3f.UNIT_Y );

I recommend consulting the jME source code for this kind of problem so you can be sure whats going on.