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 / Help me with my game

Author
Message
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 27th Feb 2009 05:29
Hey im workin on makin a break-out game using DB. Ive kinda got the basic (haha) frame worked out, but i need a way to check which side of the blocks the ball hits. Ive got the positions of the blocks set into an array (bricklocations(10,2)) with the first of the two subsections being x and the 2nd being y. everything, ball, paddle, and bricks are sprites. the bricks are 50*20, any ideas on a collision detection system?
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 27th Feb 2009 15:48
you could text for collision with sprite collision then to find what side test for weather BallY>Bricky ballx>BrickX ballX<Brick X ext ext


so if bally>bricky and ballX>brickx then it hit the right side of the top of the brick see?
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 27th Feb 2009 16:15
nice idea caleb, but ive already tried that and it doesn't work
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 27th Feb 2009 21:29 Edited at: 27th Feb 2009 21:33
Here is how I would approach it:

We have a ball and we know its diameter (the width of the picture). We also know its center point.

We have a box and we know its dimensions.

There are only 4 points on the ball we need to be concerned about (8 really, but I will get into that later), that is, the top point, the bottom point, the left point, and the right point, since these are only points that will be colliding.

These points can be found simply by:

Where BallX and BallY are the coordinates for the top left pixel of the ball (should be the location that you place it by).

On the box, there are only 4 numbers: The Y coordinate of the top line, the y coordinate of the bottom line, the X coordinate of the left line, and the X coordinate of the right line.

When the ball collides with the brick, you check the 4 points of the ball against the 4 numbers from the brick. Here are the cases:

If Rightside>BallX+BallRadius>Left side
We know that the ball is either on top or on the bottom of the brick so then we check
IF BallY=Bottom Side

What this will tell us is if the first case is true, but the second is false, the ball must be colliding on top (since when it collides with the bottom, Bally will = the bottom side Y coordinate).

The next check will check to see what side is colliding and should only be done if the ball has NOT collided with the top or bottom (put it in the else case)

IF LeftSide>=BallX+BallDiameter we know that the ball is colliding on the left side. Why? Because this shows:

The * is the point of contact. IF the brick left side has a greater X value than the Balls X position+ its total width (to find its right side), then the ball is to the left of the brick.

The same reasoning can be used for the right side:
If RightSide<=BallX then the collision is on the right side


So now we know which side collides. The other 4 points on the ball I mentioned earlier are at 45 degrees from the center, to check for diagonal hits. I would suggest not even worrying about those, though and just have the ball continue through it.

NOTE: This entire thing assumes that the brick is wider than the ball but shorter. If the ball isn't taller , than that isn't a problem, but if the ball is wider than the brick, you will have to change the first check to look more like the second one.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 27th Feb 2009 21:49
nice one. that should work
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 27th Feb 2009 22:11
Tnx BN2, ill try it and get back 2 u
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 28th Feb 2009 17:27
hey guys tnx BN2, it sorta worked. now the collision detection system gets it right about 40-50% of the time. Heres my code, mind doing a little debugging?

BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 28th Feb 2009 20:28
Sure I will take a look at it.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 28th Feb 2009 21:55
Still working on it, but I had an idea that has been kinda nagging on my mind. Technically, there should be a way to find out an exact location for the collision because we know everything about both objects. I haven't completely finished it, but I did come up with an interesting, much more math related, way. So, here goes:

The locations where the ball can be to collide with it form an elliptical octagon. This is really close looking to an ellipse (which has a single equation that can be used to find any point on it) and since the ball won't be stopping, we can fudge the numbers a bit and use it instead of being completely precise.

After some experimentation, I found that the ellipse has these 2 radii (ellipses have an x radius and a y radius): X is 35 and Y is 20.

Here are some formulas for drawing an ellipse:

X=XRADIUS*cos(angle)+CX
Y=YRADIUS*sin(angle)+CY

Where you increase the angle. This ellipse is to show the location for the center of the circle, meaning that:

BallX=XR*cos(angle)+BrickX
BallY=YR*sin(angle)+BrickY

Note that BrickX and BrickY are the coordinates for the CENTER of the brick, not the corner, which is used to position it. We are trying to find the angle, so we can do this:

(BallX-BrickX)/35=cos(angle)
(BallY-BrickY)/20=sin(angle)

Using a bit of trig we see that:

acos(BallX/35)=angle
asin(BallY/20)=angle

If you don't know about arcsines and arccosines, don't worry, you will learn about them eventually, just go with it.

So, now we know the angle between the center of the brick and the center of the ball. With some quick checks, we can know how to handle the collision.

If the angle is between 45 and 135, the balls Y velocity should be set to positive (that is, it needs to start moving up)

If the angle is between 135 and 225, the ball's X velocity should be flipped, so that it is going left.

If it is between 225 and 315, balls Y velocity should be flipped, such that the ball goes down.

If it is less than 45 or greater than 315, flip the x direction so that it goes right. Note the wording this time. With the other directions, they simple increase, so you just need to know the range. This time, though, the angles restart, since you exceed 360 degrees. Therefore, you need 2 checks, one to see if the angle is less than 45 and the other (using an OR modifier in between) to see if it is greater than 315.

Does this make sense? I personally prefer these kinds of solutions because math is always faster and you do the same thing no matter what it is, until you check the results.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 1st Mar 2009 07:04 Edited at: 1st Mar 2009 17:41
nice BN2, but [REMOVED] we just got thru a brief intro 2 trig so i kinda understand the sine and cosine stuff. If u can adapt a code to work with mine i would be b=very thankful, tnx again 4 ur help

[Edit]
after further inspection and research, and the realization that arsine/arccosine is the same as sin-1 and cosine-1, I now understand do a sufficient degree your math. I prolly could have figured that out eventually, but again tnx, uve been a great help.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 1st Mar 2009 07:44 Edited at: 1st Mar 2009 15:02
hey BN2, after further inspection of ur comment, i realized that u did give me the code i need, so i put it into my game. It works better, although still not perfect, so once again, I am asking for you to debug. Tnx 4 all ur help



[Edit]
This is the new code for it, ill quit messing with it for a while to give u some time to work, again tnx a bunch!

Login to post a reply

Server time is: 2025-05-16 20:33:28
Your offset time is: 2025-05-16 20:33:28