【问题标题】:CollisionShape flying away from worldCollisionShape 飞离世界
【发布时间】:2013-01-14 19:29:43
【问题描述】:

我正在 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
    }
}

【问题讨论】:

  • 需要为世界和玩家设置重力吗??
  • @LuziusL 根据教程,全局设置就足够了。删除你的两个重力设置并尝试bulletAppState.getPhysicsSpace.setGravity(new Vector3f(0f,-9.81f,0f));
  • (教程为here)
  • 我使用该教程构建了我的代码。我试过你的提示..它仍然徘徊在远处
  • 嗯。您可以修改您的示例以提供完整的代码并使用 SDK 中包含的模型和资源吗?目前我必须对您的代码进行一些更改,因此我无法复制您所看到的行为。

标签: java collision jmonkeyengine


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2020-07-29
    • 2015-10-02
    相关资源
    最近更新 更多