【问题标题】:How do I make my main character move towards an object? LibGDX/Java如何让我的主角朝着一个物体移动? LibGDX/Java
【发布时间】:2015-03-11 01:13:41
【问题描述】:

我想要让我的球员从弹簧弹跳到另一个弹簧。例如下图:

这是我的代码(到目前为止我一直在尝试做的事情)。这在 update()method 中调用

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());
        }
    }

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

【问题讨论】:

  • 你的意思是他从不退缩?也许你的重力是 5,而你的弹簧是 10,所以你的重力永远不会赶上。你在哪里告诉它你在空中,不再弹跳?

标签: java android libgdx box2d collision-detection


【解决方案1】:

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

如何确定该值取决于您希望玩家对弹簧的反应。您是否希望玩家以与弹簧相同的速度继续移动,并且仅在击中弹簧时改变方向?或者您是否希望弹簧为玩家提供将他送入下一个弹簧的力量?或者可能是两者的某种组合,比如玩家击中弹簧的速度决定了弹簧将他发射到另一个方向的力度?最简单的方法是保持相同的速度并且只改变方向,因此这是一个很好的起点,您可以在此基础上继续构建。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多