【问题标题】:Rotation of a boat animation in C++C ++中船动画的旋转
【发布时间】:2017-04-04 05:50:28
【问题描述】:

我正在用 C++ 创建一个攻击船游戏,但我的船在屏幕上跟随鼠标时出现问题。我的计划是让船像船一样跟随鼠标(缓慢旋转,而不是瞬时旋转,大约需要 4 秒来完成 360 转),并且在大多数情况下它会做它应该做的事情。

当鼠标位于屏幕左侧时(当我的鼠标越过 -x 轴时)时会发生该错误,因为船跟随鼠标,船会转向错误的方向并进行 360 度旋转,而不是跟随鼠标。

这是我用来转船的代码。

        angle = atan2(delta_y, delta_x) * 180.0 / PI; 

        //Rotate the boat towards the mouse and
        //make the boat turn more realistically
        if (angle - rotate > 0) {
            rotate += 1.0f; // turns left
        } else if (angle - rotate < 0) {
            rotate -= 1.0f; // turns right
        }
        if (angle - rotate >= 360.0f) {
            rotate = 0.0f;
        }` 

【问题讨论】:

    标签: c++ animation rotation 2d glut


    【解决方案1】:

    您忘记钳制角度差。它应该在区间&lt;-pi,+pi&gt; [rad] 上,所以这个区间之外的任何角度差异都会导致这样的问题。试试这个:

    angle = atan2(delta_y, delta_x) * 180.0 / PI;  // target [deg]
    da = angle-rotate; // unclamped delta [deg]
    while (da<-180.0f) da+=360.0f;
    while (da>+180.0f) da-=360.0f;
         if (da >= +1.0f) rotate += 1.0f;
    else if (da <= -1.0f) rotate -= 1.0f;
    else                  rotate  = 0.0f;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 2011-05-20
      相关资源
      最近更新 更多