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 に答える