0

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

我有这些变量可用:mouseX、mouseY、playerX、playerY。

4

1 回答 1

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);
于 2014-05-17T14:51:34.440 回答