Block Breaker - Solving the Deceleration Concern

Hello,

While building the game I couldn’t shake the constant viewing of deceleration when it came to longer play sessions. I noted that this particularly happened when the ball reflected back downwards or to the left.

I felt like doing some physics tweaking. I did some investigation on the Collider2D object that gets received when the collision method is called, and found that I could grab the transform position of the object the ball collided with. Calculating the ball’s position relative to the collision I could then accelerate appropriately knowing which way the ball should be moving next. Some notes below on a side affect.

I tagged my walls “SideWall” and top “TopWall” so that they could only accelerate in one direction (can do 0 acceleration with them as well). This was mainly done to prevent an endless loop of the ball bouncing towards the two top corners that could happen.

Here is my code (I used 0-0.5 to increase the speed faster, for testing mainly):
http://pastebin.com/mtmZqy97

May not be the most elegant solution, but wanted to share my work.

This doesn’t include some of the later parts or closing of the function if you are directly copying and pasting!

NOTE I also lowered the ball gravity to 0. This was to simulate to myself a more natural breaking bricks game like ones I have played before. Gravity is still good to work with, but I wanted the “always attempting to hit the top” that most games of this style I have played.

EDIT I did not mention but should that an uncapped speed is dangerous. You can have the ball clip out of bounds or through bricks because detection was not fast enough.

1 Like

Privacy & Terms