#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Breakout / Brick Breaker - an elegant coding solution to ball angles?

Raised on VCSPosts: 634Member
edited May 2015

Hello -

I don't know if you're like me, who often takes a break from coding a game by starting another . I stumbled upon the AtariAge website (letting my age show again), and after reading for a while, started wondering about Breakout, the paddle game, and how much coding the guys had to cram into 4K (the original 2600 cartridges). Hats off to those resourceful gentlemen, who were both creative and practical at once.

I then set out to get the Brick Breaker template created way back when for the legacy iPhone (read: 4), and took a look under the hood.

If the ball collides with the paddle, the it changes the attribute as follows:
self.Motion.Linear VelocityX = self.Motion.Linear Velocity.X +2*( self.Position.X - game.Mouse.Position.X )

The coding above allows for the possibility of ball travel at an angle of zero; I could theoretically hit the 0 "sweet spot" and go to the movies or wash the car, come back, and find the ball going straight up and down per secula seculorum.

I thought about it a bit, and a solution could be to segment the paddle into x amount of segments, say 6, and code according to positive or negative linear velocity x and ball x minus paddle x obviating the 0 sweet spot. If you segment the paddle to six slices, then you would get a series of angles to bounce the ball based on segment and linear velocity x (ball direction).

My solution is currently grotesque, with a rule grid instead of one formula to handle it all.

I was fiddling for a while with pen and paper, but everyone's awake here at my home, so it's a task best left for later.

That said, I read somewhere that coding must be simple and elegant . That translates to doing the most with the least - think of our heroes with only 4K to spare.

Does anyone have a suggestion on how to keep the code as short as possible?

Thanks, regards.

• Posts: 2,343Member

I would use a counter when...ball's X position is equal at paddles position and top of scenes position for more than say..10 seconds...to change the angle by 15* or something like that...never done this but that's what I'd be trying to do for a fix...

• Posts: 2,782Member, PRO

@mhedges , I would actually allow the user the freedom "try" and reach that sweet spot.

Your calculation however leads to the game accidentally reaching the 0 deg infinite bounce angle, and not giving the player a way to escape it since you only take the angle of impact into account.

My suggestion is to add the speed of the paddle to the exit angle (or X velocity), thereby the player has the feeling of more control.

• Raised on VCS Posts: 634Member

Gents,

As much as I'd hate to admit it, you're right. I factored in a multiple to leverage (multiply) the effect of the distance between ball center and paddle center. That still leaves the freedom to control the ball at will, but then I added other rules so you can't do it per secular seculorum. Thanks much!