简体   繁体   English

乒乓球游戏中的碰撞检测导致问题

[英]Collision detection in game of pong corner cases causing issues

I have made a game of pong and everything about it is working so far.我做了一个乒乓球游戏,到目前为止一切都在工作。 I did the collisions against the paddles to the ball and it can correctly bounce them away unless it hits the corners.我对球拍进行了碰撞,除非撞到角落,否则它可以正确地将它们弹开。

This is a video of what happens when the ball hits the corner of one of the paddles.这是当球击中其中一个桨的角落时会发生什么的视频。

https://drive.google.com/file/d/1nyRzsp5tn5Qvst7kVjtlr_rDNH98avbA/view?usp=sharing https://drive.google.com/file/d/1nyRzsp5tn5Qvst7kVjtlr_rDNH98avbA/view?usp=sharing

Essentially what you can see happen in the video is the ball hits the the paddle on the left at the corner and it doesn't bounce back it just traverses the paddle downwards and then keeps going and they get a point.从本质上讲,您可以在视频中看到发生的事情是,球在拐角处撞到左侧的球拍,它没有反弹,只是向下穿过球拍,然后继续前进,他们得到一分。 Here is my code for the collision testing这是我的碰撞测试代码

public void collision() {
    this.width = (float) getBounds().width;
    this.height = (float) getBounds().height;
    for (int i = 0; i < handler.objects.size(); i++) {
        if (handler.objects.get(i).getId() == ID.ball) {
            GameObject current = handler.objects.get(i);
            if (getBounds().intersects(current.getBounds())) {
                current.setVx(current.getVx() * -1);
            }

        }
    }
}

getBounds is an overridden method that just returns a rectangle for both the ball(which is a circle) and the paddle getBounds 是一个被覆盖的方法,它只为球(一个圆形)和桨返回一个矩形

I am happy to provide more of my code if necessary.如有必要,我很乐意提供更多我的代码。

Any help on this would be great, thanks!对此的任何帮助都会很棒,谢谢!

You are swapping the velocity direction each time you are hitting the paddle, even when you are still touching the paddle in the next frame.每次击中桨时,您都在交换速度方向,即使在下一帧中您仍在触摸桨时也是如此。 The solution is to swap the velocity direction only when the ball is heading towards the paddle, not when (already) going away.解决方案是在球朝向桨叶时交换速度方向,而不是在(已经)离开时交换速度方向。 The pseudo code can look like this:伪代码可能如下所示:

if (GeometryUtil.isDirectionGoingDown(direction)) {
    if (GeometryUtil.isBallHittingHorizontalLine(
            newPosition,
            Ball.RADIUS,
            paddleTopLeft,
            paddleTopRight)) {
        direction = calculateNewDirectionAfterHittingPaddle(
                        direction,
                        paddlePosition,
                        newPosition);
        ball.setDirection(direction);
    }
}

Here the GeometryUtil.isDirectionGoingDown() method is checking if the ball is actually going down.这里的GeometryUtil.isDirectionGoingDown()方法正在检查球是否真的向下。 That way the new direction is only set when this condition returned true .这样,仅当此条件返回true时才设置新方向。 When the direction has been swapped in one frame, the next frame will not swap the direction again because GeometryUtil.isDirectionGoingDown() will return false then.当在一帧中交换了方向时,下一帧将不会再次交换方向,因为GeometryUtil.isDirectionGoingDown()将返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM