0
votes

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.

1
The downvote is probably cause this question have been answered before,you should make a search first before opening a new questionSteveL

1 Answers

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