简体   繁体   English

在Box2D(LibGDX)中拍摄

[英]Shooting in Box2D (LibGDX)

What values should I set to: 'setLinearVelocity(x, y);' 我应该设置什么值:'setLinearVelocity(x,y);' to object body, if I want to shoot it from the center of the player body to the location of my mouse? 如果要从播放器主体的中心射击到鼠标的位置,该怎么做?

I have these variables available: mouseX, mouseY, playerX, playerY. 我有以下可用变量:mouseX,mouseY,playerX,playerY。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM