I'm implementing a Kinect application in XNA.
I'm pretty new on 3D programming and I'd like to know how to retrieve parameters such as radius or height from a cylinder model in order to create a bounding box around it for collision detection.
My problem is that my cylinders' position and angle are syncronized with the position of the forearm of the player in the field of the Kinect and so I don't know how to define the bounding box parameters (Center Min or Max values...).
Here is the code for my bounding box creation method:
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);
}
Here is the code for my collision detection method
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;
}