【问题标题】:How to improve/fix collision detection between ball and paddle?如何改进/修复球和桨之间的碰撞检测?
【发布时间】:2014-01-25 13:28:46
【问题描述】:

基本上,我正在制作类似于乒乓球、突围等的游戏。当球和桨发生碰撞时,我会遇到一些问题……但只是有时!

这个视频是正在发生的事情:http://www.youtube.com/watch?v=uFZIxFIg0rI

所以是的,基本上,当球与球拍相撞时,球有时似乎有点疯狂......通常情况下,如果我将球拍向与球接近的相反方向移动。此外,球有时会被卡在游戏窗口底部和球拍之间......即使我有代码可以阻止球、球拍等离开屏幕......

任何想法...或简单的修复?谢谢

哦,顺便说一句...这是我用于检测球和桨之间的碰撞并阻止它们离开游戏窗口的代码:

//CODE FOR CHECKING IF THE BALL IS OUTSIDE OF THE SCREEN

        //checks for collision with left wall
        if (pPosition.X <= 0)
        {
            pVelocity.X = -pVelocity.X;                
        }

        //checks for collision with right wall
        if (pPosition.X + pTexture.Width >= screenWidth)
        {
            pVelocity.X = -pVelocity.X;                                           
        }

        //checks for collision with top wall
        if (pPosition.Y <= 0)
        {
            pVelocity.Y = -pVelocity.Y;                
        }

        //checks for collision with bottom wall
        if (pPosition.Y + pTexture.Height >= ScreenHeight)
        {
            pVelocity.Y = -pVelocity.Y; //only need to invert Y velocity...                    
        }
    }


//CODE FOR CHECKING COLLISION BETWEEN BALL AND PADDLE

if (Ball.pRectangle.Intersects(Paddle.GetRectangle))
        {
            //Ball.pPosition.Y -= Ball.pVelocity.Y;
            Ball.pVelocity.Y = -Ball.pVelocity.Y;
        }            

        Ball.pPosition += Ball.pVelocity; //As this is in the update method, this just enables the ball to keep moving each frame...

编辑 17:21:

if (Ball.pRectangle.Intersects(Paddle.GetRectangle))
        {
            if (Ball.pRectangle.Bottom > Paddle.GetRectangle.Top)
            { //intersecting top of paddle
                //WORKING
                Ball.pPosition.Y = Paddle.GetRectangle.Top - Ball.pHeight;
                Ball.pVelocity.Y = -Ball.pVelocity.Y;
            }


 if (Ball.pRectangle.Right > Paddle.GetRectangle.Left && Ball.pRectangle.Right <     Paddle.GetRectangle.Right)
            { //intersecting left of paddle
                //WORKING
                Ball.pPosition.X = Paddle.GetRectangle.Left - Ball.pWidth;
                Ball.pVelocity.X = -Ball.pVelocity.X;
            }

 if (Ball.pRectangle.Left < Paddle.GetRectangle.Right && Ball.pRectangle.Left > Paddle.GetRectangle.Left)
            { //intersecting right of paddle
                //NOT WORKING
                Ball.pPosition.X = Paddle.GetRectangle.Right + Ball.pWidth;
                Ball.pVelocity.X = -Ball.pVelocity.X;
            }
        }

【问题讨论】:

    标签: c# xna collision-detection game-physics


    【解决方案1】:

    您的问题不是球被卡在游戏窗口底部和球拍之间,而是它卡在球拍内。

    当您检测到球拍从顶部击中时,通过将球的 Y 坐标设置在球拍上方来避免这种情况。

    如果您检测到球拍底部被击中,那么您必须将球的 Y 坐标设置在球拍下方。

    同样,如果你从侧面击球,你必须设置球的 X 坐标,使其位于球拍之外。

    关键是击球后面板和球不能互相接触,否则你会立即检测到第二次击球。

    【讨论】:

    • 好的,我会试试看,如果它有效,我会告诉你:)。感谢您的快速回复。
    • 当它从侧面与桨碰撞时,如果我将球的Y分量设置在桨上方,那会不会看起来有点不切实际?
    • 哦,好的,我会尝试的...我想我应该已经意识到,当你提到从桨的上方或下方击球时,它对于左侧或右侧的击球的工作方式相同它的一面。不过,这确实有道理,谢谢:)
    • @6a6179 如果解决了您的问题,请不要忘记标记为已解决
    • :D ty... 当球击中桨的顶部以及击中桨的左侧但当球击中桨的右侧时,它似乎正在工作似乎无法正常工作...我已经修改了上面的问题,以包括我为顶部,左侧和右侧编写的代码...谢谢:)
    猜你喜欢
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多