0

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;
}
4

1 回答 1

0

我认为你把事情复杂化了。这是我的想法:

class Ball
{
    public Vector3 Position;
    public float Radius;

    // ...
}

class Table
{
    public Vector3 Position;
    public SizeF Size;

    // ...

    public bool CheckForCollision(Ball ball)
    {
        Vector3 upperLeftCorner = new Vector3(
                this.Position.X - this.Size.Width,
                this.Position.Y,
                this.Position.Z - this.Size.Heigth),
            lowerRightCorner = new Vector3(
                this.Position.X + this.Size.Width,
                this.Position.Y,
                this.Position.Z + this.Size.Height);

        if(ball.Position.X < upperLeftCorner.X ||
            ball.Position.X > lowerRightCorner.X)
        {
            return false;
        }

        if(ball.Position.Z < upperLeftCorner.Z ||
            ball.Position.Z > lowerRightCorner.Z)
        {
            return false;
        }

        if(ball.Position.Y - ball.Radius > this.Position.Y)
        {
            return false;
        }

        return true;
    }
}

自从我上次使用 XNA 已经有一段时间了,所以我可能把坐标搞砸了,但这就是我的意思:

  origin
    +---->  X, Width
    |
    |  upperLeftCorner
    |     +----------------+
    v     |                |
          |                |
    y,    |                |
  height  |                |
          |                |
          |        +       |
          | table.position |
          |                |
          |                |
          |                |
          |                |
          +----------------+
                       lowerRightCorner
于 2012-03-05T21:02:59.313 回答