C# 球与矩形碰撞时会卡住

C# 球与矩形碰撞时会卡住,c#,xna,collision-detection,physics,bounding-box,C#,Xna,Collision Detection,Physics,Bounding Box,我现在遇到的一个新问题是,有时当球在屏幕上弹跳时,它可能会突然与矩形碰撞而卡住,因此它可能会击中矩形和屏幕侧面之间的一个点。这有时会导致以下两种情况:a)球看起来像是在摇晃或是什么,我假设它仍然与矩形碰撞,它的速度(正如我定义的)不断反转。。。但它仍然被卡住了。。。(这就是我无法解决的问题…哈哈:(),或b)当球与矩形碰撞时,它开始沿着矩形的边缘移动并来回移动,但它继续这样做。。。(所以它基本上是沿着边缘移动的) 我已经上传了一段视频到youtube上,展示了这一点(顺便说一句,球被卡住了,在屏

我现在遇到的一个新问题是,有时当球在屏幕上弹跳时,它可能会突然与矩形碰撞而卡住,因此它可能会击中矩形和屏幕侧面之间的一个点。这有时会导致以下两种情况:a)球看起来像是在摇晃或是什么,我假设它仍然与矩形碰撞,它的速度(正如我定义的)不断反转。。。但它仍然被卡住了。。。(这就是我无法解决的问题…哈哈:(),或b)当球与矩形碰撞时,它开始沿着矩形的边缘移动并来回移动,但它继续这样做。。。(所以它基本上是沿着边缘移动的)

我已经上传了一段视频到youtube上,展示了这一点(顺便说一句,球被卡住了,在屏幕的右侧上下移动,然后开始沿着白色矩形的上边缘移动,最后反弹到被遗忘的位置……基本上,球会自行释放,这是正常的,但如果再等5秒钟,它会重复这个过程)

我用于矩形碰撞检测的代码是:

if (theBall.GetRectangle.Bottom >= cornerSquare.GetRectangle.Top && theBall.GetRectangle.Bottom <= cornerSquare.GetRectangle.Bottom)
{
    theBall.pVelocity.Y = -theBall.pVelocity.Y;
}
我希望有人能找到一个简单有效的解决方案,因为我只想“完成”所有这些碰撞检测的废话,哈哈…因为它占用了我大部分的时间,而我可以更高效地完成游戏的其他部分


感谢阅读、花时间阅读、花时间帮助……等等……:D

如果您的球被卡在边缘,您只需将球的位置移到碰撞前的位置(前一帧中的位置),通过这种方式,你可以改变它的速度,确保球不再碰撞,这样就可以了。

你可以通过将球的速度减去球的位置来实现这一点。

如果你的球被卡在边缘上,你可以简单地将球的位置移到碰撞前的位置(前一帧中的位置),这样你就可以改变球的速度并确保球不再碰撞,这样就可以了。

你可以通过将球的速度减去球的位置来实现这一点。

如果你的球被卡在边缘上,你可以简单地将球的位置移到碰撞前的位置(前一帧中的位置),这样你就可以改变球的速度并确保球不再碰撞,这样就可以了。

你可以通过将球的速度减去球的位置来实现这一点。

如果你的球被卡在边缘上,你可以简单地将球的位置移到碰撞前的位置(前一帧中的位置),这样你就可以改变球的速度并确保球不再碰撞,这样就可以了。

您可以通过将球速度减去球位置来实现此目的。

首先尝试使用计算出的测试矩形检查碰撞

例如,每次更新球都会移动矢量2(1,1)。在移动前的下一步中,找出用于碰撞的矩形的位置

int nextPosX = currRect.X + (int)movementVector.X;
int nextPosY = currRect.Y + (int)movementVector.Y;

// find out where the rectangle would be after this update:
Rectangle nextStepsCollisionRect = new Rectangle(nextPosX, nextPosY, width, height);

// check if the test rectangle would collide:
if( collisionTest(nextStepsCollisionRect, sideWall) )
{
    // do not move the intended way!
    // invert X and recalc with pre-calculated rectangle again...
} else {
    // no collision. now move the caculated way:
    ball.CollisionRectangle = nextStepsCollisionRect;
}

这是一个非常简单的方法。如果球是一个非常快的子弹,请使用“光线跟踪”(仅需提及)。

首先尝试使用计算出的测试矩形检查碰撞

例如,每次更新球都会移动矢量2(1,1)。在移动前的下一步中,找出用于碰撞的矩形的位置

int nextPosX = currRect.X + (int)movementVector.X;
int nextPosY = currRect.Y + (int)movementVector.Y;

// find out where the rectangle would be after this update:
Rectangle nextStepsCollisionRect = new Rectangle(nextPosX, nextPosY, width, height);

// check if the test rectangle would collide:
if( collisionTest(nextStepsCollisionRect, sideWall) )
{
    // do not move the intended way!
    // invert X and recalc with pre-calculated rectangle again...
} else {
    // no collision. now move the caculated way:
    ball.CollisionRectangle = nextStepsCollisionRect;
}

这是一个非常简单的方法。如果球是一个非常快的子弹,请使用“光线跟踪”(仅需提及)。

首先尝试使用计算出的测试矩形检查碰撞

例如,每次更新球都会移动矢量2(1,1)。在移动前的下一步中,找出用于碰撞的矩形的位置

int nextPosX = currRect.X + (int)movementVector.X;
int nextPosY = currRect.Y + (int)movementVector.Y;

// find out where the rectangle would be after this update:
Rectangle nextStepsCollisionRect = new Rectangle(nextPosX, nextPosY, width, height);

// check if the test rectangle would collide:
if( collisionTest(nextStepsCollisionRect, sideWall) )
{
    // do not move the intended way!
    // invert X and recalc with pre-calculated rectangle again...
} else {
    // no collision. now move the caculated way:
    ball.CollisionRectangle = nextStepsCollisionRect;
}

这是一个非常简单的方法。如果球是一个非常快的子弹,请使用“光线跟踪”(仅需提及)。

首先尝试使用计算出的测试矩形检查碰撞

例如,每次更新球都会移动矢量2(1,1)。在移动前的下一步中,找出用于碰撞的矩形的位置

int nextPosX = currRect.X + (int)movementVector.X;
int nextPosY = currRect.Y + (int)movementVector.Y;

// find out where the rectangle would be after this update:
Rectangle nextStepsCollisionRect = new Rectangle(nextPosX, nextPosY, width, height);

// check if the test rectangle would collide:
if( collisionTest(nextStepsCollisionRect, sideWall) )
{
    // do not move the intended way!
    // invert X and recalc with pre-calculated rectangle again...
} else {
    // no collision. now move the caculated way:
    ball.CollisionRectangle = nextStepsCollisionRect;
}

这是一个非常简单的方法。如果球是一个非常快的子弹,请使用“光线跟踪”(只需提及).

谢谢pinckerman的回复。那么,我该如何确定球是否“卡住”,而不是刚刚与矩形碰撞?我的意思是,我可以检查球是否与矩形碰撞,但我是否要检查它是否再次与矩形碰撞,因为我假设球与矩形碰撞两次或更多,这就是为什么它会发生碰撞看起来它在摇晃,所以它的方向是相反的…?谢谢你第一次检测到碰撞,你的行为就像我解释的那样,这样你应该避免那种卡住的情况。你先减去速度,然后根据需要反转速度。好的,我会试试。谢谢你的回答。那么我该如何找到呢我的意思是,我可以检查球是否与矩形碰撞,但是我要检查它是否与矩形再次碰撞,因为我假设它与矩形碰撞了两次或两次以上,这就是为什么它看起来像在摇晃,所以它的方向是相反的…?谢谢当你第一次检测到碰撞时,你的行为就像我解释的那样,这样你应该避免那种卡住的情况。你先减去速度,然后根据需要反转速度。好的,我会试试。谢谢你的回答。那么我该如何去发现球是否“卡住”,而不是刚刚与球相撞矩形?我的意思是我可以检查它是否与矩形发生碰撞,但是我要检查它是否再次与矩形发生碰撞,因为我假设它与矩形发生了两次或两次以上的碰撞,这就是为什么它看起来像是在抖动,所以它的方向是反向的…?谢谢你第一次检测到碰撞时,你就这样做了