在Box2D中拍摄(LibGDX)

时间:2014-05-17 13:37:29

标签: libgdx box2d bullet

我应该设置什么值:'setLinearVelocity(x,y);'对象身体,如果我想从玩家身体的中心射击到我的鼠标位置?

我有以下变量:mouseX,mouseY,playerX,playerY。

1 个答案:

答案 0 :(得分:1)

float velx = mouseX - playerX;
float vely = mouseY - playerY;
float length = Math.sqrt(velx * velx + vely * vely);
if (length != 0) {
      velx = velx / length;
      vely = vely / length;
}
float finalVelx = velx * speed;
float finalVely = vely * speed;

setLinearVelocity(finalVelx,finalVely);