简体   繁体   中英

Java: rotating image so that it points at the mouse cursor

I want the player image to point towards the mouse cursor. I use this code to get the postion of the mouse cursor:

private int cursorX = MouseInfo.getPointerInfo().getLocation().x;
private int cursorY = MouseInfo.getPointerInfo().getLocation().y;

Note: The default player image points upwards

You'll have to use trigonometry in order to calculate the angle of rotation. For that you'll first need to obtain the location of the image and the cursor. I cannot tell you how to get the position for the image as this may vary. For this example (adapted from here ), I'll assume imageX and imageY are the x and y positions of your image:

float xDistance = cursorX - imageX;
float yDistance = cursorY - imageY;
double rotationAngle = Math.toDegrees(Math.atan2(yDistance, xDistance));

To find the angle from a coordinate (0,0) to another coordinate (x,y), we can use the trigonometric function tan^-1(y/x).

Java's Math class specifies a static method atan2 which acts as a tan^-1 function (also known as "arctangent", hence "atan") and returns the angle in radians . (There is a method atan which takes one argument. See the linked Javadoc.)

In order to find the angle in degrees from the coordinate of your "player" to the coordinate of the mouse cursor, (I'll assume this "player" you make mention of has x and y coordinates), we need to do something like this:

double theta = Math.atan2(cursorY - player.getY(), cursorX - player.getX());

It is also of note that an angle of zero radians would indicate that the mouse is directly to the right of the player. You mention that the "default player image" points upwards; if you mean that before rotation, your image faces upward for the player, it would be more conventional to geometry and the Java implementation of atan2 to have your player face right "by default".

Though this was asked two years ago...

If you need the mouse to keep updating the mouse position in the window, see mouseMotionListener . The current you use to get the mouse position is relative to the whole screen. Just keep that in mind.

Otherwise, here is a method I use,

public double angleInRelation(int x1, int y1, int x2, int y2) {
    // Point 1 in relation to point 2
    Point point1 = new Point(x1, y1);
    Point point2 = new Point(x2, y2);
    int xdiff = Math.abs(point2.x - point1.x);
    int ydiff = Math.abs(point2.y - point1.y);
    double deg = 361;
    if ( (point2.x > point1.x) && (point2.y < point1.y) ) {
        // Quadrant 1
        deg = -Math.toDegrees(Math.atan(Math.toRadians(ydiff) / Math.toRadians(xdiff)));

    } else if ( (point2.x > point1.x) && (point2.y > point1.y) ) {
        // Quadrant 2
        deg = Math.toDegrees(Math.atan(Math.toRadians(ydiff) / Math.toRadians(xdiff)));

    } else if ( (point2.x < point1.x) && (point2.y > point1.y) ) {
        // Quadrant 3
        deg = 90 + Math.toDegrees(Math.atan(Math.toRadians(xdiff) / Math.toRadians(ydiff)));

    } else if ( (point2.x < point1.x) && (point2.y < point1.y) ) {
        // Quadrant 4
        deg = 180 + Math.toDegrees(Math.atan(Math.toRadians(ydiff) / Math.toRadians(xdiff)));

    } else if ((point2.x == point1.x) && (point2.y < point1.y)){
        deg = -90;
    } else if ((point2.x == point1.x) && (point2.y > point1.y)) {
        deg = 90;
    } else if ((point2.y == point1.y) && (point2.x > point1.x)) {
        deg = 0;
    } else if ((point2.y == point2.y) && (point2.x < point1.x)) {
        deg = 180;
    }
    if (deg == 361) {
        deg = 0;
    }
    return deg;
}

In words, you get the angle of each of the θs as shown in the picture below and check if x or y are 0 and make a special case for that.

The origin is the middle of the picture and each of the points (marked with a hand-drawn cross) is where the mouse position is.

有关鼠标位置的图片

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