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.

Newcomers DBPro Corner / Break-Out Collision

Author
Message
The Great Poe
User Deleted
Posted: 7th Aug 2004 14:08
DBPro

Okay, I started on a stupid break-out game for practice, and I've encountered my very first problem.

How does one code collision so that the ball bounces off the walls without going in a totally wrong direction?
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 7th Aug 2004 19:45
The usual method is;

if collision(ball, sidewall) = 1
ballx = -1 * ballx : rem reverse ball side to side
endif
if collision(ball, topwall)=
bally = -1 * bally : remeverse ball top to bottom
endif

and so on. for the walls themselves you can use ball position to do the reversing and not use the collision routines, but when it comes to the paddle or the bricks you almost need the collision stuff.

The type of collision is either sprite collision or object collision, which for the latter you have to turn collision on.

Any truly great code should be indisguishable from magic.
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 9th Aug 2004 09:20
Also keep in mind that ballx and bally refered to above are the balls speed, not its position. You will no doubt have something along the lines of positionx=positionx+speedx in your loop, and the same for the y values.
The Great Poe
User Deleted
Posted: 10th Aug 2004 09:14
Well, I got it so it bounces off the walls, cause that doesn't have to involve collision, but how do I do it with collision? When I tried it, the ball would bounce in the exact opposite direction, instead of off the bricks in the right way.
The Great Poe
User Deleted
Posted: 10th Aug 2004 10:10
Ok. I had an idea as to how to do it, and then implemented it, but it's not working for some reason. Is this the right way to go about it?
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 10th Aug 2004 20:45
Could it be that the ball is in your x range and y range at the same time? If so, then both conditions are being satisfied and both x and y speeds are changing - hence it would bounce in the exact opposite direction. Just a thought.
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 11th Aug 2004 02:37 Edited at: 11th Aug 2004 02:41
Technically speaking, you should not be checking for both X & Y collisions on every brick you have, only that one or the other is taking place at the time. The direction that balls goes after this depends on which of two is making contact, but it's rare for both to take place at the same time. ie;

if collision_x() then collide = 1 : goto hit
if collision_y() then collide = 2 : goto hit
....
other code
....
hit:
if collide = 1 then reverse_ball_y()
if collide = 2 then reverse_ball_x()
...
etc.

It doesn't really matter which way you do the detection stuff in this instance but it is important to know if an up & down or left & right collision has been made. if either is true, 90% of the time you don't have to test the other. In the case of a complete reversal of the balls, like a corner, you let the detection run the above code on the two bricks making the corner space.

Any truly great code should be indisguishable from magic.
The Great Poe
User Deleted
Posted: 11th Aug 2004 05:03
Yeah, but how do you make it so it's testing for x and y collision independently?
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 11th Aug 2004 07:03
something like this should work:

do
ball movement bit
gosub collision x
if collision x = false then gosub collision y
loop

that way it will only check the y direction if it hasn't already changed x direction.

By the way, SandraD - did you notice I added a revised dbc code to the log slider.....I thought I'd have one last shot at it!
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Aug 2004 08:43
http://www.thegamecreators.com/?m=codebase_view&i=67ac560245c9b4822f6e288ba7137bd4

Take a look at the demo. It's not breakout, but the way the collision works is the same basically. It should give you an idea of how to know which side of the brick the ball has hit.

"eureka" - Archimedes
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 11th Aug 2004 12:28
Yeah Ric, I noticed. But Calculus had given me the formula which works out much better for the scheme I wanted, go I'm going with that.

And yes Poe, the detection is normally handled by position of the ball versus the brick you are testing, since you know where each is drawn. For myself, I like the "divided position" process which maps the bricks into an array and uses the ball's position as an index into the array to detect collision, testing first one way then the other. In my Assembly and Forth language versions of break-out I used the famous 8 pixel method, where I simply tested points around the ball to know which collision had taken place. The exact method isn't that important as just making the detection, testing one way and declaring match without testing the other unless the prior has not taken place.

Any truly great code should be indisguishable from magic.

Login to post a reply

Server time is: 2024-09-22 20:15:41
Your offset time is: 2024-09-22 20:15:41