【问题标题】:Jmonkey: Make RigidBodyControl React to GravityJmonkey:使 RigidBodyControl 对重力做出反应
【发布时间】:2025-12-17 17:25:07
【问题描述】:

我正在尽最大努力让一个物体掉下来,但到目前为止我什至无法靠近。这是我正在尝试的代码。

    BulletAppState bulletAppState = new BulletAppState();

    cubemesh = new Box(1f,1f,1f);
    Geometry something = new Geometry("cube", cubemesh);
    Material bronze = new Material(assetManager, 
    "Common/MatDefs/Light/Lighting.j3md");
    something.setLocalTranslation(0,1,0);
    bronze.setTexture("DiffuseMap", assetManager.loadTexture("Textures/bronze.jpg"));
    something.setMaterial(bronze);
    rootNode.attachChild(something);

    RigidBodyControl control = new RigidBodyControl(10f);
    Vector3f direction = new Vector3f(0,-9.81f,0);
    something.addControl(control);

    //all the random lines i've tried
    stateManager.attach(bulletAppState);
    control.setGravity(direction);
    bulletAppState.getPhysicsSpace().setGravity(direction);
    rootNode.attachChild(something);
    bulletAppState.getPhysicsSpace().add(control);

我们将不胜感激。

【问题讨论】:

  • jME wiki里有很多例子:wiki.jmonkeyengine.org/doku.php/jme3:beginner:hello_physics不管怎样,你把bulletAppState附加到AppStateManager了吗?
  • 是的,我在行中添加了,忘记了。
  • 你能在屏幕上看到你的对象吗?您的代码无法编译。它也不包括所有必要的东西。请阅读sscce.org 并发布一个完整的示例来重现您的问题。
  • 完成,只需使用随机 jpg。 (不想更改变量名)

标签: jmonkeyengine


【解决方案1】:

你的例子中的物理学对我有用。但是使用你的材料,我什么都看不见,因为没有光。

尝试附加Light

AmbientLight light = new AmbientLight();
light.setColor(ColorRGBA.White);
rootNode.addLight(light);

尝试随机线不会让你走得太远。我建议阅读 jME wiki,以便了解这些行的实际作用。这是一个简约的例子,它使用了不需要光的Material

public void simpleInitApp() {
    BulletAppState bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);

    Geometry something = new Geometry("cube", new Box(1,1,1));
    something.setMaterial( new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md") );
    something.setLocalTranslation(0,2,0);
    something.addControl( new RigidBodyControl(10f) );

    rootNode.attachChild(something);
    bulletAppState.getPhysicsSpace().add(something);
}

这个例子展示了一个彩色的下落立方体。如果这对您不起作用,则您的 jME 版本或其设置可能有问题 (我使用的是 jMonkeyEngine 3.1-alpha1)。

【讨论】: