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
注意:默认的播放器图像指向上方
3 个解决方案
#1
2
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:
你必须用三角函数来计算旋转的角度。为此,您首先需要获取图像和光标的位置。我不能告诉你如何得到图像的位置,因为这可能会有所不同。对于这个例子(改编自这里),我假设imageX和imageY是图像的x和y位置:
float xDistance = cursorX - imageX;
float yDistance = cursorY - imageY;
double rotationAngle = Math.toDegrees(Math.atan2(yDistance, xDistance));
#2
2
To find the angle from a coordinate (0,0) to another coordinate (x,y), we can use the trigonometric function tan^-1(y/x).
找到角从一个坐标(0,0)到另一个坐标(x,y),我们可以使用三角函数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.)
Java的数学类指定一个静态方法量化充当tan ^ 1函数(也称为“应该”,因此“:”)并返回角的弧度。(有一个方法atan,取一个参数。看到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:
为了找到从“player”坐标到鼠标光标坐标的角度(我假设您提到的这个“player”有x和y坐标),我们需要做如下事情:
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".
同样值得注意的是,0弧度的角度将表明鼠标直接位于播放器的右边。你提到“默认玩家形象”指向上方;如果你的意思是在旋转之前,你的图像对玩家来说是朝上的,那么在几何学和atan2的Java实现中,你的玩家的脸应该是“默认的”。
#3
0
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.
如果需要鼠标来更新窗口中的鼠标位置,请参阅mouseMotionListener。你用来获取鼠标位置的电流相对于整个屏幕。记住这一点。
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.
在的话,你得到的每一个角θs如下图所示,检查如果x或y是0,使一个特例。
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.
原点在图片的中间,每个点(用手绘的十字标记)都是鼠标的位置。
#1
2
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:
你必须用三角函数来计算旋转的角度。为此,您首先需要获取图像和光标的位置。我不能告诉你如何得到图像的位置,因为这可能会有所不同。对于这个例子(改编自这里),我假设imageX和imageY是图像的x和y位置:
float xDistance = cursorX - imageX;
float yDistance = cursorY - imageY;
double rotationAngle = Math.toDegrees(Math.atan2(yDistance, xDistance));
#2
2
To find the angle from a coordinate (0,0) to another coordinate (x,y), we can use the trigonometric function tan^-1(y/x).
找到角从一个坐标(0,0)到另一个坐标(x,y),我们可以使用三角函数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.)
Java的数学类指定一个静态方法量化充当tan ^ 1函数(也称为“应该”,因此“:”)并返回角的弧度。(有一个方法atan,取一个参数。看到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:
为了找到从“player”坐标到鼠标光标坐标的角度(我假设您提到的这个“player”有x和y坐标),我们需要做如下事情:
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".
同样值得注意的是,0弧度的角度将表明鼠标直接位于播放器的右边。你提到“默认玩家形象”指向上方;如果你的意思是在旋转之前,你的图像对玩家来说是朝上的,那么在几何学和atan2的Java实现中,你的玩家的脸应该是“默认的”。
#3
0
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.
如果需要鼠标来更新窗口中的鼠标位置,请参阅mouseMotionListener。你用来获取鼠标位置的电流相对于整个屏幕。记住这一点。
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.
在的话,你得到的每一个角θs如下图所示,检查如果x或y是0,使一个特例。
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.
原点在图片的中间,每个点(用手绘的十字标记)都是鼠标的位置。