gpt4 book ai didi

java - 如何在 LibGDX 中获取模型实例的 3d 坐标?

转载 作者:行者123 更新时间:2023-11-30 08:32:03 25 4
gpt4 key购买 nike

我想从我的模型实例中获取 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 的,我从来没有上过关于它的数学类(class)。我卡住了。

最佳答案

还有游戏对象类:

public static class GameObject extends ModelInstance {
public Vector3 center = new Vector3();
public Vector3 dimensions = new Vector3();
public float radius;
public final Vector3 position = new Vector3();
public final Quaternion rotation = new Quaternion();
public final Vector3 scale = new Vector3();

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 updateTransform() {
this.transform.set(position, rotation, scale);
}
}`

它只返回 (0, 0, 0),当我使用 updateTransform 方法时,它甚至不渲染。

很抱歉有多个答案。不知道怎么排版在评论区

关于java - 如何在 LibGDX 中获取模型实例的 3d 坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40332212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com