【问题标题】:Moving a body to specific point [Box2D , Libgdx]将身体移动到特定点 [Box2D , Libgdx]
【发布时间】:2014-07-22 00:09:43
【问题描述】:

当我的角色靠近硬币时,我正在尝试制作一个简单的磁铁效果,这应该会移动到他的位置

我有这个:

if(Mycharacter.position.x +2  >= position.x)    //position = coin position
                {

            body.setTransform(Mycharacter.position.x, Mycharacter.position.y, 0);
                 }

这接近我想要的,但我需要能够看到硬币对我的角色的移动。

我对 Box2d 和 Libgdx 还是很陌生,所以如果可能的话,请保持非常简单,这无助于我在物理方面很糟糕。提前致谢。

【问题讨论】:

标签: java libgdx box2d


【解决方案1】:

我找到了答案:

if("Any condition")
{
  body.setLinearVelocity((Character.position.x - position.x) * Velocity, (Character.position.y - position.y)* Velocity);
}

提醒,这只是实现我想要的最简单的方法。我相信有更合适的方法来做到这一点。

【讨论】:

    【解决方案2】:

    你可以这样做:

    1. Compute vector from you to coin (vx = coin.x - player.x, same for y)
    2. Compute the vectors's distance (using MathUtils.sqrt(vx*vx+vy*vy))
    3. If distance is shorter than magnet range, then reduce the distace.
    4. recompute new vector from player to coin (vx *= (newDistance/oldDistance))
    5. update coin'S position
    

    【讨论】:

    • 这将是一个非 Box2D 解决方案。使用 Box2D 时,第 4 步将向身体施加力,第 5 步将由 Box2D 计算。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多