我目前有代码将生成分段点,并且在每个分段点将在该点周围的短圆柱内生成一些点(在3D中,分段位置都具有0.01F的az值(但是我喜欢具有变化的一行中的z值 - 即它仍然在一条线上,但在线z = 3x例如),但x和y是随机的)。然而,目前产生的所有点都在朝向上方的圆柱体中,我希望能够旋转这些点,使得它们产生的圆柱体被生成。 in面向两个部分之间的方向。 Here's图片显示的内容与目前的样子相似
我发现this关于轴周围旋转点的类似问题;我接受了答案,并将该代码用于我的RotatePoints()函数,但它似乎没有正常工作,我不知道为什么。下面是我的伪代码,我需要做什么才能让这个函数正常工作?有一个更好的方法吗?这些点只需要在旋转的圆柱体内生成,那么完全不同的方法会更有效,更容易吗?
我所拥有的只是每个片段的位置,每个点在本地空间中存储为Vector3 {x,y,z}。
伪代码
double radius;
// Generates the positions where the points will be generated around
// These are just the x,y,z positions of the object in world space
Vector3[] segmentLocations = GenerateSegmentPositions(numSegments);
for (int i = 0; i < numSegments; i++) {
// Generates points in a cylinder facing up the +ve y-axis
// This works fine
Vector3[][] pointsAroundSegment = GeneratePoints(segmentLocations[i], radius);
if (i != numSegments - 1 && i > 0) {
// Generate a normalise direction vector for the new direction
Vector3 newDir = Vector3.Normalise(segmentLocations[i + 1] - segmentLocations[i]);
double theta = Vector3.AngleBetween(newDir - Vector3.Normalise(segmentLocations[i] - segmentLocations[i - 1]));
// Rotates points (this currently rotates the points so they 'should' be facing the new direction, I haven't yet modified this to face the halfway point)
// This doesn't work
pointsAroundSegment = RotatePoints(pointsAroundSegment, newDir, theta/2);
} else if (i == numSegments - 1) {
// Generate final point
// This works fine
pointsAboutSegment = GenerateFinalPoint(segmentLocations[i]);
}
}
// This is the actual rotation function
// RotatePoints() effectively just calls this for each point in the array
public static double[] Rotate(double x, double y, double z, double u, double v, double w, double theta) {
double[] c = new double[3];
c [0] = u * (u * x + v * y + w * z) * (1 - Math.Cos (theta)) + x * Math.Cos (theta) + (-w * y + v * z) * Math.Sin (theta);
c [1] = v * (u * x + v * y + w * z) * (1 - Math.Cos (theta)) + y * Math.Cos (theta) + (w * x - u * z) * Math.Sin (theta);
c [2] = w * (u * x + v * y + w * z) * (1 - Math.Cos (theta)) + z * Math.Cos (theta) + (-v * x + u * y) * Math.Sin (theta);
return c;
}
答案 0 :(得分:2)
答案由Poosh提供;
使用标准化的(u ^ 2 + v ^ 2 + w ^ 2 = 1)方向向量,通过(a,b,c)旋转点(x,y,z),角度θ使用以下功能:
public static double[] Rotate(double x, double y, double z, double a, double b, double c, double nu, double nv, double nw, double theta) {
double[] rP = new double[3];
rP [0] = (a * (nv * nv + nw * nw) - nu * (b * nv + c * nw - nu * x - nv * y - nw * z)) * (1 - Math.Cos (theta)) + x * Math.Cos (theta) + (-c * nv + b * nw - nw * y + nv * z) * Math.Sin (theta);
rP [1] = (b * (nu * nu + nw * nw) - nv * (a * nu + c * nw - nu * x - nv * y - nw * z)) * (1 - Math.Cos (theta)) + y * Math.Cos (theta) + (c * nu - a * nw + nw * x - nu * z) * Math.Sin (theta);
rP [2] = (c * (nu * nu + nv * nv) - nw * (a * nu + b * nv - nu * x - nv * y - nw * z)) * (1 - Math.Cos (theta)) + z * Math.Cos (theta) + (-b * nu + a * nv - nv * x + nu * y) * Math.Sin (theta);
return rP;
}