gpt4 book ai didi

c++ - 如何正确旋转 vector 以匹配另一个 vector ? (打开)

转载 作者:太空狗 更新时间:2023-10-29 23:13:41 26 4
gpt4 key购买 nike

一段时间以来,我一直在为以下问题而苦苦挣扎:我有一个程序允许用户绘制各种长度的贝塞尔曲线(第一个为 4 个点,所有其他为 3 个点,只需将曲线一个接一个地连接起来)。我需要沿着曲线放置小的矩形火车轨道,让用户绕着他们制作的轨道行驶。我已经定义了顶点,并制定了将它们正确放置在线上的方法,但事实证明旋转很棘手。对于平滑曲线,我当前的实现工作正常,但尖角会导致轨道不再对齐,并且所有经过该角的轨道都具有完全相同的旋转,从而完全破坏它。所有相关代码如下:

用点填充曲线 std::vector 的代码:

p0 = glm::vec2(pointVertexData.at(0), pointVertexData.at(1));
p1 = glm::vec2(pointVertexData.at(3), pointVertexData.at(4));
p2 = glm::vec2(pointVertexData.at(6), pointVertexData.at(7));
p3 = glm::vec2(pointVertexData.at(9), pointVertexData.at(10));
curveVertexData = Subdivide(0.0f, 1.0f, 0.05f, curveVertexData);
for (int i = 0; i < timesToLoop; i++)
{
p0 = p3;
p1 = glm::vec2(pointVertexData.at(n), pointVertexData.at(n+1));
p2 = glm::vec2(pointVertexData.at(n+3), pointVertexData.at(n+4));
p3 = glm::vec2(pointVertexData.at(n+6), pointVertexData.at(n+7));
std::vector<GLfloat> tempVec = Subdivide(0.0f, 1.0f, 0.05f, tempVec);
curveVertexData.insert(curveVertexData.end(), tempVec.begin()+3, tempVec.end());
}

segmentation 代码:

std::vector<GLfloat> Subdivide(GLfloat u0, GLfloat u1, GLfloat maxLineLength, std::vector<GLfloat> recurVertices)
{
GLfloat umid = (u0 + u1) / 2.0;
glm::vec2 x0 = Interpolate(p0, p1, p2, p3, u0, pFinal);
glm::vec2 x1 = Interpolate(p0, p1, p2, p3, u1, pFinal);
GLfloat length = sqrt(pow((x1.x - x0.x), 2) + pow((x1.y - x0.y), 2));
if (length > maxLineLength)
{
std::vector<GLfloat> firstVertices = Subdivide(u0, umid, maxLineLength, firstVertices);
std::vector<GLfloat> secondVertices = Subdivide(umid, u1, maxLineLength, secondVertices);
secondVertices.insert(secondVertices.begin(), firstVertices.begin(), firstVertices.end()-3);
recurVertices = secondVertices;
return recurVertices;
}
else
{
recurVertices.push_back(x0.x);
recurVertices.push_back(x0.y);
recurVertices.push_back(0.1f);
recurVertices.push_back(x1.x);
recurVertices.push_back(x1.y);
recurVertices.push_back(0.1f);
numberOfVertices += 6;
return recurVertices;
}
}

设置带有轨道顶点的 std::vector 的代码:

    std::vector<GLfloat> tempVertices;
numberOfTrackVertices = 0;
for (int i = 0; i < curveVertexData.size() - 2; i+=3)
{
std::cout << "Now calculating point # " << i << " : ";
if(i != 0 && i < curveVertexData.size() - 5)
shiftVertices(trainVertices, curveVertexData[i], curveVertexData[i + 1], curveVertexData[i + 2], curveVertexData[i + 3], curveVertexData[i + 4], curveVertexData[i + 5], curveVertexData[i - 3], curveVertexData[i - 2], curveVertexData[i - 1], &tempVertices);
else if (i == 0)
shiftVertices(trainVertices, curveVertexData[i], curveVertexData[i + 1], curveVertexData[i + 2], curveVertexData[i + 3], curveVertexData[i + 4], curveVertexData[i + 5], curveVertexData[i], curveVertexData[i + 1], curveVertexData[i + 2], &tempVertices);
else
shiftVertices(trainVertices, curveVertexData[i], curveVertexData[i + 1], curveVertexData[i + 2], curveVertexData[i], curveVertexData[i + 1], curveVertexData[i + 2], curveVertexData[i - 3], curveVertexData[i - 2], curveVertexData[i - 1], &tempVertices);
}

最后,我认为最有可能是罪魁祸首的代码,用于改变轨道旋转的代码。我目前的算法如下:(请注意,“currentOrientation”设置为等于顶点的前两个元素彼此相减的原因是因为它们代表矩形的后下角,当彼此相减时,给出一个 vector 表示哪个方向盒子是有方向的)

    void shiftVertices(GLfloat inVertices[], GLfloat x, GLfloat y, GLfloat z, GLfloat rx, GLfloat ry, GLfloat rz, GLfloat qx, GLfloat qy, GLfloat qz, std::vector<GLfloat> *container)
{
glm::vec3 tempVectors[36];
glm::vec3 moveVector = glm::vec3(x, y, z);
glm::vec3 rotateVector = glm::normalize(glm::vec3(rx - qx, ry - qy, rz - qz));
rotateVector = glm::normalize(glm::cross(rotateVector, UP));
bool unFilled = true;
int i = 0;
int n = 0;
while(unFilled)
{
tempVectors[n].x = inVertices[i];
i++;
tempVectors[n].y = inVertices[i];
i++;
tempVectors[n].z = inVertices[i];
i++;
n++;
if (n >= 36)
unFilled = false;
}
glm::vec3 currentOrientation = glm::normalize(tempVectors[0] - tempVectors[1]);
GLfloat angleToRotate = glm::acos(glm::dot(currentOrientation, rotateVector));
angleToRotate = (180.0f * angleToRotate) / PI;
std::cout << angleToRotate << "\n";
glm::mat4 rotationMatrix;
rotationMatrix = glm::rotate(rotationMatrix, angleToRotate, UP);
for (int u = 0; u < 36; u++)
{
tempVectors[u] = glm::vec3(rotationMatrix * glm::vec4(tempVectors[u], 1.0));
tempVectors[u] = tempVectors[u] + moveVector;
}
i = 0;
n = 0;
unFilled = true;
while (unFilled)
{
container->push_back(tempVectors[n].x);
container->push_back(tempVectors[n].y);
container->push_back(tempVectors[n].z);
numberOfTrackVertices++;
n++;
if (n >= 36)
unFilled = false;
}
}

这个实现给出了以下结果: http://imgur.com/a/8OI2E (除最后一张图片外的所有图片。抱歉,它不允许我嵌入图片)

我为此查找了很多资源,但收效甚微。一种实现是 Jur van den Berg 对这个问题的回答:https://math.stackexchange.com/questions/180418/calculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d请注意,我将此称为图像中的“倾斜对称方法”。我的算法实现如下,并且如前所述,无法正常运行:(注意这段代码替换了上一个示例中的中间部分代码,前后循环保持不变)

        glm::vec3 crossVector = glm::cross(currentOrientation, rotateVector);
GLfloat sineAngle = crossVector.length();
GLfloat cosAngle = glm::dot(currentOrientation, rotateVector);
glm::mat3 experimentalRMatrix;
glm::mat3 skewSymmetric = { 0, (-1.0f * crossVector.z), crossVector.y,
crossVector.z, 0, (-1.0f *crossVector.x),
(-1.0f * crossVector.y), crossVector.x, 0 };
glm::mat3 skewSecond = skewSymmetric * skewSymmetric;
skewSecond = skewSecond * ((1.0f - cosAngle) / (sineAngle * sineAngle));
experimentalRMatrix = glm::mat3() + skewSymmetric + skewSecond;
testVector = experimentalRMatrix * currentOrientation;
rotationMatrix = glm::mat4(experimentalRMatrix);

所有这些都摆在桌面上,我希望分析为什么我尝试解决问题失败,和/或能够正确旋转顶点的解决方案。

谢谢你。

最佳答案

查看评论...切线空间和四元数似乎已经成功了

关于c++ - 如何正确旋转 vector 以匹配另一个 vector ? (打开),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37628585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com