Java 放松朝向鼠标的旋转

Java 放松朝向鼠标的旋转,java,rotation,processing,trigonometry,easing-functions,Java,Rotation,Processing,Trigonometry,Easing Functions,下面的代码使用一个简单的ease函数将线旋转到鼠标位置,但问题是atan2()方法的工作形式是-PI到PI,当角度达到任一限制时,使线向后旋转,我可以使它从0旋转到2_π,但没有什么不同,因为直线将向后旋转,直到到达目标角度,如果我不使用缓和计算,效果很好,因为从-PI到π的跳跃是不可见的,那么如何缓和我的旋转并避免此问题 float angle = 0; float targetAngle = 0; float easing = 0.1; void setup() { size(320,

下面的代码使用一个简单的ease函数将线旋转到鼠标位置,但问题是atan2()方法的工作形式是-PI到PI,当角度达到任一限制时,使线向后旋转,我可以使它从0旋转到2_π,但没有什么不同,因为直线将向后旋转,直到到达目标角度,如果我不使用缓和计算,效果很好,因为从-PI到π的跳跃是不可见的,那么如何缓和我的旋转并避免此问题

float angle = 0;
float targetAngle = 0;
float easing = 0.1;

void setup() {
  size(320, 240);
}

void draw() {
  background(200);
  noFill();
  stroke( 0 );

  // get the angle from the center to the mouse position
  angle = atan2( mouseY - height/2, mouseX - width/2 );
  // check and adjust angle to go from 0 to TWO_PI
  if ( angle < 0 ) angle = TWO_PI + angle;

  // ease rotation
  targetAngle += (angle - targetAngle) * easing;

  pushMatrix();
  translate( width/2, height/2 );
  rotate( targetAngle );
  line( 0, 0, 60, 0 );
  popMatrix();
}
浮动角度=0;
浮动目标角=0;
浮动利率=0.1;
无效设置(){
尺寸(320240);
}
作废提款(){
背景(200);
noFill();
冲程(0);
//获取从中心到鼠标位置的角度
角度=atan2(鼠标-高度/2,鼠标-宽度/2);
//检查并调整角度,使其从0变为2_PI
如果(角度<0)角度=两个π+角度;
//放松旋转
目标角+=(角度-目标角)*缓和;
pushMatrix();
平移(宽度/2,高度/2);
旋转(目标角);
线(0,0,60,0);
popMatrix();
}

谢谢

我不确定我是否正确理解了这个问题。。。您的意思是,如果鼠标位置略小于+PI,并且
targetAngle
略大于-PI,则线会远离鼠标旋转吗?问题是,即使两个值都在相同的范围(-PI,PI),它们仍然可以彼此相差很远。您必须调整
角度
以适应当前
目标角度
值的PI邻域

// get the angle from the center to the mouse position
angle = atan2( mouseY - height/2, mouseX - width/2 );
// check and adjust angle to be closer to targetAngle
if ( angle < targetAngle - PI ) angle = angle + TWO_PI;
if ( angle > targetAngle + PI ) angle = angle - TWO_PI;
// get the angle from the center to the mouse position
angle = atan2( mouseY - height/2, mouseX - width/2 );
// calculate the shortest rotation direction
float dir = (angle - targetAngle) / TWO_PI;
dir = dir - Math.round(dir);
dir = dir * TWO_PI;

// ease rotation
targetAngle += dir * easing;