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