gpt4 book ai didi

java - 当速度 vector 与位置 vector 相加时,速度 vector 趋于无穷大

转载 作者:行者123 更新时间:2023-12-01 09:06:12 25 4
gpt4 key购买 nike

我正在使用 LibGdx 编写一些代码。我有一个利用更新的实体代码和一个输入适配器,该适配器在按键时添加到速度 vector 。在调试过程中,我注意到,当将速度 vector 添加到位置 vector 时,速度会在一瞬间达到无穷大。代码如下:

输入适配器:

@Override
public boolean keyDown(int keycode) {
switch (keycode) {
case Input.Keys.UP:
e.getVelocity().add(0,1);
break;
case Input.Keys.DOWN:
e.getVelocity().add(0,-1);
break;
case Input.Keys.LEFT:
e.getVelocity().add(-1,0);
break;
case Input.Keys.RIGHT:
e.getVelocity().add(1,0);
break;
}
return true;
}

@Override
public boolean keyUp(int keycode) {
switch (keycode) {
case Input.Keys.UP:
e.getVelocity().sub(0,1);
break;
case Input.Keys.DOWN:
e.getVelocity().sub(0,-1);
break;
case Input.Keys.LEFT:
e.getVelocity().sub(-1,0);
break;
case Input.Keys.RIGHT:
e.getVelocity().sub(1,0);
break;
}
return true;
}

游戏类别:

SpriteBatch batch;
Map<UUID,Entity> entities = new HashMap<>();
@Override
public void create () {
batch = new SpriteBatch();
Entity e = new Entity(UUID.randomUUID(),new Texture("avatar.png"),Vector2.Zero,Vector2.Zero);
entities.put(e.getId(),e);
Gdx.input.setInputProcessor(new InputAdapter(){...});
}

@Override
public void render () {
entities.values().forEach(Entity::update);
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
entities.values().forEach(e->e.draw(batch));
batch.end();
}

@Override
public void dispose () {
batch.dispose();
entities.values().forEach(Entity::dispose);
entities.clear();
}

实体类:

 private UUID id;
private Texture texture;
private Vector2 position;
private Vector2 velocity;

public Entity(UUID id, Texture texture, Vector2 position, Vector2 velocity) {
this.id = id;
this.texture = texture;
this.position = position;
this.velocity = velocity;
}

public UUID getId() {
return id;
}

public Texture getTexture() {
return texture;
}

public Vector2 getPosition() {
return position;
}

public void setPosition(Vector2 position) {
this.position = position;
}

public Vector2 getVelocity() {
return velocity;
}

public void setVelocity(Vector2 velocity) {
this.velocity = velocity;
}

public void draw(SpriteBatch batch) {
batch.draw(texture,getPosition().x,getPosition().y);
}

public void dispose() {
texture.dispose();
}

public void update() {
System.out.println(getVelocity());
getPosition().add(getVelocity());
}

输出(按下向下键后):

(0.0,0.0)
(0.0,-1.0)
(0.0,-2.0)
(0.0,-4.0)
(0.0,-8.0)
(0.0,-15.0)
(0.0,-30.0)
(0.0,-60.0)
(0.0,-120.0)
(0.0,-240.0)
(0.0,-480.0)
(0.0,-960.0)
(0.0,-1920.0)
(0.0,-3840.0)
(0.0,-7680.0)
(0.0,-15360.0)
(0.0,-30720.0)
(0.0,-61440.0)
(0.0,-122880.0)
(0.0,-245760.0)
(0.0,-491520.0)
(0.0,-983040.0)
(0.0,-1966080.0)
(0.0,-3932160.0)
(0.0,-7864320.0)
(0.0,-1.572864E7)
(0.0,-3.145728E7)
(0.0,-6.291456E7)
(0.0,-1.2582912E8)
(0.0,-2.5165824E8)
(0.0,-5.0331648E8)
(0.0,-1.00663296E9)
(0.0,-2.01326592E9)
(0.0,-4.02653184E9)
(0.0,-8.0530637E9)
(0.0,-1.61061274E10)
(0.0,-3.22122547E10)
(0.0,-6.4424509E10)
(0.0,-1.28849019E11)
(0.0,-2.57698038E11)
(0.0,-5.15396076E11)
(0.0,-1.03079215E12)
(0.0,-2.0615843E12)
(0.0,-4.1231686E12)
(0.0,-8.2463372E12)
(0.0,-1.64926744E13)
(0.0,-3.29853488E13)
(0.0,-6.5970698E13)
(0.0,-1.31941395E14)
(0.0,-2.63882791E14)
(0.0,-5.27765581E14)
(0.0,-1.05553116E15)
(0.0,-2.11106233E15)
(0.0,-4.22212465E15)
(0.0,-8.4442493E15)
(0.0,-1.68884986E16)
(0.0,-3.37769972E16)
(0.0,-6.7553994E16)
(0.0,-1.35107989E17)
(0.0,-2.70215978E17)
(0.0,-5.40431955E17)
(0.0,-1.08086391E18)
(0.0,-2.16172782E18)
(0.0,-4.32345564E18)
(0.0,-8.6469113E18)
(0.0,-1.7293823E19)
(0.0,-3.4587645E19)
(0.0,-6.917529E19)
(0.0,-1.3835058E20)
(0.0,-2.7670116E20)
(0.0,-5.5340232E20)
(0.0,-1.10680464E21)
(0.0,-2.2136093E21)
(0.0,-4.4272186E21)
(0.0,-8.854437E21)
(0.0,-1.7708874E22)
(0.0,-3.5417749E22)
(0.0,-7.0835497E22)
(0.0,-1.4167099E23)
(0.0,-2.8334199E23)
(0.0,-5.6668398E23)
(0.0,-1.13336796E24)
(0.0,-2.2667359E24)
(0.0,-4.5334718E24)
(0.0,-9.0669436E24)
(0.0,-1.8133887E25)
(0.0,-3.6267775E25)
(0.0,-7.253555E25)
(0.0,-1.450711E26)
(0.0,-2.901422E26)
(0.0,-5.802844E26)
(0.0,-1.1605688E27)
(0.0,-2.3211376E27)
(0.0,-4.642275E27)
(0.0,-9.28455E27)
(0.0,-1.85691E28)
(0.0,-3.71382E28)
(0.0,-7.42764E28)
(0.0,-1.485528E29)
(0.0,-2.971056E29)
(0.0,-5.942112E29)
(0.0,-1.1884224E30)
(0.0,-2.3768449E30)
(0.0,-4.7536898E30)
(0.0,-9.5073795E30)
(0.0,-1.9014759E31)
(0.0,-3.8029518E31)
(0.0,-7.6059036E31)
(0.0,-1.5211807E32)
(0.0,-3.0423614E32)
(0.0,-6.084723E32)
(0.0,-1.2169446E33)
(0.0,-2.4338892E33)
(0.0,-4.8677783E33)
(0.0,-9.7355566E33)
(0.0,-1.9471113E34)
(0.0,-3.8942226E34)
(0.0,-7.7884453E34)
(0.0,-1.5576891E35)
(0.0,-3.1153781E35)
(0.0,-6.2307562E35)
(0.0,-1.24615125E36)
(0.0,-2.4923025E36)
(0.0,-4.984605E36)
(0.0,-9.96921E36)
(0.0,-1.993842E37)
(0.0,-3.987684E37)
(0.0,-7.975368E37)
(0.0,-1.5950736E38)
(0.0,-3.1901472E38)
(0.0,-Infinity)
(0.0,-Infinity)
(0.0,-Infinity)
(0.0,-Infinity)
(0.0,-Infinity)
(0.0,-Infinity)
(0.0,-Infinity)

我不确定为什么速度 vector 会增加,因为它唯一被修改的时间是在按键事件期间。如果有人能解释为什么会发生这种情况,我们将不胜感激!

最佳答案

在 Java 中,不存在不可变对象(immutable对象)这样的东西。您已将静态对象实例 Vector2.Zero 的两个副本传递给每个新实体。因此,每当您按下一个键时,您都会向 Vector2.Zero 添加一个值,而每当您更新一个实体时,您都会多次向其自身添加 Vector2.Zero 的新值,因为每个 Entity 对象都会引用同一个实例。

创建实体类的一种安全方法是让它实例化自己的 final Vector2 实例:

public class Entity {

private UUID id;
private Texture texture;
private final Vector2 position = new Vector2();
private final Vector2 velocity = new Vector2();

public Entity(UUID id, Texture texture) {
this.id = id;
this.texture = texture;
}

//...
}

此外,当实体的每个实例都使用相同的图像时,为它们实例化一个新的纹理对象并不是一个好主意。您正在将完全相同的图像数据的许多副本冗余地加载到 GPU。加载一次纹理并将相同的实例传递给所有实体,并且不要忘记在 dispose() 方法中处理它。不要在实体的 dispose 方法中处理它,因为它会被处理多次(实体不“拥有”纹理,因为它没有实例化它)。

关于java - 当速度 vector 与位置 vector 相加时,速度 vector 趋于无穷大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41268235/

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