gpt4 book ai didi

java - libgdx:使用相机测试对象是否超出屏幕

转载 作者:行者123 更新时间:2023-11-29 05:30:10 25 4
gpt4 key购买 nike

我关注了this创建雪花对象池的示例。当它离开屏幕时它会被释放。我的问题是我无法访问 WorldRenderer 的相机以使用:pointInFrustrum()。我通过使相机静止来解决这个问题。是否有另一种方法可以确定雪花是否超出屏幕或获取相机的当前位置?这似乎不是一个好的解决方案。

雪花.java:

 public void update (float delta){

// update snowflake position
position.add(0, -1*delta*5);

// if snowflake is out of screen, set it to dead
if(!WorldRenderer.cam.frustum.pointInFrustum(new Vector3(position.x, position.y, 0))) {
alive = false;
}
}

World 的更新方法中存在同样的问题。我需要访问相机才能找到 y 位置:

 private void updateSnowflakes(float deltaTime) {
spawnTime += deltaTime;
if(spawnTime > spawnDelay) {
spawnTime = 0;
Snowflake item = snowflakePool.obtain();
float x = rand.nextFloat() * Helper.FRUSTUM_WIDTH;
float y = WorldRenderer.cam.position.y + Helper.FRUSTUM_HEIGHT/2;
item.init(x, y);
activesSnowflakes.add(item);
}

int len = activesSnowflakes.size;
for (int i = len; --i >= 0;) {
Snowflake item = activesSnowflakes.get(i);
if (item.alive == false) {
activesSnowflakes.removeIndex(i);
snowflakePool.free(item);
} else {
item.update(deltaTime);
}
}
}

最佳答案

如果你需要在其他类中使用worldrendered cam。您需要将其作为参数发送。你的方法也不错。只是不要忘记在 WorldRendered 构造函数中初始化静态相机,因为它可能会在之前的运行中幸存下来。

在这里你每帧都创建一个新的Vector,这会让垃圾收集器很伤心。不要这样做。

if(!WorldRenderer.cam.frustum.pointInFrustum(new Vector3(position.x, position.y, 0))) {

关于java - libgdx:使用相机测试对象是否超出屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21367186/

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