0

I am developing a game in JME3 and for some reason my spatial Player is flying away (upwards) from my world. I don't see what I am missing, could you take a look and tell me how to make the Player stay on the world?

    public class Main extends SimpleApplication {

    private BulletAppState bulletAppState;

    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }

    Node playerNode = new Node("player");
    Spatial player;

    @Override
    public void simpleInitApp() {
        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);
        bulletAppState.getPhysicsSpace().setGravity(new Vector3f(0f,-9.81f,0f));
        speed = 10.0f;

        //World
        Spatial world = assetManager.loadModel("Scenes/city/sound_city.j3o");
        world.setLocalScale(2.0f);

        CollisionShape worldcshape = CollisionShapeFactory.createDynamicMeshShape(world);
        RigidBodyControl rworld = new RigidBodyControl(worldcshape);
        rworld.setMass(0f);
        bulletAppState.getPhysicsSpace().add(rworld);
        world.addControl(rworld);
        rootNode.attachChild(world);

        //Player
        player = assetManager.loadModel("Models/player/player.j3o");
        playerNode.attachChild(player);
        CollisionShape playerShape = CollisionShapeFactory.createDynamicMeshShape(player);
        RigidBodyControl plr = new RigidBodyControl(playerShape,123.0f);
        plr.setFriction(1f);

        plr.setMass(60f);
        bulletAppState.getPhysicsSpace().add(plr);

        player.addControl(plr);
        rootNode.attachChild(playerNode);

        //SoundModel
        Spatial sound = assetManager.loadModel("Models/sound/sound.j3o");
        sound.setLocalScale(0.3f);
        sound.setLocalTranslation(3.0f, 0, 5.0f);
        sound.rotate(0, FastMath.DEG_TO_RAD*-40, 0);
        rootNode.attachChild(sound);

        Spatial sound2 = sound.clone();
        sound.setLocalTranslation(3.0f, 0, 6.0f);
        rootNode.attachChild(sound2);

        //Light
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
        rootNode.addLight(sun);

        initCam(player);
        initKeys();
    }

    private void initCam(Spatial player) {
        cam.setLocation(player.getLocalTranslation().add(0, 30.0f, 0));
        cam.lookAt(playerNode.getLocalTranslation(), Vector3f.UNIT_Z.negate());
//        cam.lookAt(playerNode.getLocalTranslation(), Vector3f.UNIT_Y);
//        cam.setRotation(cam.getRotation().fromAngles(FastMath.DEG_TO_RAD*90, FastMath.DEG_TO_RAD*180, 0));

        // Disable the default flyby cam
        flyCam.setEnabled(false);
        // Enable a chase cam
        ChaseCamera chaseCam = new ChaseCamera(cam, player, inputManager);
//        chaseCam.setTrailingEnabled(false);

        chaseCam.setDefaultHorizontalRotation(FastMath.DEG_TO_RAD*(90));
        chaseCam.setDefaultVerticalRotation(FastMath.DEG_TO_RAD*(90));
    }

    private void initKeys() {
        //WASD
        inputManager.addMapping("Left", new KeyTrigger(keyInput.KEY_A));
        inputManager.addMapping("Right", new KeyTrigger(keyInput.KEY_D));
        inputManager.addMapping("Up", new KeyTrigger(keyInput.KEY_W));
        inputManager.addMapping("Down", new KeyTrigger(keyInput.KEY_S));

        //ArrowKeys
        inputManager.addMapping("Left", new KeyTrigger(keyInput.KEY_LEFT));
        inputManager.addMapping("Right", new KeyTrigger(keyInput.KEY_RIGHT));
        inputManager.addMapping("Up", new KeyTrigger(keyInput.KEY_UP));
        inputManager.addMapping("Down", new KeyTrigger(keyInput.KEY_DOWN));

        inputManager.addListener(analogListener, new String[]{"Left", "Right", "Up", "Down"});
    }

    private AnalogListener analogListener = new AnalogListener() {
        public void onAnalog(String name, float value, float tpf) {
            if (name.equals("Left")) {
                player.rotate(0, value*speed, 0);
            }
            if (name.equals("Right")) {
                player.rotate(0, -value*speed, 0);
            }
            if (name.equals("Up")) {
                Vector3f v = player.getLocalRotation().getRotationColumn(2).normalize().mult(speed*value*1.4f);;
                playerNode.move(v.negate());
            }
            if (name.equals("Down")) {
                Vector3f v = player.getLocalRotation().getRotationColumn(2).normalize().mult(speed*value);;
                playerNode.move(v);
            }
        }
    };

    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }
}
Luzius L
  • 157
  • 1
  • 9
  • Do you need to set the gravity for the world and the player?? – Amir Afghani Jan 14 '13 at 19:35
  • @LuziusL According to the tutorial it's enough to set it globally. Remove both of your gravity settings and try `bulletAppState.getPhysicsSpace.setGravity(new Vector3f(0f,-9.81f,0f));` instead. – us2012 Jan 14 '13 at 20:30
  • (The tutorial is [here](http://jmonkeyengine.org/wiki/doku.php/jme3:advanced:physics#physicsspace_code_samples)) – us2012 Jan 14 '13 at 20:36
  • I built my code with that tutorial. I tried your tip.. It's still hovering away – Luzius L Jan 14 '13 at 21:11
  • 1
    Hmm. Can you modify your example to provide full code and use models and resources which are contained in the SDK? At the moment I have to make quite a few changes to your code, so I can't replicate the behaviour you are seeing. – us2012 Jan 15 '13 at 19:18
  • Now I added all there is. The floating problem is over now, but I can't move the player model. ? – Luzius L Jan 15 '13 at 20:10
  • @LuziusL What did you do to solve the floating problem? Could you post it as a self answer? (I'm trying to get as many of the JMonkey questions with accepted answers as possible) – Richard Tingle Jul 31 '13 at 10:31

1 Answers1

0

Your y coordinate axis probably points the other way, so you actually have to set your gravity to 9.81 rather than -9.81.

us2012
  • 16,083
  • 3
  • 46
  • 62