在OpenGL中沿椭圆路径进行动画处理

时间:2018-09-06 13:42:11

标签: c++ animation opengl drawing

我正在尝试使用OpenGL中的DDA算法使红色圆圈遵循半圆的路径。尽管圆在X轴上略有偏移,但随着半圆角度的增加而增加,我几乎拥有它。

enter image description here enter image description here

任何帮助将不胜感激!这是我的代码:

scrPt movecircle (scrPt p1, scrPt p2)
{
    scrPt circlePos;

    float angle, x = p1.x, y = p1.y, vectorX, vectorY;

    // Get tahe x distance between the two points
    int dx = p2.x - p1.x, steps;

    // Get the y distance between the two points
    int dy = p2.y - p1.y;

    // Get the length between the points
    float length = sqrt(dx*dx + dy*dy);

    if (fabs (dx) > fabs (dy)) 
    steps = fabs (dx); 
    else 
        steps = fabs (dy);

    // calculate the direction
    float xIncrement = float (dx) / float (steps); 
    float yIncrement = float (dy) / float (steps); 

    if (nextPos == 0)
    {
        for(int i = 0; i < steps; i++)
        {
            glClear(GL_COLOR_BUFFER_BIT);
            angle = PI * i / steps;
            vectorX = x + (length / 2) * cos(angle + theta);
            vectorY = y + dy / 2 + (length / 2) * sin(angle + theta);
            circlePos.x = round(vectorX - length / 2);
            circlePos.y = round(vectorY);
            drawCircle (circlePos.x, circlePos.y);
            drawArch();
            glFlush();
            usleep(3000);
        }
    }
    else
    {   
        for (int i = 0; i < steps; i++)
        {
            glClear(GL_COLOR_BUFFER_BIT);
            drawCircle (round(x),round(y));
            glFlush();
            usleep(3000);
            x += xIncrement; 
            y += yIncrement;
        }
    }

    return circlePos;
}

1 个答案:

答案 0 :(得分:0)

for循环中存在几个导致问题的错误。我需要改变

此:

vectorX = x + (length / 2) * cos(angle + theta);

对此:

vectorX = x + (dx / 2) + (length / 2) * cos(angle + theta);

这:

circlePos.x = round(vectorX - (length / 2));

对此:

circlePos.x = round(vectorX);