i am making a arkanoid like game and need help making the simple object detection between the ball and a block. i dont know how to determine which side of the block the ball hits.
Printable View
i am making a arkanoid like game and need help making the simple object detection between the ball and a block. i dont know how to determine which side of the block the ball hits.
ok, il make myself a little bit clearer:
i want to know how to determine what side of the block the ball hit like if the ball hit a side i would reflect the ball and mirror its x speed but the prob is is i dont know how to determine if it is in a certain side.
This is how I solved it in my ball game:
Split every block into 4 parts (see picture below). When checking if the ball overlaps the block, also check which of the four triangles it is in. If it's in the top one, it's most likely the ball came from above. If it's in the left one, it's most likely it came from the left. And so on...
Obviously, this has some flaws if the ball's speed is too great, but may be a place to start.
You could also combine it with checking the ball's speed. If it enters the top triangle and the Y speed is going upwards there is certainly something wrong (the ball went through the block) :).
Also, a theoretical solution I came up with is to "draw a line" from the balls last position to its current position. Then check which line of the block's four edges it overlaps. If none - no collision. If one - that's the side it collided with. If many - Check which edge was overlapped "first" (is closest to the last ball position).