
-----------------------------------
d2bb
Sat Mar 03, 2007 8:21 pm

Help With Collision.
-----------------------------------
Alrighty well i got my circle vs circle collision down. so if ne 1 can help with :

Circle vs Line

Circle vs Box




This what i used for Circle + circle.

        
 dbc := sqrt ((xball - xplayer2) ** 2 + (yball - yplayer2) ** 2) % collision Detection. For player 1 and ball
            if dbc  1.0 then
            result Distance (px, py, x2, y2)
        else
            var ix : real := x1 + u * (x2 - x1)
            var iy : real := y1 + u * (y2 - y1)
            result Distance (px, py, ix, iy)
        end if
    end DistancePointLine

-----------------------------------
d2bb
Sat Mar 10, 2007 6:20 am

RE:Help With Collision.
-----------------------------------
yes i see what the script means, but not working to well :) ( as in errors lol )


qstions.
Functions allowed in loops ?


Or should be declared at start of program?

-----------------------------------
richcash
Sat Mar 10, 2007 12:40 pm

Re: Help With Collision.
-----------------------------------
Nope, functions are declared outside of the loop. Functions return a result, for example the sqrt () command you used earlier was a function. When you call the sqrt in the program it returns the square root of whatever you put in the brackets. Well, when you create your own functions it's pretty much the same, except in this case you're giving multiple parameters to the function DistancePointLine, and it will return the minimum distance between the point and line its given. (Btw, that function does work if you implement it correctly).

To check for the intersection of a line and a circle, check if the shortest distance between the center of the circle and the line is less than or equal to the circle's radius. Make sense?

For rectangle and circle intersection, check if any of the rectangle's 4 corners are inside of the circle; check if the furthest left point on the circle (x - r, y) or the furthest right point (x + r, y) or the highest point (x, y + r) or the lowest point (x, y - r) are inside of the rectangle. But this won't cover all of the scenarios of intersection, will it? What if the rectangle is on top of the circle with its four corners sticking out? Then we must also check if the center of the circle is inside of the rectangle.

I was supposed to write a tutorial on all of this, but I forgot all about it. I guess I'll get back to it now that it's March Break!

-----------------------------------
Martin
Sat Mar 10, 2007 1:56 pm

RE:Help With Collision.
-----------------------------------
Just a bit of advice for circle-circle collision: you don't need to take the square root at the end (and probably shouldn't). Computers are very good at some kinds of math, and very bad at others. Square roots tend to be things that computers are bad at.

So instead just do the (x-a)^2 + (y-b)^2 < r^2.

For example, say your ball's radius is 5, and you want to check if a point p = (p1, p2) is inside of the ball. The ball is located at (x, y). You'd write:
if( (x - p1)**2 + (y-p2)**2 