[英]Create a 3D sphere and 3D box C++
I need to implement a tool to detect intersction between a 3D box and 3D Sphere in c++. 我需要实现一个工具来检测c ++中3D盒子和3D Sphere之间的相交。 Write now I find a way how to detect the intersection using that code.
现在写,我找到了一种使用该代码检测交点的方法。
inline float squared(float v) { return v * v; } bool doesCubeIntersectSphere(vec3 C1, vec3 C2, vec3 S, float R) { float dist_squared = R * R; /* assume C1 and C2 are element-wise sorted, if not, do that now */ if (SX < C1.X) dist_squared -= squared(SX - C1.X); else if (SX > C2.X) dist_squared -= squared(SX - C2.X); if (SY < C1.Y) dist_squared -= squared(SY - C1.Y); else if (SY > C2.Y) dist_squared -= squared(SY - C2.Y); if (SZ < C1.Z) dist_squared -= squared(SZ - C1.Z); else if (SZ > C2.Z) dist_squared -= squared(SZ - C2.Z); return dist_squared > 0; }
What I need is an example of C++ code to create a 3D sphere using origin vector and a radius and 我需要的是一个C ++代码示例,该示例使用原点矢量和半径来创建3D球体,
I need to create a 3D sphere using origin vector and a radius and a 3D box through its maximum and minimum corner vector. 我需要使用原点向量和半径以及通过其最大和最小拐角向量创建3D框来创建3D球面。
I may be mistaken, but (assuming Axis-Aligned boxes): The length of a vector from origin to corner C1 or C2 should be the radius r, right? 我可能会弄错,但是(假设轴对齐的框):从原点到角C1或C2的向量的长度应为半径r,对不对?
Explanation for my deriviation below: An Axis-Aligned-box with equal distance from center to all corners is a perfect cube. 以下是我的推导解释:从中心到所有角的距离相等的Axis-Aligned框是理想的立方体。 Translating such a cube to the origin puts two of the corners exactly at the line from the origin through the point {x=1,y=1,z=1}.
将这样的立方体转换为原点将使两个角恰好位于从原点到点{x = 1,y = 1,z = 1}的线上。 Thus those two corners will have coordinates {d,d,d} and {-d, -d, -d}, where d is "distance" of the corner along the axises X,Y,Z.
因此,这两个角将具有坐标{d,d,d}和{-d,-d,-d},其中d是角在X,Y,Z轴上的“距离”。 The distance to say the first corner is squaring and adding all components of the vector, and taking the square root, eg:
要说的第一个角的距离是对向量的所有分量进行平方和相加,并取平方根,例如:
|C1| | C1 | = |{d,d,d}|
= | {d,d,d} || = sqrt(d * d + d * d + d * d) = sqrt(3 * d * d)
= sqrt(d * d + d * d + d * d)= sqrt(3 * d * d)
Therefore solve: 因此解决:
r = sqrt(3 *d * d) r = sqrt(3 * d * d)
<=> <=>
r * r = 3 * d * d r * r = 3 * d * d
<=> <=>
d = sqrt(r*r/3) d = sqrt(r * r / 3)
<=> <=>
d = r/sqrt(3) d = r / sqrt(3)
This needs to be translated back to the center of the Sphere, thus: 这需要转换回球体的中心,因此:
C1 = { S.x+d, S.y+d, S.z+d} C1 = {S.x + d,S.y + d,S.z + d}
C2 = { Sx-d, Sy-d, Sz-d} C2 = {Sx-d,Sy-d,Sz-d}
Your explanation is a little vague, so I made some assumptions. 您的解释有点含糊,所以我做了一些假设。 Perhaps I'm dead wrong.
也许我完全错了。 Anyway here is some non-tested code showing what I mean:
无论如何,这里有一些未经测试的代码显示了我的意思:
void makeCube(vec3 S, float R, vec3* C1, vec3* C2)
{
static const float sqrt_one_third = sqrtf(1.0f/3.0f);
float d = R * sqrt_one_third;
C1->X = S.X + d;
C1->Y = S.Y + d;
C1->Z = S.Z + d;
C2->X = S.X - d;
C2->Y = S.Y - d;
C2->Z = S.Z - d;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.