Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Breakout game

Message
Posted: 26th Feb 2013 17:39
I've coded most of my breakout game right now, but currently have a issue with the angles. Right now i'm just using 8 directions

I'm using a direction + 1 to alter when it hits the walls and brick. Im thinking of having angles so when the ball hits 1 half of the paddle, it will go one way, or vice versa. Any ideas where I could start with this please.
Posted: 26th Feb 2013 18:25 Edited at: 26th Feb 2013 18:28
Look into using sine and cosine:

This is a bodged example that will not work for all angles. Read this for useful maths: http://en.wikipedia.org/wiki/Reflection_(physics)

Posted: 26th Feb 2013 18:48
When the ball hits the paddle, determine the angle of reflection:

That will return a float from -1 to +1.
If 180 degrees points straight up from the paddle:

angle = 180 + 65*n#

This will give us an angle from 115 to 245. Now store this direction as a vector

ball.vx = sin(angle)
ball.vy = cos(angle)

ball.x = ball.x + ball.vx*ball.speed
ball.y = ball.y + ball.vy*ball.speed

Then, if you hit a side wall or the side of a brick, invert the X direction:

if ball hits side of something then ball.vx = -ball.vx

And do the same thing for ball.vy if you hit the top or bottom of something.

"You're all wrong. You're all idiots." ~Fluffy Rabbit