问题是我有两个在三维空间中的点,其中y+向上,x+向右,z+朝向你。我想将一个长度等于两点之间距离的圆柱体定位在它们之间,使其两个中心端点接触这两个点。我已经将圆柱体平移到了两个点的中心位置,并需要帮助设计一个旋转矩阵来应用于圆柱体,以便它朝向正确的方向。整个事物的变换矩阵如下:
translate(中心点) * rotateX(一些X度数) * rotateZ(一些Z度数)
平移是最后应用的,这样我就可以在平移之前将其定位到正确的方向。
以下是我目前为止的进展:
mat4 getTransformation(vec3 point, vec3 parent)
{
float deltaX = point.x - parent.x;
float deltaY = point.y - parent.y;
float deltaZ = point.z - parent.z;
float yRotation = atan2f(deltaZ, deltaX) * (180.0 / M_PI);
float xRotation = atan2f(deltaZ, deltaY) * (180.0 / M_PI);
float zRotation = atan2f(deltaX, deltaY) * (-180.0 / M_PI);
if(point.y < parent.y)
{
zRotation = atan2f(deltaX, deltaY) * (180.0 / M_PI);
}
vec3 center = vec3((point.x + parent.x)/2.0, (point.y + parent.y)/2.0, (point.z + parent.z)/2.0);
mat4 translation = Translate(center);
return translation * RotateX(xRotation) * RotateZ(zRotation) * Scale(radius, 1, radius) * Scale(0.1, 0.1, 0.1);
}
我尝试了下面给出的解决方案,但似乎根本没有起作用
mat4 getTransformation(vec3 parent, vec3 point)
{
// moves base of cylinder to origin and gives it unit scaling
mat4 scaleFactor = Translate(0, 0.5, 0) * Scale(radius/2.0, 1/2.0, radius/2.0) * cylinderModel;
float length = sqrtf(pow((point.x - parent.x), 2) + pow((point.y - parent.y), 2) + pow((point.z - parent.z), 2));
vec3 direction = normalize(point - parent);
float pitch = acos(direction.y);
float yaw = atan2(direction.z, direction.x);
return Translate(parent) * Scale(length, length, length) * RotateX(pitch) * RotateY(yaw) * scaleFactor;
}
运行上述代码后得到以下结果:
![enter image description here](https://istack.dev59.com/shSSt.webp)
以下是我想做的事情的图像(请原谅我的绘画技巧)。
acos2
吗? - Beta