Collisions
Hiya guys. I''m working on a game for school with some basic 2d collisions. As of right now, I have a collision between a circle, and a square. The way I''m currently doing it is that I measure the radius of the sphere, and the length from the center to a perpendicular edge of the square. The only problem is that this in essence treats the square as a cube. How would I go about adding on the corners of the square ? I''m completely stumped.
/---\
| |
|----\___/
| |
| |
|______|
This is pretty much what happens. The circle covers the corner of the square before actually coming back as a hit. I''m not sure how I could fix this. Any ideas ? ? ? Thanx in advance.
The Kid
I don''''t know what the future holds, but I know who holds the future.
I don''t know what the future holds, but I know who holds the future.
Whoops, my picture didn''t really come out that well. I''m sure problem isn''t THAT difficult. I''m sure that there is someone out there that could help me. Anyone ?
I don''t know what the future holds, but I know who holds the future.
OK. So you have a circle at co-ordinates (x1, y1) with radius r and a square at co-ordinates (x2, y2) with sides of length L. This probably isn''t the quickest way to find the collisions, but it should work.
1. Find the vector from the square to the circle (I assume your co-ordinates are in the centre of the square and circle). vector (v) = (x1 - x2, y1 - y2)
2. Find the angle v makes with the horizontal. angle (a) = atan ((x1 - x2)/(y1 - y2)) *** watch for (y1 - y2) = 0 - this means the angle is 90º and will cause an error in atan!
3. Find the distance from the centre of the square to the edge at that angle. distance (d) = L/(cos a)
4. Add d to your radius to get the distance from the centre of the circle to the centre of the square (D).
5. Find the length of the vector in (1) - length (m) = root ((x1 - x2)² + (y1 - y2)²)
6. If m <= D then you have a collision
I hope the above does work - I don''t have time to test it right now - give it a try and if it doesn''t work post back here and I''ll try and figure out what I''ve done wrong.
1. Find the vector from the square to the circle (I assume your co-ordinates are in the centre of the square and circle). vector (v) = (x1 - x2, y1 - y2)
2. Find the angle v makes with the horizontal. angle (a) = atan ((x1 - x2)/(y1 - y2)) *** watch for (y1 - y2) = 0 - this means the angle is 90º and will cause an error in atan!
3. Find the distance from the centre of the square to the edge at that angle. distance (d) = L/(cos a)
4. Add d to your radius to get the distance from the centre of the circle to the centre of the square (D).
5. Find the length of the vector in (1) - length (m) = root ((x1 - x2)² + (y1 - y2)²)
6. If m <= D then you have a collision
I hope the above does work - I don''t have time to test it right now - give it a try and if it doesn''t work post back here and I''ll try and figure out what I''ve done wrong.
August 30, 2001 02:05 PM
to keep it from testing for a collision even when the square is far away from the circle do this:
w is the width of the square
r is the radius of the circle
d is the distance between them (distance formula with the center points)
if((sqrt(0.5*w*w)+r)<=d)
a collision is possible
w is the width of the square
r is the radius of the circle
d is the distance between them (distance formula with the center points)
if((sqrt(0.5*w*w)+r)<=d)
a collision is possible
I think you meant: if (sqrt(2 * w * w) + r <= d)
Quicker to do: if ((1.42 * w) + r <= d)
1.42 * w is approximately the square root of 2 * w * w, and means the computer doesn''t have to calculate a square root in real time.
Quicker to do: if ((1.42 * w) + r <= d)
1.42 * w is approximately the square root of 2 * w * w, and means the computer doesn''t have to calculate a square root in real time.
Correction to my first post.
Step 3 - Find the distance from the centre of the square to the edge at that angle. distance (d) = L/(cos a)
This only works for 45º <= a <= 90º. Better to use d = L/(sin a) and force 0º <= a <= 45º by:
1) making the angle calculation in step 2 a = atan((|x1 - x2|)/(|y1 - y2|)). |x| means positive value of x, i.e. |1| == |-1| == 1
2) doing: if (a > 45){a = 90 - a} (in degrees - probably need to do a = pi/2 - a for radians)
I'm still haven't tested this, so it may be wrong!
Edited by - Enigma on August 30, 2001 6:34:19 PM
Step 3 - Find the distance from the centre of the square to the edge at that angle. distance (d) = L/(cos a)
This only works for 45º <= a <= 90º. Better to use d = L/(sin a) and force 0º <= a <= 45º by:
1) making the angle calculation in step 2 a = atan((|x1 - x2|)/(|y1 - y2|)). |x| means positive value of x, i.e. |1| == |-1| == 1
2) doing: if (a > 45){a = 90 - a} (in degrees - probably need to do a = pi/2 - a for radians)
I'm still haven't tested this, so it may be wrong!
Edited by - Enigma on August 30, 2001 6:34:19 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement