Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Issue with collision  (Read 1389 times)

0 Members and 1 Guest are viewing this topic.

Blingu

  • Newbie
  • *
  • Posts: 15
    • View Profile
Issue with collision
« on: September 17, 2018, 07:02:13 pm »
Hi, I got a strange problem with my Arkanoid clone, whenever the ball hits a corner of the paddle it doesn't bounce off like expected:
https://media.giphy.com/media/vguZfLMc4eyiu3M7ls/giphy.gif

I already tried to fix it on several ways, one of them was:
/*I use the Collision class from Sonar Systems tutorial videos, although it happens with normal getGlobalBounds().intersects (and so on) collision check too.*/

else if (Collision::PixelPerfectTest(ball, paddle.getShape()))
{
   if (!ball.getGlobalBounds().contains(paddle.getShape().getPosition().x, paddle.getShape().getPosition().y))
        {
             y_ballSpeed = -y_ballSpeed;
        }
}

I wanted to check if the ball collides with the top left corner of the paddle (I could check every corner by adding some numbers above), if not it would normally bounce off, but it doesn't have any effect. The problem appears only if the ball hits a corner, except of that it works pretty well.
« Last Edit: September 17, 2018, 07:07:25 pm by Blingu »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Issue with collision
« Reply #1 on: September 19, 2018, 10:41:17 pm »
Remember that when it's colliding, this means that they're overlapping which is a state that you don't want when they are supposed to be impenetrable. Just swapping the direction of the ball doesn't change this state so it's still colliding. If it's still colliding in the next test, it will swap the direction again. This can continue over and over.

Probably simplest solution here is to (when it is colliding) move it upwards so that it is now touching the paddle instead of penetrating it. Technically, you want it to be just not touching but close enough that it's not noticeable. Then, it's no longer colliding so it will happily continue away from the paddle; of course you still need to flip the y direction for it to travel in the opposite direction ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tigre Pablito

  • Full Member
  • ***
  • Posts: 225
    • View Profile
    • Email
Re: Issue with collision
« Reply #2 on: September 21, 2018, 04:14:31 am »
Hi,

you gave this code,

else if (Collision::PixelPerfectTest(ball, paddle.getShape()))
{
   if (!ball.getGlobalBounds().contains(paddle.getShape().getPosition().x, paddle.getShape().getPosition().y))
        {
             y_ballSpeed = -y_ballSpeed;
        }
}

maybe if you add to the condition that the ball is going to the bottom of the screen, like this

   if (!ball.getGlobalBounds().contains(paddle.getShape().getPosition().x, paddle.getShape().getPosition().y)
&& y_ballSpeed > 0
)

to avoid the issue Hapax wrote about ... otherwise, when the ball hits the paddle on its very very edge, as the ball´s Y comes very low, the original condition would be true more than once, making the sentence:

y_ballSpeed = -y_ballSpeed;

execute a few times and getting a wrong behaviour of the ball

but if the condition requests the ball leading to the bottom, there won´t be a 2nd execution of that sentence because on the 2nd time the ball will be pointing to the screen top

Hope this helps

 

anything