我应该设置什么值:'setLinearVelocity(x,y);'对象身体,如果我想从玩家身体的中心射击到我的鼠标位置?
我有以下变量:mouseX,mouseY,playerX,playerY。
答案 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);