【问题标题】:Need help in animation Opengl c/c++在动画方面需要帮助 Opengl c/c++
【发布时间】:2015-11-13 00:05:18
【问题描述】:

当一个点到达底部时,我试图将其弹起。不幸的是,它一直在下降。代码如下:

void Timer(int value) {
    if (value) {
        if(initMpoints[1][1] <= 500 && initMpoints[0][1] >= 0) {
            txForC -= 0;
            tyForC -= 5;            //Move C downward.
            txForM -= 0;
            tyForM -= 5;
            if (initMpoints[0][1] == 0) {
                txForC += 0;
                tyForC += 5;            //Move C downward.
                txForM += 0;
                tyForM += 5;
            }
        }

    }
    glutPostRedisplay();
    glutTimerFunc(50, Timer, value);
}

我该如何解决这个问题?

【问题讨论】:

  • 这里的代码还没有完全完成。如果我从一开始就做了什么,请告诉我。提前致谢。
  • 看来是learn how to use a debugger 的好机会。
  • 能贴出更有意义的代码sn-p吗?为什么在initMpoints[0][1] == 0 时撤消对txForC 等的更改,而不是简单地从第一个if() 语句中排除这种情况?这些变量代表什么?您的代码如何知道某个点何时到达底部?你有没有检查过如果点超过底部位置会发生什么?

标签: c++ c opengl animation


【解决方案1】:

可以通过使用abs(sin(x)) 函数来完成弹跳胺化的非常好的和更自然的示例。

在你的情况下,我将像这样重写你的函数。

// Speed of bouncing
float fSpeed = 0.01f;
float fPI = 3.1415926f;

// Height of bouncing
float fHeight = 100.0f;

void Timer(int iValue)
{
    static float fAngle = 0.0;

    // if animation enabled
    if (iValue)
    {
        tyForC = fHeight * abs(sin(fAngle));
        tyForM = fHeight * abs(sin(fAngle));

        // Go to next position
        fAngle += fSpeed;
    }

    // Restore previous value to avoid bitgrid overflow
    if (fAngle >= (4.0f * fPI - fSpeed / 2.0))
    {
        fAngle = 0.0f;
    }

    glutPostRedisplay();
    glutTimerFunc(50, Timer, iValue);
}

在您的情况下,对于类似动画的牙齿,代码将类似于

float fHeight = 100.0f;

void Timer(int iValue)
{
    static float deltaC = 5.0f;
    static float deltaM = 5.0f;

    if (iValue)
    {
        tyForC += deltaC;
        tyForM += deltaM;

        if (tyForC <= 0 || tyForC >= fHeight)
            deltaC *= -1.0f;

        if (tyForM <= 0 || tyForM >= fHeight)
            deltaM *= -1.0f;
    }

    glutPostRedisplay();
    glutTimerFunc(50, Timer, iValue);
}

【讨论】:

  • @MC Mad Moefat:我回答你的问题了吗?
  • 谢谢它真的有帮助...如果我的编程不好,我很抱歉。不过还在学习。谢谢。
猜你喜欢
  • 2010-10-07
  • 1970-01-01
  • 2012-08-26
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多