So, I'm trying to develop a 3d table tennis game, and I'm having problems when it comes to colliding the ball with the table. I have a bounding sphere for the ball and bounding box for the table, but the intersection isn't that accurate, and I'm guessing that the bounding box is incorrect since colliding the ball with the racket is good.
I'm trying to display the bounding box so that it would be easier to see where the error is, but I cant seem to implement the tutorials I've found with my code, or just don't know how. And, I'm I right in creating an AABB since my table isn't moving? If anyone could help, it would be greatly appreciated, or if anyone could suggest a better way to collide a ball with a box (if there is) thanks. I've pasted my bounding box detection, and the collision between the sphere and box. If more code is needed I'll post it. Thanks
private bool Ball_Table(Model model1, Matrix world1)
{
for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++)
{
BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere;
sphere1 = sphere1.Transform(world1);
if(table_box.Intersects(sphere1))
return true;
}
return false;
}
.
protected BoundingBox UpdateBoundingBox(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
table_box = new BoundingBox(min, max);
return table_box;
}