View Full Version : Collision detection geo building?? help!

iNsaNEiVaN

06-19-2001, 08:31 AM

Ok ive read about every topic on collision detection there is but I havent been able to find code that helps. Ok, lets say I have a square placed on 0,0 that is 2 in both directions. (so -1,-1 : -1,1 : 1,1 : 1,-1) How would I write code to detect if a bouncing ball with a radius of 0.5 hit any one of the 4 sides? (yes I know how to do a^2 + b^2 = c^2)

This shouldn't be to hard to do since its 2d. But keep it general because the squares position is dynamic.

My idea is that I would find the slope and endpoints of all sides and then go from there.

thank you!

Deiussum

06-19-2001, 10:46 AM

I'm not sure if I completely understand your question, so forgive me if this answer seems a bit too simplistic.

Save the min and max of both dimensions. Then just test if it's inside the box with something like so...

posx = x position of ball

posy = y position of ball

r = radius of ball

minx = left bound of box

miny = bottom bound of box

maxx = right bound of box

maxy = top bound of box

if ( posx-r >= minx && posx+r <= maxx &&

posy-r >= miny && posy+r <= maxy)

// you're in the box

else

// you're not

So long as you don't need to rotate the box, this should work. (You don't really say if the boxes will rotate, just that they will move.)

[This message has been edited by Deiussum (edited 06-19-2001).]

iNsaNEiVaN

06-19-2001, 03:59 PM

ok, but lets say it is rotated now (lines not straight) how would I do it now?

thanks

ffish

06-19-2001, 06:43 PM

If you want to render this in OpenGL, the easiest way would be to draw it with vertical/horizontal sides and rotate it by some angle. Then you can use Deiussum's simple test. Otherwise, if it has to be prerotated, calculate the intersection of the sphere with each of the lines representing the sides. Here's a good explanation of the sphere-line intersection test: http://astronomy.swin.edu.au/pbourke/geometry/sphereline/

Hope that helps.

MrShoe

06-20-2001, 02:02 AM

If you have simple boundaries, it is very easy to detect collision in a primitive way:

if(ball_coords > boundary_coords){

collision response;

}

Im not sure if this helps but thats what I used in my pong game.

Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.