Page 1 of 2

Breakout Help (bouncing ball)

Posted: Tue Feb 12, 2013 8:09 pm
by SeducingOrange
Hello everyone. I am trying to write a traditional breakout clone. I have setup the bricks and the paddle, but can not figure out how to implement the ball. The ball has to bounce off the paddle and hit the brick, when that happens the brick has to disappear. Since I am new to love, I would really like some help from experienced lovers. Thanks a lot!

-SeducingOrange (I even made a lewd name in spirit of LOVE :awesome:)

EDIT:
Oops. Meant to upload a love file :oops: excuse the sloppy coding.

Re: Breakout Help (bouncing ball)

Posted: Tue Feb 12, 2013 10:16 pm
by micha
It is easiest for us to help you, if you upload a .love file of what you got already.

Here are a few steps I suggest:
1) Implement the ball, without any collision on the blocks or paddle, only on the boundary of the playing field. By that you will learn how to use velocity.
2) Next you can implement the collision with the paddle. This is similar to the boundary, only with an extra if-condition. If you want to do it super-cool you will also make the angle of reflection depending on the position of the ball on the paddle. In many breakout games you can control the ball with this mechanism. But this is not important in the beginning.
3) Implement collision with the blocks. For that I suggest you look into some tutorials on collision detection (e.g. in platformer games).

Re: Breakout Help (bouncing ball)

Posted: Thu Feb 14, 2013 7:24 am
by micha
Alright. I had a look into your code.

I give you a hint, how to get started. Maybe you can work out the details on your own:
You initialize the ball with

Code: Select all

ball = {x = 50, y = 50, radius = 50, segments = 500, speed = level * 200, speed = math.random (360)}
Instead of having one "speed"-variable, you need two variables, namely for the to coordinate directions. Lets call them vx and vy (for velocity x and velocity y)
Also, for now, lets say that the initial speed is set in the beginning (I picked arbitrary numbers here). We'd get

Code: Select all

ball = {x = 50, y = 50, vx = 10, vy = 5, radius = 50}
Also for now, I leave away the segments property, as it is only a matter of drawing. It would only make sense if you have multiple balls and each one had a different number of segments.

Now in the update-function, you have to move the ball according to velocity

Code: Select all

ball.x = ball.x + ball.vx*dt
ball.y = ball.y + ball.vy*dt
And don't forget to draw the ball in the draw-function

Code: Select all

love.graphics.circle( "fill", ball.x, ball.y, ball.radius, 20)
Now you should get a ball moving across the playing field. However without any collision or interaction.

Try this next. What do you have to add, to make the ball bounce of the walls?


Here is an unrelated suggestion: Your code contains a lot of repitions. This is usually a sign that you can make the code shorter by programming in a more clever way. For example, you have a lot of rectangle-drawing function calls. Have a look into what tables are in lua and try to implement this drawing with a for loop.

Re: Breakout Help (bouncing ball)

Posted: Fri Feb 15, 2013 3:05 pm
by SeducingOrange
Thanks for the hint. I think I got it now! I really appreciate the help you gave me! I can finally finish this project. :awesome: :awesome: :awesome:

Re: Breakout Help (bouncing ball)

Posted: Mon Aug 05, 2013 4:29 pm
by SeducingOrange
I decided to come back to this after a long time of not working on it (I started a bunch of different projects and learned C++). I got collision with the paddle and walls working bot not with the bricks. Can you help me out with that? I attached a love file for ya.

Thanks for all the help by the way

Re: Breakout Help (bouncing ball)

Posted: Tue Aug 06, 2013 1:09 am
by davisdude
I hate to have to tell you this, but you need to make classes. In other words, rewrite most of your code. Then it's exactly the same as having a bunch of paddles that you can't control.

Re: Breakout Help (bouncing ball)

Posted: Tue Aug 06, 2013 1:23 am
by SeducingOrange
Oh I see. Do you have any good material for writing classes in lua? I thought it didn't support classes.

Re: Breakout Help (bouncing ball)

Posted: Tue Aug 06, 2013 2:20 am
by substitute541
Since you are a beginner, you can just use the class libraries out there. Here's one that I mainly use.
Note: Sexualpunsarethestandard here.

Edit: If you want to learn more, Programming in Lua has a chapter for simulating OOP.

Re: Breakout Help (bouncing ball)

Posted: Tue Aug 06, 2013 2:36 am
by SeducingOrange
Lol. I figured out the sexual puns when I downloaded the Live Coding Kit (LICK). That's not as bad as the cock one though. Thanks for references! And what's with the obey in everyone's avatar?

Re: Breakout Help (bouncing ball)

Posted: Tue Aug 06, 2013 3:42 am
by substitute541
SeducingOrange wrote:Lol. I figured out the sexual puns when I downloaded the Live Coding Kit (LICK). That's not as bad as the cock one though. Thanks for references! And what's with the obey in everyone's avatar?
It's also a standard here.