2012-12-04 147 views
3

我在JME(JMonkey)中遇到問題,然後翻譯兩個框。我搜索了論壇,發現與其他語言有類似的問題,但我不明白答案,這可能是因爲我不知道其他語言。我有兩個包含.lookat([另一個盒子])的框,一個旋轉,然後是一個本地翻譯。在我看來,本地翻譯應該朝着它所面對的方向移動盒子,但它似乎並沒有沿着世界軸線移動。有一點值得注意。我不知道在3D中使用矩陣數學,我發現一些答案使用矩陣數學來解決問題。我想了解這一點,以便我可以在將來避免這個問題。我儘可能縮小了我的代碼,所以它沒有任何不切實際的部分。jmonkey旋轉和翻譯

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

回答

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

} 
+0

這都幫助我,是的。也許官方的JMonkeyEngine3文檔假設用戶更喜歡數學和內部工作,但是我看到的這個問題依然存在很多次。 – noncom