C# 三维XNA碰撞

C# 三维XNA碰撞,c#,3d,xna,C#,3d,Xna,我一直在尝试在我的程序中进行碰撞检测。在我的程序中,立方体中有一个球。我希望球在立方体内反弹,如果我的立方体上有一个洞,我希望它掉出来 我现在是如何进行“碰撞”的: protected void Bounce() { if (ballPosition.X >= boxWidth || ballPosition.X <= -boxWidth) modelVelocity.X *= -1; if (ballPosition.

我一直在尝试在我的程序中进行碰撞检测。在我的程序中,立方体中有一个球。我希望球在立方体内反弹,如果我的立方体上有一个洞,我希望它掉出来

我现在是如何进行“碰撞”的:

protected void Bounce()
    {
        if (ballPosition.X >= boxWidth || ballPosition.X <= -boxWidth)
            modelVelocity.X *= -1;
        if (ballPosition.Y >= boxHeight*4.18 || ballPosition.Y <= -boxHeight)
            modelVelocity.Y *= -1;
        if (ballPosition.Z >= boxLength || ballPosition.Z <= -boxLength)
            modelVelocity.Z *= -1;
    }
protectedvoid Bounce()
{

如果(ballPosition.X>=boxWidth | | | ballPosition.X=boxHeight*4.18 | | | ballPosition.Y=boxLength | | ballPosition.Z要检测球体和长方体何时相交,可以分别使用边界球体和边界框。然后使用该方法和不同类型之一来检测球体和长方体边缘何时相交

举个例子:

void CollisionDetection()
{
    if (!boxBoundingBox.Contains(ballBouningSphere, ContainmentType.Contains)) 
    //if the box doesn't completely contain the ball they are overlapping
    {
        //Collision happened.
    }
}

然后,对于孔,您可能希望创建一个自定义对象
,如果球和框重叠,检查球是否在孔中,如果在孔中,则取消碰撞。这些只是一些想法。HTH

要检测球和框何时相交,可以分别使用边界球和边界盒。然后使用t他使用了一种不同的方法来检测球体和长方体边缘何时相交

举个例子:

void CollisionDetection()
{
    if (!boxBoundingBox.Contains(ballBouningSphere, ContainmentType.Contains)) 
    //if the box doesn't completely contain the ball they are overlapping
    {
        //Collision happened.
    }
}

然后,对于孔,您可能希望创建一个自定义对象
,如果球和框重叠,检查球是否在孔中,如果在孔中,则取消碰撞。这些只是一些想法。HTH

要检测球和框何时相交,可以分别使用边界球和边界盒。然后使用t他使用了一种不同的方法来检测球体和长方体边缘何时相交

举个例子:

void CollisionDetection()
{
    if (!boxBoundingBox.Contains(ballBouningSphere, ContainmentType.Contains)) 
    //if the box doesn't completely contain the ball they are overlapping
    {
        //Collision happened.
    }
}

然后,对于孔,您可能希望创建一个自定义对象
,如果球和框重叠,检查球是否在孔中,如果在孔中,则取消碰撞。这些只是一些想法。HTH

要检测球和框何时相交,可以分别使用边界球和边界盒。然后使用t他使用了一种不同的方法来检测球体和长方体边缘何时相交

举个例子:

void CollisionDetection()
{
    if (!boxBoundingBox.Contains(ballBouningSphere, ContainmentType.Contains)) 
    //if the box doesn't completely contain the ball they are overlapping
    {
        //Collision happened.
    }
}

然后,对于孔,您可能希望创建一个自定义对象
,如果球和长方体重叠,则检查球是否在孔中,如果在孔中,则取消碰撞。这些只是一些想法。HTH

使用轴逻辑:当X>targetX和XX轴碰撞确认时。 但这并不意味着存在碰撞:你必须对Y轴(以及3D的Z轴)做同样的操作 所以你得到:

public bool collision(Vector3 pos, Vector3 dimensions, Vector3 targetPos, 
Vector3 targetDimensions)
{
   return (pos.X + dimensions.X > targetPos.X &&
           pos.X < targetPos.X + targetDimensions.X &&
           pos.Y + dimensions.Y > targetPos.Y &&
           pos.Y < targetPos.Y + targetDimensions.Y &&
           pos.Z + dimensions.Z > targetPos.Z &&
           pos.Z < targetPos.Z + targetDimensions.Z);
}
公共布尔碰撞(Vector3位置、Vector3维度、Vector3目标位置、,
矢量3(目标尺寸)
{
返回(位置X+尺寸X>目标位置X&&
位置X<目标位置X+目标尺寸X&&
位置Y+尺寸Y>目标位置Y&&
位置Y<目标位置Y+目标尺寸Y&&
位置Z+尺寸Z>目标位置Z&&
位置Z<目标位置Z+目标尺寸Z);
}

使用轴的逻辑:当X>targetX和XX轴碰撞确认时。 但这并不意味着存在碰撞:你必须对Y轴(以及3D的Z轴)做同样的操作 所以你得到:

public bool collision(Vector3 pos, Vector3 dimensions, Vector3 targetPos, 
Vector3 targetDimensions)
{
   return (pos.X + dimensions.X > targetPos.X &&
           pos.X < targetPos.X + targetDimensions.X &&
           pos.Y + dimensions.Y > targetPos.Y &&
           pos.Y < targetPos.Y + targetDimensions.Y &&
           pos.Z + dimensions.Z > targetPos.Z &&
           pos.Z < targetPos.Z + targetDimensions.Z);
}
公共布尔碰撞(Vector3位置、Vector3维度、Vector3目标位置、,
矢量3(目标尺寸)
{
返回(位置X+尺寸X>目标位置X&&
位置X<目标位置X+目标尺寸X&&
位置Y+尺寸Y>目标位置Y&&
位置Y<目标位置Y+目标尺寸Y&&
位置Z+尺寸Z>目标位置Z&&
位置Z<目标位置Z+目标尺寸Z);
}

使用轴的逻辑:当X>targetX和XX轴碰撞确认时。 但这并不意味着存在碰撞:你必须对Y轴(以及3D的Z轴)做同样的操作 所以你得到:

public bool collision(Vector3 pos, Vector3 dimensions, Vector3 targetPos, 
Vector3 targetDimensions)
{
   return (pos.X + dimensions.X > targetPos.X &&
           pos.X < targetPos.X + targetDimensions.X &&
           pos.Y + dimensions.Y > targetPos.Y &&
           pos.Y < targetPos.Y + targetDimensions.Y &&
           pos.Z + dimensions.Z > targetPos.Z &&
           pos.Z < targetPos.Z + targetDimensions.Z);
}
公共布尔碰撞(Vector3位置、Vector3维度、Vector3目标位置、,
矢量3(目标尺寸)
{
返回(位置X+尺寸X>目标位置X&&
位置X<目标位置X+目标尺寸X&&
位置Y+尺寸Y>目标位置Y&&
位置Y<目标位置Y+目标尺寸Y&&
位置Z+尺寸Z>目标位置Z&&
位置Z<目标位置Z+目标尺寸Z);
}

使用轴的逻辑:当X>targetX和XX轴碰撞确认时。 但这并不意味着存在碰撞:你必须对Y轴(以及3D的Z轴)做同样的操作 所以你得到:

public bool collision(Vector3 pos, Vector3 dimensions, Vector3 targetPos, 
Vector3 targetDimensions)
{
   return (pos.X + dimensions.X > targetPos.X &&
           pos.X < targetPos.X + targetDimensions.X &&
           pos.Y + dimensions.Y > targetPos.Y &&
           pos.Y < targetPos.Y + targetDimensions.Y &&
           pos.Z + dimensions.Z > targetPos.Z &&
           pos.Z < targetPos.Z + targetDimensions.Z);
}
公共布尔碰撞(Vector3位置、Vector3维度、Vector3目标位置、,
矢量3(目标尺寸)
{
返回(位置X+尺寸X>目标位置X&&
位置X<目标位置X+目标尺寸X&&
位置Y+尺寸Y>目标位置Y&&
位置Y<目标位置Y+目标尺寸Y&&
位置Z+尺寸Z>目标位置Z&&
位置Z<目标位置Z+目标尺寸Z);
}

ballPosition.X和.Y引用的位置在哪里?如果不使用某种半径,这些值无法检测球体的所有边。上面提到的位置很有可能是球体的中心,而你看到的是剪切通过的边。你必须做数学运算才能找到球的边。ball在哪里位置.X和.Y引用?如果不使用某种半径,这些值无法检测球体的所有边。您上面提到的位置很可能实际上是球体的中心,您看到的是边剪切通过。您必须进行数学运算才能找到球的边。ballPosition.X和.Y在哪里引用?如果不使用某种半径,这些值无法检测球体的所有边。您上面提到的位置很可能实际上是球体的中心,并且您看到的是边剪切通过。您必须进行数学运算才能找到球的边。ballPosition.X和.Y引用在哪里?这些值不能绕道