我有两个模型实例modelInstance1
和modelInstance2
。
modelInstance1 - position Vector3(0,0,0)
modelInstance2 - position Vector3(10,10,10)
如何将modelInstance1旋转到modelInstance2并在update方法中移动此方向?
回答后的问题:
我试过回答基本的xoppa的立方体教程并且有些不好。有什么问题吗?
@Override
public void create() {
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
modelBatch = new ModelBatch();
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(10f, 10f, 10f);
cam.lookAt(0,0,0);
cam.near = 1f;
cam.far = 300f;
cam.update();
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(5f, 5f, 10f,
new Material(ColorAttribute.createDiffuse(Color.GREEN)),
Usage.Position | Usage.Normal);
instance1 = new ModelInstance(model);
instance2 = new ModelInstance(model);
instance2.transform.translate(10,0,10);
camController = new CameraInputController(cam);
Gdx.input.setInputProcessor(camController);
}
渲染和更新方法。
@Override
public void render() {
Vector3 position1 = new Vector3(), position2 = new Vector3(), direction = new Vector3();
instance1.transform.getTranslation(position1);
instance2.transform.getTranslation(position2);
direction = (position2).sub(position1).nor();
instance1.transform.setToRotation(direction, Vector3.X);
instance2.transform.translate(0.05f, 0, 0.05f).rotate(0, 1, 0, 10*Gdx.graphics.getDeltaTime()).translate(0, 0, 0);
camController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(instance1, environment);
modelBatch.render(instance2, environment);
modelBatch.end();
}
我尝试将setToRotate方法更改为:
instance1.transform.setToLookAt(position1, position2, new Vector3(0,1,0));
但同样的问题..
答案 0 :(得分:1)
在伪代码中面对看起来像:
modelInstance1.direction(from matrix) = (modelInstance2.position).sub(modelInstance1.position).nor();
modelInstance1.setRotation(modelInstance1.direction, Vector3.Y);
更新: 在此示例中,方向向量应用于对象的Vector3.Y轴。你的场景可以有另一个轴系统,所以有时你需要尝试不同的物体轴来找到目标轴。
答案 1 :(得分:0)
终于....就是这样!!!!!!!!!!!!!!!!!!
当有人知道同样的代码做得更好时,请告诉我。
我希望能帮助别人。 :)
Vector3 position1 = new Vector3(), position2 = new Vector3(), direction = new Vector3();
instance2.transform.translate(0.5f, 0, 0.5f).rotate(0, 1, 0, 45*Gdx.graphics.getDeltaTime()).translate(0, 0, 0);
instance1.transform.getTranslation(position1);
instance2.transform.getTranslation(position2);
direction = (position2).sub(position1).nor();
direction.set(-direction.x, -direction.y, -direction.z);
Quaternion quaternion = new Quaternion();
Matrix4 instanceRotation = instance1.transform.cpy().mul(instance1transform);
instanceRotation.setToLookAt(direction, new Vector3(0,-1,0));
instanceRotation.rotate(0, 0, 1, 180);
instanceRotation.getRotation(quaternion);
instance1.transform.set(position1, quaternion);