[英]Math calculation for angle between two points Java
我正在尝试使用lwjgl和opengl计算鼠标和“玩家” Im之间的角度。 这是旋转图像的用途:
GL11.glTranslatef(p.getPosition().x+playerTexture.getTextureWidth()/2, p.getPosition().y+playerTexture.getTextureHeight()/2, 0);
GL11.glRotated(0, 0.0f, 0.0f, 1.0f);
GL11.glTranslatef(-p.getPosition().x+-playerTexture.getTextureWidth()/2, -p.getPosition().y+-playerTexture.getTextureHeight()/2, 0);`
我已经尝试了两件事,但是每次它都会不断旋转图像。 我的尝试:
public float getAngle(Player p) {
float angle = (float) (Math.atan2(Mouse.getY() -
p.getPosition().y+playerTexture.getTextureHeight()/2 , Mouse.getX() -
p.getPosition().x+playerTexture.getTextureWidth()/2));
return angle;
}
public void calcAngle(Player p){
double y = Mouse.getY() - p.getPosition().y;
double x = Mouse.getX() - p.getPosition().x;
double dir = Math.atan2(y, x);
}
我希望有人知道解决方法。 谢谢。
我看不到您实际在哪里使用getAngle
方法,但是我怀疑您是直接在openGL旋转函数中使用从Math.atan2
计算的角度?
如果真是这样,我想你可能会混合使用单位。 我相信Math.atan2
返回弧度,而openGL期望度。 尝试使用Math.toDegrees(angle)
。
但是,我希望它显示出很小的角度,而不是像疯了一样旋转...
我想解决这个问题的第一件事就是将角度写入控制台。 是0、90、180等还是0、1.723、3.1431等?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.