GameSalad

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?

mhedgesmhedges Raised on VCSPosts: 634Member
edited May 2015 in Miscellaneous

Hello -

I don't know if you're like me, who often takes a break from coding a game by starting another :D . 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 B) . 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.

Comments

Sign In or Register to comment.