简体   繁体   English

jmonkey旋转和平移

[英]jmonkey rotation and translation

I am having an issue in JME (JMonkey) with rotating then translating two boxes. 我在JME(JMonkey)中遇到一个问题,即旋转然后平移两个框。 I have searched the forums and found some similar issues with other languages but I do not understand the answers and that may be because I do not know the other languages. 我在论坛上进行了搜索,发现其他语言存在一些类似的问题,但我不明白答案,这可能是因为我不了解其他语言。 I have two boxes with .lookat( [the other box] ), a rotate, and then a local translation. 我有两个带有.lookat([其他框])的框,一个旋转框,然后是一个本地翻译框。 In my mind the local translation should move the box in the direction it is facing, but it does not it seems to be moving along the world axes. 在我看来,本地平移应该使盒子朝着它所面对的方向移动,但是似乎并没有沿着世界的轴线移动。 One thing of note; 一件值得注意的事; I do not know anything about using matrix math in 3d, some of the answers I found used matrix math to solve the problem. 我对在3d中使用矩阵数学一无所知,我发现的某些答案是使用矩阵数学解决问题的。 I would like to understand this so that I can avoid this problem in the future. 我想了解这一点,以便将来避免出现此问题。 I have reduced my code as small as possible so it does not have any unnessicary parts. 我已将代码尽可能地减少,因此它没有任何不必要的部分。

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() )));
}
}

Check out this: 看看这个:

@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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM