0

我正在 JME3 中开发游戏,出于某种原因,我的空间播放器正在(向上)从我的世界飞走。我看不出我错过了什么,你能看看并告诉我如何让玩家留在世界上吗?

    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
    }
}
4

1 回答 1

0

您的y坐标轴可能指向另一个方向,因此您实际上必须将重力设置为9.81而不是-9.81

于 2013-01-14T19:34:21.657 回答