Question

I use jmonkeyengine and I downloaded a spaceship model from blendswap and converted it to j3o to load it with jmonkeyengine for a space scene where I can control the ship and travel around. However the spaceship is not loaded. The space and planets appear but I want the spaceship to be what the player controls and not first-person like it appears.

enter image description here

I expect the spaceship to appear because I load it without errors and add it to the scene but it stil doesn't show.

//add saucer
ufoNode = (Node) assetManager
        .loadModel("usaucer_v01.j3o");      
rootNode.attachChild(ufoNode);

What should I do to make the spaceship appear? The program is

public class PlanetSimpleTest extends SimpleApplication {

    private PlanetAppState planetAppState;
    private Geometry mark;
    private Node ufoNode;
    private GameCharControl ufoControl;
    Camera cam2;

    public static void main(String[] args){
        AppSettings settings = new AppSettings(true);
        settings.setResolution(1024,768);
        PlanetSimpleTest app = new PlanetSimpleTest();

        app.setSettings(settings);
        //app.showSettings = true;
        app.start();
    }

    @Override
    public void simpleInitApp() {
        // Only show severe errors in log
        java.util.logging.Logger.getLogger("com.jme3").setLevel(java.util.logging.Level.SEVERE);

        // Toggle mouse cursor
        inputManager.addMapping("TOGGLE_CURSOR", 
                new MouseButtonTrigger(MouseInput.BUTTON_LEFT),
                new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addListener(actionListener, "TOGGLE_CURSOR"); 
        // Toggle wireframe
        inputManager.addMapping("TOGGLE_WIREFRAME", 
            new KeyTrigger(KeyInput.KEY_T));
        inputManager.addListener(actionListener, "TOGGLE_WIREFRAME");
        // Collision test
        inputManager.addMapping("COLLISION_TEST", 
            new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
        inputManager.addListener(actionListener, "COLLISION_TEST"); 

        // Setup camera

        // In orbit
        this.getCamera().setLocation(new Vector3f(0f, 0f, 180000f));

        // On surface
        //this.getCamera().setLocation(new Vector3f(-6657.5254f, 27401.822f, 57199.777f));
        //this.getCamera().lookAtDirection(new Vector3f(0.06276598f, 0.94458306f, -0.3222158f), Vector3f.UNIT_Y);

        // Add sun
        //PointLight sun = new PointLight();
        //sun.setPosition(new Vector3f(-100000f,0,180000f));
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-.1f, 0f, -1f));
        sun.setColor(new ColorRGBA(0.75f,0.75f,0.75f,1.0f));      
        rootNode.addLight(sun);

        // Add sky
        Node sceneNode = new Node("Scene");
        sceneNode.attachChild(Utility.createSkyBox(this.getAssetManager(), "Textures/blue-glow-1024.dds"));
        rootNode.attachChild(sceneNode);

        // Create collision test mark
        Sphere sphere = new Sphere(30, 30, 5f);
        mark = new Geometry("mark", sphere);
        Material mark_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mark_mat.setColor("Color", ColorRGBA.Red);
        mark.setMaterial(mark_mat);

        // Add planet app state
        planetAppState = new PlanetAppState(rootNode, sun);
        stateManager.attach(planetAppState);

        // Add planet
        FractalDataSource planetDataSource = new FractalDataSource(4);
        planetDataSource.setHeightScale(900f);
        Planet planet = Utility.createEarthLikePlanet(getAssetManager(), 63710.0f, null, planetDataSource);
        planetAppState.addPlanet(planet);
        rootNode.attachChild(planet);

        // Add moon
        FractalDataSource moonDataSource = new FractalDataSource(5);
        moonDataSource.setHeightScale(300f);
        Planet moon = Utility.createMoonLikePlanet(getAssetManager(), 20000, moonDataSource);
        planetAppState.addPlanet(moon);
        rootNode.attachChild(moon);
        moon.setLocalTranslation(-150000f, 0f, 0f);

        //add saucer
        ufoNode = (Node) assetManager
                .loadModel("usaucer_v01.j3o");
        ufoNode.setLocalScale(100f);
        rootNode.attachChild(ufoNode);

    }

    @Override
    public void simpleUpdate(float tpf) {        
        // slow camera down as we approach a planet
        Planet planet = planetAppState.getNearestPlanet();
        if (planet != null && planet.getPlanetToCamera() != null) {
            this.getFlyByCamera().setMoveSpeed(
                    FastMath.clamp(planet.getDistanceToCamera(), 100, 100000));
        }     
    }

    private ActionListener actionListener = new ActionListener(){
        public void onAction(String name, boolean pressed, float tpf){     
            if (name.equals("TOGGLE_CURSOR") && !pressed) {
                if (inputManager.isCursorVisible()) {
                    inputManager.setCursorVisible(false);
                } else {
                    inputManager.setCursorVisible(true);
                }
            }
            if (name.equals("TOGGLE_WIREFRAME") && !pressed) {
                for (Planet planet: planetAppState.getPlanets()) {
                    planet.toogleWireframe();
                }
            }
            if (name.equals("COLLISION_TEST") && !pressed) {
                CollisionResults results = new CollisionResults();
                Ray ray = new Ray(cam.getLocation(), cam.getDirection());

                // Test collision with closest planet's terrain only
                planetAppState.getNearestPlanet().getTerrainNode().collideWith(ray, results);

                System.out.println("----- Collisions? " + results.size() + "-----");
                for (int i = 0; i < results.size(); i++) {
                  // For each hit, we know distance, impact point, name of geometry.
                  float dist = results.getCollision(i).getDistance();
                  Vector3f pt = results.getCollision(i).getContactPoint();
                  String hit = results.getCollision(i).getGeometry().getName();
                  System.out.println("* Collision #" + i);
                  System.out.println("  You shot " + hit + " at " + pt + ", " + dist + " wu away.");
                }

                if (results.size() > 0) {
                  // The closest collision point is what was truly hit:
                  CollisionResult closest = results.getClosestCollision();
                  // Let's interact - we mark the hit with a red dot.
                  mark.setLocalTranslation(closest.getContactPoint());
                  rootNode.attachChild(mark);
                } else {
                  // No hits? Then remove the red mark.
                  rootNode.detachChild(mark);
                }
            }  
        }
    }; 

}

The spaceship I took from here and converted to jme3:s binary format j3o and added to the game, but I'm obviously not doing everything to make it appear in the scene. I've gotten this far in the jmonkeyengine IDE but then when I load it in Eclipse it doesn't work so I'm trying to create the scene with the jmonkeyengine IDE first.

enter image description here

I then try and create a scene with the spaceship but I get an Exception in the JME SDK.

enter image description here

After adding the lines

ufoNode.setLocalScale(300f);
ufoNode.setLocalTranslation((new Vector3f(10f, 10f, 180010f)));

the spaceship does appear but maybe not perfect. Can it be improved?

enter image description here

enter image description here

Update 140104 17:54 CET

It seems the spaceship was upside down(?) so I've rotated it now.

enter image description here

//add saucer
ufoNode = (Node) assetManager.loadModel("usaucer_v01.j3o");
ufoNode.setLocalScale(1000f);
ufoNode.setLocalTranslation((new Vector3f(10f, 10f, 165000f)));

/* This quaternion stores a 180 degree rolling rotation */ 
Quaternion roll180 = new Quaternion(); 
roll180.fromAngleAxis(FastMath.PI , new Vector3f(0,0,1)); 
/* The rotation is applied: The object rolls by 180 degrees. */ 
ufoNode.setLocalRotation(roll180);      
rootNode.attachChild(ufoNode);  
Was it helpful?

Solution

You add the spaceship at 0,0,0 (as you don't move it) but have moved your camera to 0f, 0f, 180000f. Most likely it is out of shot or far too small to see.

Try loading the spaceship within the jME SDK and confirm that it works within the engine using the scene editor there.

If that works then try a simple test scene just dropping the spaceship and camera in - then once that works move a step at a time towards your desired setting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top