Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Oval Collision Detection
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
richcash




PostPosted: Sat Oct 14, 2006 11:21 pm   Post subject: Oval Collision Detection

So, I'm trying to write the algorithm for oval collision detection to put in a tutorial I'm working on. I haven't spent too much time on it because I'm working on a lot of things at once. Here's what I have :

code:
var x1, x2, y1, y2, xrad1, xrad2, yrad1, yrad2 : int
x1 := 100
y1 := 100
x2 := 200
y2 := 200       %manipulate
xrad1 := 40     %these values
yrad1 := 20
xrad2 := 20
yrad2 := 40

%***MAIN FUNCTION***
fcn ovalOval (x1, y1, x2, y2, xrad1, xrad2, yrad1, yrad2 : int) : boolean
    var m1, n1, m2, n2, angle1, angle2, d1, d2, d : real
    angle1 := arctand ((y2 - y1) / (x2 - x1 + 1e-3))    %find the angle between the two circles
    angle2 := arctand ((y1 - y2) / (x1 - x2 + 1e-3))
    m1 := xrad1 * sind (angle1) + x1
    m2 := xrad2 * sind (angle2) + x2
    n1 := yrad1 * cosd (angle1) + y1                    %m and n are points on the circumference of the
    n2 := yrad2 * cosd (angle2) + y2                    %oval at that angle
    d1 := sqrt ((m1 - x1) ** 2 + (n1 - y1) ** 2)
    d2 := sqrt ((m2 - x2) ** 2 + (n2 - y2) ** 2)        %find distances (d1 and d2 are kind of radii at
    d := sqrt ((x2 - x1) ** 2 + (y2 - y1) ** 2)         %an angle)
    result d <= d1 + d2
end ovalOval


loop
    drawfilloval (x1, y1, xrad1, yrad1, 9)
    drawfilloval (x2, y2, xrad2, yrad2, 12)
    View.Update
    exit when ovalOval (x1, y1, x2, y2, xrad1, xrad2, yrad1, yrad2)
    delay (5)
    cls
    x1 += 1
    y1 += 1
end loop


My problem is that it doesn't work. If you play around with the x and y radii values, the accuracy of the detection varies (sometimes being horrible!). I want to know if I have made an error in my calculations (or worst-case scenario, I'm completely on the wrong track, because I did derive it myself)? If the math is right, then does anyone have any ideas to make the function more accurate?



{Below is my reasoning and logic behind the function, you don't need to read it.
First, I found the angle between the two ovals' centers and the y-axis using tangent. Then, I wanted to find the point on the oval at that angle. So, I found the coordinates of the point at that angle for a unit circle using sine and cosine and then multiplied it by the x and y radius so it fits the oval. (I added the x and y of the center of the oval because my calculations were obviously for an oval at the origin.) Then I found the distance between that point on the oval and the centre of the oval, to find what I call an angular radius (probably not technical name). Then I used basic circle detection to check if the two "angular distances" of the ovals added up is greater than the distance between the center of the two ovals. If it is, then the ovals have collided.}
Sponsor
Sponsor
Sponsor
sponsor
TheOneTrueGod




PostPosted: Sun Oct 15, 2006 1:33 am   Post subject: (No subject)

x**2 + y**2 = r**2 is the circular collision detection, but do you know WHY this is? If not, perhaps you should reconsider writing this tutorial. Nonetheless, here it goes.

(x-dx/rx)**2 + (y-dy/ry)**2 = 1 I believe is the equation of an oval (Could be off based on the - dx, it could be a +dx, but whatever...).

dx = delta X (x offset)
dy = delta Y
rx = x Radius
ry = y Radius

Design your algorithm based around this, and you'll be on the right track.
richcash




PostPosted: Sun Oct 15, 2006 1:06 pm   Post subject: (No subject)

Quote:
x**2 + y**2 = r**2 is the circular collision detection, but do you know WHY this is

Yes, I know that. I just prefer working with a variable like d1, d2, etc. while I'm debugging it. I can easily change it.

Quote:
(x-dx/rx)**2 + (y-dy/ry)**2 = 1

Thanks, TOTG! Very Happy I think it's like this : (dx/rx)**2 + (dy/ry)**2 = 1
But I still need to find the angle to find a point on the first oval, don't I? Then I can check if that point is inside the second oval with your formula. My new function isn't much better :
code:
fcn ovalOval (x1, y1, x2, y2, xrad1, xrad2, yrad1, yrad2 : int) : boolean
    var m, n, angle1 : real
    angle1 := arctand ((y2 - y1) / (x2 - x1 + 1e-3))
    m := xrad1 * sind (angle1) + x1
    n := yrad1 * cosd (angle1) + y1
    result ((m - x2) / xrad2) ** 2 + ((n - y2) / yrad2) ** 2 <= 1
end ovalOval

Or is there a way to avoid finding the angle? Sorry, I don't have time to think too much, I'm working on something else aswell right now.
I appreciate any help a lot.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: