The triangle can be easily defined by three lines: horizontal, vertical, and diagonal:
- Line1: x = 0
- Line2: y = 0
- Line3: x = 1-y |/
- corner90: TOP_LEFT
or
- Line1: x = 1
- Line2: y = 0
- Line3: x = y \|
- corner90: TOP_RIGHT
or
- Line1: x = 0
- Line2: y = 1
- Line3: x = y |\
- corner90: BOT_LEFT
or
- Line1: x = 1
- Line2: y = 1
- Line3: x = 1-y /|
- corner90: BOT_RIGHT
These weird symbols are pictures of triangles, I just didn't bother to draw horizontal lines.
Now, You need first to treat the triangle as a square. If it collides, then You need to check the square against triangle's slope.
Let's define a function to check if a point is on 'good' side of the slope (outside the triangle):
function isOutSlope ( x, y, corner90 ):Boolean {
if ( corner90 == TOP_LEFT ) return x > 1-y // x = 1-y; // |/
if ( corner90 == TOP_RIGHT ) return x < y // x = y; // \|
if ( corner90 == BOT_LEFT ) return x > y // x = y; // |\
if ( corner90 == BOT_RIGHT ) return x < 1-y // x = 1-y; // /|
}
Now we need to check just one point of square - the one that is closest to corner90, when the two shapes almost collide, but not yet. If triangle.corner90 is TOP_LEFT, then it will be also top left corner of square , if corner90 is BOT_RIGHT, then it is bot right corner of square etc. If You don't understand my point here, then sketch a few situations, where square and square would collide, but square and rectangle either don't, almost or do collide; that should clear it up a little bit.
Let's make another function to make it more readable:
function getCornerLocalPos ( corner ):Point {
if ( corner == TOP_LEFT ) return new Point (0,0)
if ( corner == TOP_RIGHT ) return new Point (0,1)
if ( corner == BOT_LEFT ) return new Point (1,0)
if ( corner == BOT_RIGHT ) return new Point (1,1)
//else throw ERROR ("THIS IS A CIRCLE!!!")
}
So, let's make the main function
function detectSquareToTriangleCollision ( rect, triang ):Boolean {
if ( !detectSquareToSquareCollision (rect,triang) ) return False //if two squares wouldn't collide, then by no chance they won't if one turns to be a triangle.
//These deltas are used to translate one coordinate context to another
delta_x = rect.x - triang.x
delta_y = rect.y - triang.y
p = getCornerLocalPos ( triang.corner90 )
p.x += delta_x
p.y += delta_y
return isOutSlope ( p.x, p.y, triang.corner90 )
}
I didn't test it, but I sketched a little bit to make sure most is valid. I wrote the code for readability, but optimizing is straight-forward. I am not a native english speaker and hence I'm sorry if I accidentally insulted Your mother or did any other faux pas. I assumed same as Mr Anton - the triangles rotate in steps of 90 degrees, not in 0-90 degree range. If that would be the case, the thing would be a little bit more tricky, but the isOutSlope function code should be helpful.