0

What values should I set to: '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.

Vilda
  • 1,675
  • 1
  • 20
  • 50
  • The downvote is probably cause this question have been answered before,you should make a search first before opening a new question – SteveL May 17 '14 at 14:52

1 Answers1

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);
SteveL
  • 3,331
  • 4
  • 32
  • 57