Help With Collision.
Author |
Message |
d2bb
|
Posted: Sat Mar 03, 2007 8:21 pm Post subject: 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 <= 14 then % statement that allows player to move the soccer ball. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
CodeMonkey2000
|
Posted: Sat Mar 03, 2007 9:11 pm Post subject: RE:Help With Collision. |
|
|
For the circle to circle collision you figured out the distance between the two mid-points and checked if it is less than the sum of the two radiis. For a Line to circle collision all you need to do is figure out the SHORTest distance between a point and a line. If the distance is less than or equal to the radius than BOOM! they collide! Once you know how to do that you can use it to determin if a circle and a box collide (ie check where your ball is with all sides of the box).
For the shortest distance between a point and a line use this function I found laying around in the turing tutorials:
Turing: |
%
% Calculate the distance between two points
function Distance (x1, y1, x2, y2 : real) : real
var dx : real := x1 - x2
var dy : real := y1 - y2
result sqrt (dx * dx + dy * dy )
end Distance
%
% Calculate the distance between a point and a line segment
%
function DistancePointLine (px, py, x1, y1, x2, y2 : real) : real
var lineSize : real := Distance (x1, y1, x2, y2 )
if lineSize = 0 then
result Distance (px, py, x1, y1 )
end if
var u : real := ((px - x1 ) * (x2 - x1 ) +
(py - y1 ) * (y2 - y1 )) / (lineSize * lineSize )
if u < 0. 0 then
result Distance (px, py, x1, y1 )
elsif u > 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
|
Posted: Sat Mar 10, 2007 6:20 am Post subject: 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
|
Posted: Sat Mar 10, 2007 12:40 pm Post subject: 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
|
Posted: Sat Mar 10, 2007 1:56 pm Post subject: 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:
Turing: | if( (x - p1)**2 + (y-p2)**2 <= 5**2 ) then
put "p is inside the ball"
else
put "No dice."
end if |
|
|
|
|
|
|
d2bb
|
Posted: Sat Mar 10, 2007 4:16 pm Post subject: RE:Help With Collision. |
|
|
Wowy. Thx just needed to clear that up cuz i had my functions inside a loop and got my error. but i think i can work around that now Thx !
- If i have any questions can i pm u : richcash ? |
|
|
|
|
|
richcash
|
Posted: Sat Mar 10, 2007 5:20 pm Post subject: Re: Help With Collision. |
|
|
d2bb wrote: - If i have any questions can i pm u : richcash ?
Well, I assume you mean any questions with this topic. You could PM me if you really wanted to, but I would strongly advise you to just post in this thread for a number of reasons.
-First, I might not be online, someone else can help you sooner;
-Second, there are people who may have better solutions to problems than I do;
-Third, if someone has the same question as you one day they can search and look in this thread and get all their answers instantly, rather than making another topic (that's why compsci keeps all their topics from years ago, so people can learn from them). |
|
|
|
|
|
|
|