This site requires JavaScript, please enable it in your browser!
Greenfoot back
Lildarkone
Lildarkone wrote ...

2011/6/18

Mouse-Dependant Rotation

Lildarkone Lildarkone

2011/6/18

#
How can you set the Rotation of an object depending on the current position of the mouse?
manster2008 manster2008

2011/6/19

#
use the following code:
MouseInfo m = getWorld().getMouseInfo();
if(m != null) {
     int x = m.getX() - getX();
     int y = m.getY() - getY();
     double r = Math.toDegrees(Math.atan2(x, y));
     setRotation(-(int) r);
}
now in the part where you set the Rotation, you may have to add or subtract 90, and remove the negative or something, because that depends on the rotation of your object.
You need to login to post a reply.