- Home /
How do I properly handle a collision between a ball and a rectangle in 2D?
I am creating a brick breaker style game and I am having an issue with how my ball reacts when hitting the side of a brick. If I control the ball by reversing it's velocity upon collision, then the collision only looks natural when colliding with the top or bottom of the brick. When the ball hits the side of the brick it is immediately sent in the opposite direction and it looks very unnatural. I have tried using ray casting to identify which side of the brick has been hit and reversing the velocity accordingly. However, that solution wasn't consistent enough and when a full scene of bricks was introduced it failed miserably. What is the best way to handle a situation like this?
Answer by JuliuszK · Jan 13, 2017 at 10:36 AM
It's definitely possible to achieve it just by adding Rigidbody2D and Collider2D components - no need to code the collisions manually - that's what the physics engine is for. I'm shortly after doing this type of a game in one of the courses on Udemy. Here's what I did:
Brick - BoxCollider2D (Is Trigger OFF)
Ball - CircleCollider2D (Is Trigger OFF), Rigidbody2D (Kinematic OFF) with Physics Material (Friction 0, Bounciness 1)
If your ball passes through the bricks then maybe you have Is Trigger option ON - this is used only for detecting if something goes into the bounds of the collider. It needs to be OFF on both colliders for them to bounce off each other. You may also want to tweak the Gravity Scale either globally or for the ball only, so it does not fall down with the gravity.
Having said the above, you may notice unpredictable bounces when the ball hits the corner of the brick, but that's rare and may not be a problem to you.
Thank you very much for your help! I finally got it working properly. You were absolutely correct, there was no need for me have any code that made changes to the velocity of the object on collision. It worked exactly as I wanted after I removed the code that made changes to the balls velocity on collision. Relying solely on the physics material (Friction 0, Bounciness 1), rigidbody2D, and colliders is definitely the right move. I also started using addForce to the rigidbody2D ins$$anonymous$$d of assigning it a velocity to begin it's movements.
Thanks again for the help!
No prob. Just a small clarification - setting the velocity at the start is O$$anonymous$$ - just not at every collision.
"Having said the above, you may notice unpredictable bounces when the ball hits the corner of the brick, but that's rare and may not be a problem to you."
I'm having that exactly problem. I tried changing the Balls RigidyBody2D.Collision Detection to Continuous, and its interpolate property to interpolate (and to extrapolate), I also tried increasing Position Interactions by 1000 times and decreasing Linear Correction and Baumgarte Scale to the $$anonymous$$imum, but the problem persists (even with all the said changes combined).
I'm out of ideas and variables to toggle, any help would be appreciated.
Just to make sure - we're talking about a case, when you have let's say a row of bricks, the ball hits in between two bricks and it acts, as if it hit a corner of one of them, right?
Well, back in the days what I had tried was scaling up the collision boxes for the bricks in the middle of the row slightly, so they overlapped. This seemed to work a little bit better, but there were still some random incorrect bounces. Plus, after the adjacent bricks were gone, the remaining bricks were left with overscaled colliders, which wasn't good.
These days, I'd look into the CompositeCollider2D, which was introduced some time ago in Unity, which basically combines together a number of child colliders. This should create a single continues edge from a number of bricks and should solve these problems.
I haven't touched Unity in a while now and never used that feature, so can't tell exactly, but it looked promising.
It worked!
I had to set the individual Colliders to be slightly bigger than their individual shapes, but with that and the Edge Radius of the parent set to zero, they beautifully merged into a single, solid shape!
Thanks for the help!!!
Answer by millej23 · Jan 12, 2017 at 03:53 PM
Hello,
Have you tried using Rigidbody on the objects and the use of colliders? I havent done too much 2D but it sounds like with ball physics Rigidbody might be what you are looking for?
Here is the Unity 2D rigidbody tutorial:
Answer by nnatoli · Jan 13, 2017 at 08:35 AM
The ball has a Rigidbody2D and a circle collider and the brick sprite has a box collider. The problem is my solution for handling that collision doesn't look as natural as I want. I can detect the collision and delete brick, however, without any additional code the ball will just continue to move forward. Ideally, I would like the ball to bounce off in the proper direction. I have tried attaching physics materials with 0 friction and 1 bounciness but the ball still goes through the sprite after deleting it. I have also added code that reverses the y velocity upon collision with the brick, which works great unless it hits the side of a brick then it doesn't look right.
I guess what I am asking is if there is a way to detect whether you hit the horizontal or vertical sides of a rectangle without ray casting or is there a way to give the ball the ability to naturally bounce off of objects while maintaining the appropriate velocity by configuring rigidbody's and such? It might be important to note that I currently allowing the ball to move by assigning values to the rigidbody2D velocity. I'll look into using force instead and see if that makes any difference.
Thanks for the help btw!