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

Author
Message
Framester
11
Years of Service
User Offline
Joined: 8th Jan 2013
Location:
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.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
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)


Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 26th Feb 2013 18:48
When the ball hits the paddle, determine the angle of reflection:

(paddle.x is the center of the paddle in this case)
n# = (ball.x - paddle.x) / (paddle.length/2)

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)


Move your ball like so:
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

Login to post a reply

Server time is: 2024-04-19 15:52:04
Your offset time is: 2024-04-19 15:52:04