我有一些物体正弦波移动。每次它到达“波浪”的顶部(或底部)时,我都必须为其设置动画。我想用导数函数来做到这一点:正如我们所知,它会改变那些点的值(从正到负或相反)。所以代码是:
// Start value
int functionValue = +1;
// Function
float y = k1 * sinf(k2 * Deg2Rad(x)) + y_base;
// Derivative function
float tempValue = -cosf(y);
// Check whether value is changed
if (tempValue * functionValue < 0)
{
animation = true;
}
functionValue = tempValue;
如果我将输出 tempValue
它会显示奇怪的数字:
0.851513
0.997643
0.0242145
0.690432
0.326303
-0.614262
0.892036
0.1348
0.709843
0.968676
0.0454846
0.920602
-0.423125
0.692132
-0.960107
0.0799654
-0.747722
-0.635241
0.148477
-0.98611
0.900912
-0.877801
0.811632
-0.362743
-0.233856
0.35512
-0.994107
0.885184
-0.468005
0.982489
0.675337
0.661048
0.870765
0.0312914
-0.319066
-0.602956
-0.996169
-0.95627
动画也很奇怪。不仅在浪潮的顶端。哪里出了问题?
最佳答案
你有
y = a * sin(b * x) + c
它的导数是
y' = a * b * cos(b * x)
不是
y' = -cos(y)
关于c++ - 正弦波和微分函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4503403/