jMonkey旋转和平移

4
我在JME(JMonkey)中旋转两个盒子然后平移时遇到了问题。我已经搜索了论坛并发现了一些其他语言的类似问题,但我不理解答案,可能是因为我不懂其他语言。我有两个盒子,都使用.lookat([另一个盒子])、旋转和本地平移。在我的想象中,本地平移应该沿着盒子面对的方向移动,但实际上它似乎沿着世界轴移动。值得注意的一点是:我不知道如何在3D中使用矩阵数学,我找到的一些答案使用矩阵数学来解决问题。我希望能够理解这个问题,以便将来避免此类问题。我已经尽量简化了代码,使其没有任何不必要的部分。
package jme3test.helloworld;
import com.jme3.app.SimpleApplication;
import com.jme3.font.BitmapText;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import java.util.ResourceBundle.Control;


public class SSF2 extends SimpleApplication {
public Geometry blue = null;
public Geometry red = null;

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

@Override
public void simpleInitApp() {
    // create a blue box at coordinates (1,-1,1)
    Box box1 = new Box( Vector3f.ZERO, 1f,2f,.5f);
    blue =  new Geometry("Box", box1);
    Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat1.setColor("Color", ColorRGBA.Blue);
    blue.setMaterial(mat1);
    blue.move(-5,0,-3);

    // create a red box straight above the blue one at (1,3,1)
    Box box2 = new Box( Vector3f.ZERO, 1f,2f,.5f);
    red = new Geometry("Box", box2);
    Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat2.setColor("Color", ColorRGBA.Red);
    red.setMaterial(mat2);
    red.move(5,0,-3);

    rootNode.attachChild(blue);
    rootNode.attachChild(red);

    blue.lookAt(red.getWorldTranslation(), new Vector3f(0,1,0) );
    red.lookAt(blue.getWorldTranslation(), new Vector3f(0,1,0) );
}

@Override
public void simpleUpdate(float tpf) {
    blue.setLocalTranslation(new Vector3f( (blue.getLocalTranslation().getX() + .02f), (blue.getLocalTranslation().getY())  , (blue.getLocalTranslation().getZ() )));
    red.setLocalTranslation(new Vector3f( (red.getLocalTranslation().getX() + .02f), (red.getLocalTranslation().getY())  , (red.getLocalTranslation().getZ() )));
}
}
1个回答

2

看看这个:

@Override
public void simpleUpdate(float tpf) {
    red.rotate(0, 0.001f, 0);

    // For the red (moves in a circle)
    Quaternion rotation = red.getLocalRotation();
    Vector3f front = new Vector3f(0, 0, 0.01f);
    Vector3f heading = rotation.mult(front);
    red.move(heading);

    /// For the blue (follows the red)
    blue.lookAt(red.getWorldTranslation(), Vector3f.UNIT_Y);
    float velocity = 0.01f;
    Vector3f trajectory = red.getWorldTranslation().subtract(blue.getWorldTranslation());
    trajectory = trajectory.normalize();
    Vector3f offset = trajectory.mult(velocity);
    blue.move(offset);
    System.out.print(offset);

}

确实帮了我,是的。也许官方的JMonkeyEngine3文档假定用户更倾向于数学和内部工作,但我仍然看到很多次提出这个问题。 - noncom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接