简体   繁体   中英

Trying to make an object rotate towards the mouse in JavaFX

I've been trying to use the atan2 function to rotate an object (triangle) towards the mouse, but it can't seem to rotate correctly in one direction. any ideas?

Point p = MouseInfo.getPointerInfo().getLocation();
        double ydistance = p.y-triangle.getTranslateY();
        double xdistance = p.x-triangle.getTranslateX();
        double angle5 = Math.toDegrees(atan2(xdistance, ydistance));
        triangle.setRotate(angle5);

You have inverted the x and y

atan2 is defined as atan2(y, x) not atan2(x, y), as you might expect.

refer to https://www.geeksforgeeks.org/java-lang-math-atan2-java/

It is the counterclockwise angle, measured in radian, between the positive X-axis, and the point (x, y).

also note that radian 0 begins from positive X-Axis,Y=0 (East)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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