在C++ OpenGL中制作一个小Pong游戏时,我认为在东西弹起时创建圆弧(半圆)会很有趣。我决定暂时跳过贝塞尔曲线,只使用直代数,但是我并没有走太远。我的代数遵循一个简单的二次函数(y = +-sqrt(mx + c))。
这个小节选只是我尚未完全参数化的一个示例,我只是想看看它的外观。但是,当我绘制此图形时,它会给我一条垂直的直线,该直线的切线接近-1.0 / 1.0。
这是GL_LINE_STRIP样式的限制吗?还是有更简单的方法绘制半圆/圆弧?还是我只是完全错过了明显的事情?
void Ball::drawBounce()
{ float piecesToDraw = 100.0f;
float arcWidth = 10.0f;
float arcAngle = 4.0f;
glBegin(GL_LINE_STRIP);
for (float i = 0.0f; i < piecesToDraw; i += 1.0f) // Positive Half
{ float currentX = (i / piecesToDraw) * arcWidth;
glVertex2f(currentX, sqrtf((-currentX * arcAngle)+ arcWidth));
}
for (float j = piecesToDraw; j > 0.0f; j -= 1.0f) // Negative half (go backwards in X direction now)
{ float currentX = (j / piecesToDraw) * arcWidth;
glVertex2f(currentX, -sqrtf((-currentX * arcAngle) + arcWidth));
}
glEnd();
}
提前致谢。
最佳答案
sqrtf((-currentX * arcAngle)+ arcWidth)的作用是什么?当i> 25时,该表达式变为虚构的。正确的方法是使用 sin()/ cos()生成问题中所述的半圆的X和Y坐标。如果要使用抛物线,则更简洁的方法是计算 y = H-H(x / W)^ 2