C# 检索三维圆柱体参数以创建边界框
我正在XNA中实现Kinect应用程序 我对3D编程相当陌生,我想知道如何从圆柱体模型中检索半径或高度等参数,以便在其周围创建边界框以进行碰撞检测 我的问题是,我的圆柱体的位置和角度与玩家前臂在Kinect中的位置同步,因此我不知道如何定义边界框参数(中心最小值或最大值…) 以下是创建边界框方法的代码:C# 检索三维圆柱体参数以创建边界框,c#,3d,xna,collision-detection,bounding-box,C#,3d,Xna,Collision Detection,Bounding Box,我正在XNA中实现Kinect应用程序 我对3D编程相当陌生,我想知道如何从圆柱体模型中检索半径或高度等参数,以便在其周围创建边界框以进行碰撞检测 我的问题是,我的圆柱体的位置和角度与玩家前臂在Kinect中的位置同步,因此我不知道如何定义边界框参数(中心最小值或最大值…) 以下是创建边界框方法的代码: private BoundingBox CalculateBoundingBox(Model model, Matrix worldTransform) { // Initialize
private BoundingBox CalculateBoundingBox(Model model, Matrix worldTransform)
{
// Initialize minimum and maximum corners of the bounding box to max and min values
Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
// For each mesh of the model
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
// Vertex buffer parameters
int vertexStride = meshPart.VertexBuffer.VertexDeclaration.VertexStride;
int vertexBufferSize = meshPart.NumVertices * vertexStride;
// Get vertex data as float
float[] vertexData = new float[vertexBufferSize / sizeof(float)];
meshPart.VertexBuffer.GetData<float>(vertexData);
// Iterate through vertices (possibly) growing bounding box, all calculations are done in world space
for (int i = 0; i < vertexBufferSize / sizeof(float); i += vertexStride / sizeof(float))
{
Vector3 transformedPosition = Vector3.Transform(new Vector3(vertexData[i], vertexData[i + 1], vertexData[i + 2]), worldTransform);
min = Vector3.Min(min, transformedPosition);
max = Vector3.Max(max, transformedPosition);
}
}
}
// Create and return bounding box
return new BoundingBox(min, max);
}
每次创建
transformedPosition
时,将其添加到列表中
然后使用该列表使用内置方法创建一个BoundingBox
。CreateFromPoints(MyListoftTransformedPositions)
该方法将返回正确的最小值和最大值
private bool isCollisionDetected(Model m1, Model m2)
{
bool detection;
BoundingBox b1 = CalculateBoundingBox(m1);
BoundingBox b2 = CalculateBoundingBox(m2);
if (b1.Intersects(b2))
{
detection = true;
}
else
{
detection = false;
}
return detection;
}