本文介绍了如何改进/修复球和桨之间的碰撞检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在制作像乒乓球,突围等游戏。当球和球拍相撞时,我会遇到一些问题……但有时!



以下是正在播放的视频:



是的,基本上,碰到球有时似乎有点发疯用球拍...通常,如果我以与球接近的相反方向移动球拍。有时球也会被卡在游戏窗口的底部和球拍之间...即使我有阻止球,球拍等进入屏幕之外的代码...



任何想法...还是简单的解决方法?谢谢



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

  //如果球不在屏幕上方则检查代码

//检查是否与左侧碰撞壁
if(pPosition.X {
pVelocity.X = -pVelocity.X;
}

//如果(pPosition.X + pTexture.Width> = screenWidth)
{
pVelocity,请检查与右墙
的碰撞。 X = -pVelocity.X;
}

//如果(pPosition.Y< = 0)
{
pVelocity.Y = -pVelocity检查与顶壁
的碰撞.Y;
}

//如果(pPosition.Y + pTexture.Height> = ScreenHeight)
{
pVelocity,检查与底壁
的碰撞。 Y = -pVelocity.Y; //只需要反转Y速度...
}
}


//检查球和桨之间碰撞的代码

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

Ball.pPosition + = Ball.pVelocity; //由于这是update方法中的内容,因此这只会使球保持每帧移动...

EDIT 在17:21:

  if(Ball.pRectangle.Intersects(Paddle.GetRectangle ))
{
if(Ball.pRectangle.Bottom> Paddle.GetRectangle.Top)
{//与桨叶
的顶部相交//工作
球。 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)
{//桨的左侧相交
//工作
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)
{/ /相对于板块
的右边//不使用
Ball.pPosition.X = Paddle.GetRectangle.Right + Ball.pWidth;
Ball.pVelocity.X = -Ball.pVelocity.X;
}
}


解决方案

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



通过设置



如果您检测到球拍底部有击球,则必须使球位于球拍上方。





同样,如果您从侧面击球,则必须设置球的X坐标,



重点是击球后,面板和球不得相互碰触,否则会立即检测到第二次击球。


Basically i am in the process of making a game which is like pong, breakout, etc. I am having some problems when the ball and paddle collide... but only sometimes!

This video here is what is occuring: http://www.youtube.com/watch?v=uFZIxFIg0rI

So yea, basically the ball sometimes seems to go a bit crazy when it collides with the paddle... normally if i am moving the paddle in the opposite direction of which the ball is approaching it. Also the ball sometimes get's caught between the bottom of the game window and the paddle... even though i have code to stop the ball, paddle, etc from going outside of the screen...

Any ideas... or simple fixes? Thanks

Oh btw... here is the code i am using for collision detection between the ball and paddle and to stop them from going outside of the game window:

//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...

EDIT at 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;
            }
        }
解决方案

Your problem is not that the ball gets caught between the bottom of the game window and the paddle, but it gets stuck within the paddle.

Avoid that by setting the Y coordinate of the ball to be above the paddle when you detect a hit with the paddle from the top.

If you detect a hit on the bottom of the paddle, then you have to set the Y coordinate of the ball to be below the paddle.

In the same way if you have hit from the side you have to set the X coordinate of the ball, so that it is outside of the paddle.

The point is that after the hit the panel and the ball must not touch each other, otherwise you detect a second hit immediately.

这篇关于如何改进/修复球和桨之间的碰撞检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:34