gpt4 book ai didi

Java游戏 - 处理线性运动

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:28:35 25 4
gpt4 key购买 nike

所以,我正在尝试在 LWJGL 中制作游戏,它似乎对我来说工作正常。虽然,我在屏幕上移动我的实体时遇到了一些问题。我想让它以相同的速度从一个点移动到另一个点。此外,我正在根据实体移动的方向为我的 Sprite 制作动画。

但是!我有一些问题:

1# 它闪烁是因为运动定义了一个修饰符:delta(使 FPS 定义的平滑运动)。实际上,它从来没有真正达到它的点(因为它重新计算并且从未达到该位置)。我该如何解决这个问题?

2# 当2个玩家加入同一个服务器时,我的角色在最快的电脑上跑得更快。我认为是 FPS 的问题,请问如何解决?

private String name;
private float positionx,positiony; // Current
private int targetx,targety; // Target
private int dx, dy; // Direction
private int pointx, pointy; // Direction
private float speed;
private Sprite sprite;

public Entity(String name, int positionx, int positiony, Sprite sprite){
this.name = name;
this.speed = 0.1f;
this.positionx = 720;
this.positiony = 450;
this.targetx = 1000; // fix this
this.targety = 10; // this for testing.
this.sprite = sprite;
this.dx = 0;
this.dy = 0;
}
//double distance = Math.sqrt((vx * vx) + (vy * vy));
public void move(long delta){
if(positionx < targetx){
dx = 1;
pointx = 1;
}else if(positionx > targetx){
dx = -1;
pointx = -1;
}else{
dx = 0;
}

if(positiony < targety){
dy = 1;
pointy = 1;
}else if(positiony > targety){
dy = -1;
pointy = -1;
}else{
dy = 0;
}


//Set animations:
if(positionx==targetx && positiony==targety){
if(pointx<0){
sprite.setAnimation(5, 2, 100); // Standing left
}else if(pointx>0){
sprite.setAnimation(6, 2, 100); // Standing right
}else if(pointy<0){
sprite.setAnimation(7, 2, 100); // Standing up
}else if(pointy>0){
sprite.setAnimation(4, 2, 100); // Standing down
}
}else{
if(pointx<0){
sprite.setAnimation(1, 2, 100); // Walking left
}else if(pointx>0){
sprite.setAnimation(2, 2, 100); // Walking right
}else if(pointy<0){
sprite.setAnimation(3, 2, 100); // Walking up
}else if(pointy>0){
sprite.setAnimation(0, 2, 100); // Walking down
}
}
//movement here.
positionx += dx*delta*speed;
positiony += dy*delta*speed;

System.out.println(dx*delta*speed);

sprite.setPosition((int)positionx, (int)positiony);
}

最佳答案

1# It flickers because the movement is defined a modifier: delta (to make smooth movement defined by the FPS). Actually, it never really reaches it's point (because it recalculates and never hits the position). How can I solve this?

如果你存储它移动的A点和B点,你可以设置一个时间间隔。每个时间间隔都会经过一段设定的距离,如果在一次迭代中对象走得太远,您可以为点 B 设置其坐标。这可以使用计时器轻松完成。这样,在一定时间后,它就会在你指定的位置上。

2# When 2 players join the same server, my character on the fastest computer runs faster. I think it's because of the FPS, how can that be solved?

如果您使用计时器,则与问题 #1 的答案相同。每个玩家将以相同的速度移动(因为每个玩家的流逝时间相同)。

底线:
fps 是可变的,而耗时对每个人都是相同的。

关于Java游戏 - 处理线性运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17954252/

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