1
\$\begingroup\$

I'm working on a Break Out game remake and I'm getting a weird bounding box collision detection behavior.

It seems like the ball is going under the paddle even if the hit is on top. Weird enough the ball doesn't go through the paddle but instead crawls within its height and then it bounces off.

My collision detection code is as follows:

bool collision = 
            std::abs( m_x - actor.m_x ) * 2 < ( m_texture->Width() + actor.m_texture->Width() ) &&
                std::abs( m_y - actor.m_y ) * 2 < ( m_texture->Height() + actor.m_texture->Height() );

This is the collision behavior I'm getting for some hits:

This is the collision behavior I'm getting for some hits.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Because your paddle has width and height it is likely that your ball manages to make it past the outside boundary of your paddle due to the update freqency and the speed of the ball. I would suggest instead of doing this type of collision detection that you instead predict where the ball will cross the boundary at its current tragectory and speed and determine if the paddle is going to be there or not.

Essentially, your first test should be wether the ball is going to go below the top Y of the paddle in the next update. If so then cast a ray from the ball to that line and determine the X location that it should hit. Then using the current tragectory of the paddle, determine if it will be there at that time or not. This is made simpler because your paddle(I assume) can only move horizontally? If not then it gets a bit more complex.

You will have to perform similar checks for hits on the sides of the paddle as well, just so you can accurately reflect it and so that it doesnt get trapped again.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .