gpt4 book ai didi

java - 如何让我的主角朝某个物体移动? LibGDX/Java

转载 作者:行者123 更新时间:2023-12-01 11:48:17 26 4
gpt4 key购买 nike

我想要发生的是让我的玩家从一个 Spring 跳到另一个 Spring 。例如,如下图所示:

enter image description here

这是我的代码(到目前为止我尝试做的事情)。这在 update() 方法

中被调用
public void springCollision(Box containsSpring) {
// player object
Player player = ((Player) this.object);
// first spring
Spring boxSpring = containsSpring.getSpring();
// second spring
Spring platformSpring = containsSpring.getPartnerPlatform().getSpring();
// if player collides with first spring
if (player.getRect().overlaps(boxSpring.getRect())) {
// distance in x between first & second spring
float dx = platformSpring.getxPos()
+ platformSpring.getSprite().getWidth()
- boxSpring.getxPos() - player.getSprite().getWidth();
// distance in y between first & second spring
float dy = platformSpring.getyPos() - boxSpring.getyPos();
Vector2 directionToSpring = new Vector2(dx, dy);
// normalise vector then set player speed
player.setxSpeed(directionToSpring.nor().x);
player.setySpeed(directionToSpring.nor().y + player.getGravity());
}
}

目前发生的情况是,当他跳到 Spring 上时,他的跳跃会继续,但由于某种原因以慢动作进行。谁能明白为什么我的算法不起作用?如果评论不清楚,请告诉我

最佳答案

我认为问题在于您将玩家的速度设置为单位方向 vector 的值。单位 vector 的总体大小为 1,x 和 y 分量将具有 0-1 之间的某个值。这可以解释为什么如果你在 x 方向上的速度小于 1,那么你的玩家移动得如此缓慢,而在 y 方向上,影响速度的主要因素可能是重力(如果它是一个大于 1 的数字)。所以你需要乘以方向由某个力/速度值 vector 。

如何确定该值取决于您希望玩家对 Spring 的 react 。您是否希望玩家简单地以与撞击 Spring 相同的速度移动,并且仅在撞击 Spring 时改变方向?或者你希望 Spring 为玩家提供力量,让他进入下一个 Spring ?或者也许是两者的某种组合,比如玩家撞击 Spring 的速度决定了 Spring 将他推向另一个方向的力度?最简单的方法是保持相同的速度,仅改变方向,因此这是一个很好的起点,然后您可以在此基础上继续构建。

关于java - 如何让我的主角朝某个物体移动? LibGDX/Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28976945/

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