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 / Angle In = Angle Out?

Author
Message
Yodaman Jer
User Banned
Posted: 1st Sep 2009 03:17
So the other day I started re-creating Breakout, but I've run into some problems with getting the ball to bounce back in different directions.

I'm also having immense trouble with speed...just to try it, I ran the code in DBPro and it ran almost lightning-fast...odd.

But anyway, I'm more concerned with getting the angle stuff worked out.

Here's my code:


This is the part I'm really focusing on right now:



I did not write that code, I copied it from an old thread (which might explain the speed issue). However, I understand most of it, I just don't know how to add angles to the ball bouncing back and forth.

Any help/advice is appreciated!

-Yodaman Jer

Hoping that, one day, I'll finally get around to producing a game for you all to play.
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 1st Sep 2009 04:02
ok, speaking for DarkNOOBS (who is currently making a breakout game) heres how I would handle ball movement and collision against flat surfaces:

make two variables : ballmovex# and ballmovey#

y is a flag (1 or -1) for up or down movement

x is a float for how much x movement per unit of movement (negative a to positive a, where a is the max x movement)

upon collision with the paddle or any bottom of ball collision you switch the y to positive (up) and keep the x the same

same goes for top of ball collision, only switch the y to negative

for left/right, keep the y the same and say x=x*-1, this will flip the x movement to its opposite, preserving the angles

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 1st Sep 2009 13:30 Edited at: 1st Sep 2009 13:43
Quote: "upon collision with the paddle or any bottom of ball collision you switch the y to positive (up) and keep the x the same"


But that's not how it works in breakout. The reflecting angle of the ball depends on where at on the paddle the ball hits, this gives the user more control over aiming the ball.



Imagine this black bar is your paddle. The illustration would show the user has the control to reflect the ball within an 80 degree range to either side. If the ball hits dead center, shoot it straight up (which is 270 degrees in DB). If the ball hits the paddle at the very edge, bounce off at 80 degrees from straight up, which is 190 or 350 degrees depending on which side it hits. All other places the ball hits on the paddle will fall between those reflective angles. You can setup a simple proportion to determine which angle the ball should travel after impact.

Assuming P.x is the center of the paddle's position and the paddle is a total of 100 pixels wide, that means the far left side is x-50 and the far right side is x+50.

B.x = ball x-position
delta = B.x - P.x

The absolute value of delta should be equal to or less than 50, otherwise it's outside of the paddle's ends. If delta is positive, the ball is on the ride side, negative its on the left side. If delta is 0, its dead center.

angle = (delta / 50.0) * 80

If delta is 50, on the edge of the paddle, then 50/50 equals 1.
1 * 80 = 80, the maximum degrees to reflect the ball from the center of the paddle. Add the angle to 270 degrees (this is in DB only, other languages don't use 270 to point straight up) and you'll get the reflective angle.

If delta is negative, adding it to 270 will result in an angle between 190 and 270, thus reflecting off to the left. If positive, reflects off to the right at an angle between 270 and 350.


This is actually part of a breakout tutorial I began writing awhile back. It's about 10 pages so far, but all that's left to cover is the collision with the bricks, which I'm experimenting with a faster (hopefully) technique.

[url="http://www.nocleanfeed.com"][/url]

Attachments

Login to view attachments
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 2nd Sep 2009 00:13
right, but I was just telling him how to et a basic angle in = angle out scenario, notthing to do with the real breakout collision

Yodaman Jer
User Banned
Posted: 2nd Sep 2009 05:05
Thanks for your responses, guys!

@Phaelax:

I think in order to test out your theory (which sounds great and I understand it for the most part) I need to re-write several lines of my code, or maybe even restart the project. I was going to do that anyway in order to solve the speed issue. I can't until I get more time though, which might not be until late next week...

Why is it that whenever I start a project I end up losing time?

-Yodaman Jer

Hoping that, one day, I'll finally get around to producing a game for you all to play.
Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 5th Sep 2009 20:12
I made a Breakout game and I used

Dim Map(X,Y)

For collision with the blocks. It is very fast. Not sure how fast Sparky's collision would be, but the built in collision is slow.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 10th Sep 2009 03:11
If I'm understanding correctly, you checked the ball to see which grid section it was in? That overlapping method wouldn't work with a very fast ball.

Pincho Paxton
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 10th Sep 2009 03:49 Edited at: 10th Sep 2009 03:54
My program checked to see if a ball passed over a map space at any point in time, so it was accurate. But also, the game was so fast that I was using 1.5 pixels, up to 3 pixels, which meant that for 3 pixel movement I just needed 3 checks, one for each pixel.

Monk
15
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 10th Sep 2009 17:59
I made a game of Pongout (pong and breakout so lots of rebounding) and just used pretty simple code to change the velocity of the ball.

Something like:



And heres my game code if you want to have a look at it.



Yodaman Jer
User Banned
Posted: 17th Sep 2009 21:40
That code looks great!

Sorry it took so long to get back here, I sort of abandoned the project because life got INSANELY busy for a few weeks. And it will continue that way until the second weekend of October...

So, when I have time I'll definitely take a look at your source code Monk. It looks really awesome!

-Yodaman Jer

Click, it's a funny video!

Login to post a reply

Server time is: 2024-05-09 15:33:06
Your offset time is: 2024-05-09 15:33:06