我想从我的模型实例中获取3d坐标(作为Vector3)。使用下面的代码,我只能得到原始的坐标,但是在呈现方法中,我的模型是围绕Y轴旋转(同时移动),我希望得到它的每个帧的坐标。我使用@Xoppa创建的一个小类:
public static class GameObject extends ModelInstance {
public Vector3 center = new Vector3();
public Vector3 dimensions = new Vector3();
public float radius;
public BoundingBox bounds = new BoundingBox();
public GameObject (Model model, String rootNode, boolean mergeTransform) {
super(model, rootNode, mergeTransform);
calculateBoundingBox(bounds);
bounds.getCenter(center);
bounds.getDimensions(dimensions);
radius = dimensions.len() / 2f;
}
}
这是我的密码:
public void render(float delta) {
super.render(delta);
if (loading && manager.update()) {
doneLoading();
}
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
batch.begin();
backgroundSprite.draw(batch);
batch.end();
for(GameObject instance : instances){
instance.transform.rotate(new Vector3(0, 1, 0), 0.5f);
float x = instance.bounds.getCenterX();
float y = instance.bounds.getCenterY();
float z = instance.bounds.getCenterZ();
System.out.println(x+" "+y+" "+z);
if(isVisible(cam, instance)){
modelBatch.begin(cam);
modelBatch.render(instance, environment);
modelBatch.end();
}
}
}
请帮帮忙,没有函数"getPosition“之类的东西,这都是关于Matrix4的,我从来没有上过一门数学课。我卡住了。
发布于 2016-10-30 14:36:54
试一试如下:
Vector3 position;
position = modelInstance.transform.getTranslation(new Vector3());
这将获取位置并将其存储在position
向量中。
有时,您可能需要使用视图和投影矩阵来转换转换,以获得正确的结果。
/* Make sure that you use a new Matrix4, otherwise you will
change the models transform, which we don't want to do. */
Matrix4 modelTransform = new Matrix4();
modelTransform.set(modelInstance.transform);
/* Multiply the transform with the combined matrix of the camera. */
modelTransform.mul(camera.combined);
/* Extract the position as usual. */
Vector3 position;
position = modelTransform.getTranslation(new Vector3());
它获取位置并将其存储在position
向量和转换中,并将其存储在modelTransform
矩阵中。你可能应该重复使用这个向量和矩阵,每次你得到这个位置,而不是创建新的。
https://gamedev.stackexchange.com/questions/132310
复制相似问题