Post by Davy on Apr 2, 2006 20:07:47 GMT -5
Hi, this is a little bit of info some b4glers might find useful. A small explanation of some basic collision detection.
Ok, hopefully you know some basic algebra, you dont have to be a physics professor or anything, but it would help if you are OK at math.
Ok, we are basing this collision detection off of 4 vertices, so think of it in 2d (looking down from overhead at a box with 4 corners. We will be doing everything from lines, each side of the box is a line. The idea is... If we are between the bottom line and the top line, and we are between the left line and the right line, then we are obviously inside the box (yes, this collision detection is for the move that is pending... So we havent actually moved yet, we are just asking if the move WOULD be possible.)
Here is our box's corners, we will name them for reference.
a---b
| |
| |
c---d
We should already have the X and Y position of each corner (otherwise... why would we need to know if the are within an un designated box?). So we will refer to them like ax and ay or dx and dy.
Now, lets start with the top line a---b. We need to find the slope (if you dont remember slope from math class it's basically the angle of the line... In a sense, so you can see why we need to know this).
We are going to call the slope's by ab.slope or bd.slope and so on. Heres how we find the slope:
ab.slope = (ay-by)/(ax.bx)
Ok, you dont have to fully understand it to use it. Just bare with me... We also need to find the Y intercepts. We will call these similarly to our slope by ab.int or cd.int and so on. Here is how we find the Y intercept:
ab.int = (-ax * ab.slope) + ay
Still bare with me if this is confusing...
We use this formula because once you have the Y intercept and the slope you can use the slope-intercept formula... Which will give you the exact Y value for any given X value... So, basically, we can use that to tell if we are ON a line. We will use Px and Py to define the point that we are trying to find collision for. Here is the slope-intercept form:
Y = (X * Slope) + Yintercept
So... if you say
IF Py = (Px*Slope) + Yintercept THEN
(blah blah blah)
ENDIF
We can determine when our point is ON the line. However, the odds of our point being directly ON the line are very slim (what with EXACT decimals and everything) so we should find out if it is simply on one side of the line. And why do we need to know if we are on one side of the line? Because if we know that they are above the bottom line... AND below the top line... AND left of the right line... And lastly, Right of the left line... Then BAM... They are within our box. Make sense? So heres how we tell if they are on one side of the line:
IF Y>(X*Slope)+Yintercept THEN
(we are above the line!!!!)
ENDIF
So if you apply this to each side of the box, you can determine a pretty acurate collision. If you need any help or I explained this too complexly (sorry, im no teacher) you can email me at wybiral@aol.com
And I wish you lots of luck. btw, this collision detection will work with boxes that arent perfect, meaning if it is a 4 cornered polygon... We can use this collision detection. Have fun
-Davy
Ok, hopefully you know some basic algebra, you dont have to be a physics professor or anything, but it would help if you are OK at math.
Ok, we are basing this collision detection off of 4 vertices, so think of it in 2d (looking down from overhead at a box with 4 corners. We will be doing everything from lines, each side of the box is a line. The idea is... If we are between the bottom line and the top line, and we are between the left line and the right line, then we are obviously inside the box (yes, this collision detection is for the move that is pending... So we havent actually moved yet, we are just asking if the move WOULD be possible.)
Here is our box's corners, we will name them for reference.
a---b
| |
| |
c---d
We should already have the X and Y position of each corner (otherwise... why would we need to know if the are within an un designated box?). So we will refer to them like ax and ay or dx and dy.
Now, lets start with the top line a---b. We need to find the slope (if you dont remember slope from math class it's basically the angle of the line... In a sense, so you can see why we need to know this).
We are going to call the slope's by ab.slope or bd.slope and so on. Heres how we find the slope:
ab.slope = (ay-by)/(ax.bx)
Ok, you dont have to fully understand it to use it. Just bare with me... We also need to find the Y intercepts. We will call these similarly to our slope by ab.int or cd.int and so on. Here is how we find the Y intercept:
ab.int = (-ax * ab.slope) + ay
Still bare with me if this is confusing...
We use this formula because once you have the Y intercept and the slope you can use the slope-intercept formula... Which will give you the exact Y value for any given X value... So, basically, we can use that to tell if we are ON a line. We will use Px and Py to define the point that we are trying to find collision for. Here is the slope-intercept form:
Y = (X * Slope) + Yintercept
So... if you say
IF Py = (Px*Slope) + Yintercept THEN
(blah blah blah)
ENDIF
We can determine when our point is ON the line. However, the odds of our point being directly ON the line are very slim (what with EXACT decimals and everything) so we should find out if it is simply on one side of the line. And why do we need to know if we are on one side of the line? Because if we know that they are above the bottom line... AND below the top line... AND left of the right line... And lastly, Right of the left line... Then BAM... They are within our box. Make sense? So heres how we tell if they are on one side of the line:
IF Y>(X*Slope)+Yintercept THEN
(we are above the line!!!!)
ENDIF
So if you apply this to each side of the box, you can determine a pretty acurate collision. If you need any help or I explained this too complexly (sorry, im no teacher) you can email me at wybiral@aol.com
And I wish you lots of luck. btw, this collision detection will work with boxes that arent perfect, meaning if it is a 4 cornered polygon... We can use this collision detection. Have fun
-Davy