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 Professional Discussion / Breakout physics?

Author
Message
Zeus
20
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 8th Apr 2011 04:37
I am creating a simple breakout game to practice bettering my knowledge in coding physics and I need to know how to make the ball bounce off of the paddle correctly. I have everything else down but that. No code is required - just a the basics of what to do would be nice. Below is my current code. Thank you!




Zeus

pictionaryjr
17
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 8th Apr 2011 08:53
you could keep an array and do a false ray cast. or you could do a pre collision check and get the area of the ball and check if its within the block and if so bounce it off the block. When i made my breakout game. i used the latter of the options and did a collision check every step so if i were moving at a speed of 6 i would check for a collision six times inbetween the balls current position and where it was going, because if i just checked where it would be, if it was going fast enough, it would go through the bricks. This way is obviously very slow and there's definately faster ways. thats just what i did my first time around.

Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 8th Apr 2011 10:29
Perhaps it's enough to invert the Y velocity and check the distance between the ball, and centre of bat.

Like ballx-batx

Then adjust the X velocity of the ball to suit that difference. That's what I did with a breakout game, but rather than using the ball v bat position, I used the momentum of the bat to control the balls X velocity. I think you should find the closest pre-collision height of the ball, so once you get a collision you can position the ball there, and not have to worry about the ball imbedding into the bat. I would just use math for collision, like if the ball is beyond the safe height, check the X position to see if it's hitting the bat, and then adjust the velocities, otherwise let it fly past and die.

Health, Ammo, and bacon and eggs!
pictionaryjr
17
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 8th Apr 2011 15:18
thats creative van. prevents have to check one side, but then again. what if the ball was above the brick like went behind it. it would wipe out all of the bricks below it.

Zeus
20
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 8th Apr 2011 16:28
Thank you both, I will see what I can do.

The Slayer
Forum Vice President
16
Years of Service
User Offline
Joined: 9th Nov 2009
Playing: (Hide and) Seek and Destroy on my guitar!
Posted: 8th Apr 2011 17:46
Quote: "Perhaps it's enough to invert the Y velocity and check the distance between the ball, and centre of bat."

Yeah, that's sorta how I did it in my Breakout clone BRIXOID.
I checked the ball x position relative to the bat x position, and increased the velocity or force depending on the amount of distance between the center of the ball and the center of the bat.

SLAYER RULES! YEAH, MAN!!
Zeus
20
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 9th Apr 2011 17:18 Edited at: 9th Apr 2011 20:36
See post below.

Zeus
20
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 9th Apr 2011 20:36
Okay so now I have slowed down the velocity, but it seems like the ball just bounces in the same place constantly and the paddle always bounces the ball in the same direction everytime. Is there anything I am doing wrong? Please correct me if I am.




Thanks!

Zeus

Zeus
20
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 10th Apr 2011 17:38
[bump]

Zeus
20
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 11th Apr 2011 04:29
No one?

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Apr 2011 05:40
When the ball hits the bat, subtract the center of the paddle's X-coordinate from the ball's X-coordinate. When the ball hits dead center, your result is 0 and should bounce straight up. If the sharpest angle the ball can reflect at is say 60 degrees:

t# = (ball.x - paddle.x) / (paddle.width/2)
ball.angle = 180 + 60*t#

Assuming 180 points the ball straight up.

The Internet: Where men are men, women are men, and children are federal agents
tasmanian guy
16
Years of Service
User Offline
Joined: 29th Jun 2010
Location: Australia
Posted: 11th Apr 2011 06:31
I recommend the book Dark Basic Pro:Game Programming Second Edition.

From scratch you learn to build an Arkanoid clone with very helpful code!

Book is worth its weight in gold.

DBPro is the new Amos (I hope)
Zeus
20
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 12th Apr 2011 03:11
I am afraid I cannot buy anything for the moment. So, this still doesn't work. What am I doing wrong?



Thanks!
Zeus

toliman
16
Years of Service
User Offline
Joined: 31st May 2010
Location: Manly, Australia
Posted: 15th Apr 2011 18:07
the problem is likely you're not adding velocity based on the 'angle' of hit. i did this using sprites, which is kinda "evil", but it works fairly well.

so, to do this yourself, it's time to use drunk math.

let's assume 1. the ball is smaller than the paddle.

2. the idea is that the surface is 'magic' and will bounce the ball left if hit left, right if hit right, and a sweet spot if hit dead center, which can either
3. reflect it using the original angle, or
4. dead upright.

quick way is to add or subtract to the horizontal speed to create the deflection you need, you can also add 'curvature', i.e. acceleration and deceleration to speed over time to simulate gravity,

be careful of acceleration and vectors you use, as it increases game difficulty and also complicates collisions as you can go through the paddle if you accelerate or make the velocity larger in value than the physical size of the ball.

remember to set a cap on the velocity or be ready tp reset them if you see the ball gets stuck during/after object collisions or goes off screen, past the bounds. checks are always fun, you never know how the game fails until it does.

let's say we use the center of the object at collision, so ball x and paddle x, it's 165 ball / 180 for the paddle (left / negative)
and 175 ball / 140 paddle (right / positive)

and the ball width is 10.

a) you get -15, which is above 1 ball lengths from the center, so a small deflect.
b) you get 25, which is 2 ball lengths from the center

if you simply divide that result , or use an integer function, you get a clean number to add to ballXspeed that's X/Y relative, i.e. int (-15/10) and int (25/10), -1 and 2

this way, you get a nice 'center' hit for values where the ball is between 170 and 190.

you can also add a challenge if you use width/2 which gives you a smaller sweet spot and a stronger deflection speed off center

i.e.



it's like 1am here, so check that with your own code.
Agent
21
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 16th Apr 2011 11:35
Didn't he say he had some type of problem with the ball embedding itself in the bat?

Quote: "but it seems like the ball just bounces in the same place constantly"


This can happen if you don't change the position of the ball as well as its velocity when it hits the paddle. Upon an impact with the paddle, the program changes the velocity of the ball so that it bounces back, but if you don't also change its position then another collision will occur immediately because the ball is still there, in collision with the paddle. This will make the ball embed itself into the paddle and stay there.

When you have a collision with the paddle, you need to change the velocity and also move it once right away, so that it's no longer in contact with the paddle. This will prevent the very next iteration of the main loop to cause another impact.

Login to post a reply

Server time is: 2026-07-18 14:26:55
Your offset time is: 2026-07-18 14:26:55