3d 三维边界框边框xna

3d 三维边界框边框xna,3d,xna,collision,bounding-box,3d,Xna,Collision,Bounding Box,所以,我正在尝试开发一个3d乒乓球游戏,当球与桌子碰撞时,我遇到了问题。我有一个球的边界球体和桌子的边界框,但是交集不是那么精确,我猜边界框是不正确的,因为球与球拍碰撞是好的 我试图显示边界框,以便更容易地看到错误所在,但我似乎无法实现我在代码中找到的教程,或者只是不知道如何实现。而且,我在创建AABB时是对的,因为我的桌子没有移动?如果有人能帮上忙,我们将不胜感激,或者如果有人能建议一种更好的方法,用盒子碰撞球(如果有),谢谢。我已经粘贴了边界框检测,以及球体和框之间的碰撞。如果需要更多的代码

所以,我正在尝试开发一个3d乒乓球游戏,当球与桌子碰撞时,我遇到了问题。我有一个球的边界球体和桌子的边界框,但是交集不是那么精确,我猜边界框是不正确的,因为球与球拍碰撞是好的

我试图显示边界框,以便更容易地看到错误所在,但我似乎无法实现我在代码中找到的教程,或者只是不知道如何实现。而且,我在创建AABB时是对的,因为我的桌子没有移动?如果有人能帮上忙,我们将不胜感激,或者如果有人能建议一种更好的方法,用盒子碰撞球(如果有),谢谢。我已经粘贴了边界框检测,以及球体和框之间的碰撞。如果需要更多的代码,我会发布它。谢谢

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;
}
private bool Ball_表(模型1,矩阵世界1)
{       
对于(int-meshIndex1=0;meshIndex1

protectedboundingbox UpdateBoundingBox(模型、矩阵变换)
{
//将边界框的最小和最大角点初始化为最大和最小值
Vector3 min=新的Vector3(float.MaxValue、float.MaxValue、float.MaxValue);
Vector3 max=新的Vector3(float.MinValue、float.MinValue、float.MinValue);
//对于模型的每个网格
foreach(model.mesh中的ModelMesh网格)
{
foreach(ModelMeshPart meshPart中的meshPart.MeshParts)
{
//顶点缓冲区参数
int vertexStride=meshPart.VertexBuffer.VertexDeclaration.vertexStride;
int vertexBufferSize=meshPart.NumVertices*vertexStride;
//以浮点形式获取顶点数据
float[]vertexData=新浮点[vertexBufferSize/sizeof(float)];
meshPart.VertexBuffer.GetData(vertexData);
//通过顶点(可能)迭代生长边界框,所有计算都在世界空间中完成
对于(int i=0;i
我认为你把事情复杂化了。以下是我的想法:

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

嘿,我发现了问题,我的边界框函数给了我错误的答案。是的,我明白你的意思,事实上我创建了一个从左上角到右下角的简单边界框,删除了该函数,并简单地检查球是否与它碰撞。谢谢回答:)
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;
    }
}
  origin
    +---->  X, Width
    |
    |  upperLeftCorner
    |     +----------------+
    v     |                |
          |                |
    y,    |                |
  height  |                |
          |                |
          |        +       |
          | table.position |
          |                |
          |                |
          |                |
          |                |
          +----------------+
                       lowerRightCorner