MouseJointDef libgdx - 绘制像愤怒的小鸟一样的轨迹线

use*_*651 4 java box2d libgdx

在libgdx游戏中

我想触摸然后然后拖动某个地方,然后在释放(touchUp)上根据目标身体的距离和方向施加方向力.触地时,目标体保持静止,然后在触摸时沿着所需的轨迹施加力.

(非常类似于愤怒的小鸟 - 当你拿着弹弓的时候,你可以看到目标身体的虚线轨迹 - 我想做同样的事情)

所以我想这可能不是最困难的事情但是给了一些选项我倾向于使用MouseJointDef但它立即施加力(即目标立即移动 - 我希望它保持静止然后一旦触摸事件发生然后施加力量)

什么是绘制轨迹的最简单方法?我也使用Box2D.

Ali*_*aaa 7

创建一个继承InputAdapter类的类,然后创建它的实例并注册它以监听触摸输入.

    Gdx.input.setInputProcessor(inputAdapter);
Run Code Online (Sandbox Code Playgroud)

有3种方法来处理触摸事件touch_down,touch_dragged并且touch_up你必须重写.

touch_down,检查触摸位置是否在鸟类区域.如果是,请将布尔标志设为true.

touch_dragged,检查上面的标志,如果是,则计算触摸位置相对于鸟类拍摄中心的距离和拍摄角度.

touch_up,您可以通过致电命令以计算的金额进行拍摄

    body2shoot.applyLinearImpulse(impulse, body2shoot.getWorldCenter());
Run Code Online (Sandbox Code Playgroud)

没有必要MouseJointDef移动body2shoot.只需body2shoot在渲染的每个循环中设置要触摸的触摸位置的变换.

为了计算轨迹我写了一个这样的类:

public class ProjectileEquation
{
public float gravity;
public Vector2 startVelocity = new Vector2();
public Vector2 startPoint = new Vector2();

public ProjectileEquation()
{   }

public float getX(float t)
{
    return startVelocity.x*t + startPoint.x;
}

public float getY(float t)
{
    return 0.5f*gravity*t*t + startVelocity.y*t + startPoint.y;
}
}
Run Code Online (Sandbox Code Playgroud)

并绘制它只是我设置startPointstartVelocity,然后在一个循环中,我给一个t(时间)增量,并调用getX(t)getY(t).