0
votes

I'm working on a brick breaker game in JavaFX (FXGL). The collision detecting is working like this: Bricks and players have a x, y position in the game world (placed in LEFT, TOP). When a collision is detected (this is done by the engine) I use the x, y position to detect where the ball is colliding with the brick to calculate the next direction. The ball direction is created by a velocity vector with (x, y) speed. The collisionLogicSecurityPadding is a small padding which is added since the collision is only detected inside the box. (It's not possible to get the ball and brick exactly at the same x, y coordinates)

if (ball.getX() > levelBrick.getX() - ball.getWidth() + collisionLogicSecurityPadding &&
    ball.getX() < levelBrick.getX() + levelBrick.getWidth() - collisionLogicSecurityPadding) {
    //velocity.getX(), -velocity.getY()
} else {
    //-velocity.getX(), velocity.getY()
}

So I measure if the ball is inside the brick with it x position. If this is the case the collision is TOP or BOTTOM. If not RIGHT or LEFT.

However this is causing the issue when the ball is hitting near the edge it detects wrong. Here a short clip: https://i.imgur.com/U8ybhRl.mp4

enter image description here The issue is that the x is smaller than the area where it thinks it's TOP or BOTTOM so it's switching the X direction which is causing an issue.

My question here is how to avoid this? Is there any way I can solve the collision that it's working correctly? When I'm looking online the most tutorials are only checking top or bottom collision.

2
Not sure im not understanding what your saying I know you can't check the exact bounds and stop it at the x and the y but why you have a collisionLogicSecurityPadding just check if its past the bounds of box and if it is count it as a collision and change direction/run whatever is necessary . Making a smaller box inside of the box you checking doesnt make sense to me unless you are trying to make it look like its overlapping for some bouncing effect? and it is clearly causing you more issues than its worth - Matt

2 Answers

1
votes

I think you could change the detection of TOP, BOTTOM, LEFT, RIGHT collisions to where the collision happened relative to the ball. Assuming the preservation of momentum you just need to negate the X- or Y-velocity.

In the example above the ball clearly generates a collision TOP and LEFT to his center of mass. Therefore it is impossible to generate a RIGHT collision which would generate the behavior you described. This leaves the Problem, that you do not know yet whether it was a collision on the LEFT or TOP. But since the Vector was pointing to the RIGHT (positive X-Movement), it is impossible to generate a collision on the LEFT. The collision must have happened at the TOP and therefore the Y-velocity needs to be multiplied by -1.

As long as the ball is the only moving object this method cannot generate weird behavior. As soon as you have an Object moving faster then the ball, you need additional checks: An Object hitting the ball on its left, while it is already in a general movement towards right will increase the X-velocity.

0
votes

As I understood your question it seems to me like your issue is that the red box goes in an incorrect direction after bouncing on an edge.

I think you need to detect collision in three instances.

  1. Top/Bottom
  2. Left/Right
  3. An Edge

If the collision occurs at an edge you need to send it in the same x direction as before with reducing the speed it goes in the direction y. y speed should always be less than initial speed and in the same direction as before.

I think above would solve your issue.