So I spent valentines day with a bottle of scotch and a book on vector algebra
It turns out my love letter to matrix multiplication got lost in the post. Being the fickle mistress she is, she upped left me for a career in projection. In my drunken stupor I smashed everything we'd made together, everything that reminded me of her.
"
Fuck your rotations! I'm going back to AABB!!", I said as I ripped all traces of orientation from both my mind and the depths of EnemyManager.cs. Intellisense screaming in agony, throwing up red pools of unreturned code paths and compiler warnings.
I blazed on through the night, cursing the moment she waltzed into my design. With sugar sweet promises of elegant collision detection and efficiency she blew my world. "
Sweep and prune" she said.
Nothing mattered, now that she'd left me what use was a two-bit data structure, lingering on in the back of my method, declared but not assigned...
Waking up blearily the next day, my fingers trembled toward F5 to see what damage had been done. My mind jarred, thinking about all the times we'd spent debugging on the coffee table. Dubious substances cast aside as we dropped breakpoints together into the small hours of morning, she was my everything.
Turns out it worked fine
I used Ishi's method. Guess rotations are out of my league for now :D
Here's my implementation in c#/xna
public enum LineClockDir
{
Clockwise,
CClockwise,
Line
}
LineClockDir getClockPosition(Vector2 pt1, Vector2 pt2, Vector2 pt3)
{
float tester = ( ((pt2.X - pt1.X) * (pt3.Y - pt1.Y)) - ((pt3.X - pt1.X) * (pt2.Y - pt1.Y)) );
if (tester > 0)
return LineClockDir.CClockwise;
else if (tester < 0)
return LineClockDir.Clockwise;
else
return LineClockDir.Line;
}
bool lineLineCollision(Vector2 l1p1, Vector2 l1p2, Vector2 l2p1, Vector2 l2p2)
{
LineClockDir test1a, test1b, test2a, test2b;
test1a = getClockPosition(l1p1, l1p2, l2p1);
test1b = getClockPosition(l1p1, l1p2, l2p2);
if (test1a != test1b)
{
test2a = getClockPosition(l2p1, l2p2, l1p1);
test2b = getClockPosition(l2p1, l2p2, l1p2);
if (test2a != test2b)
{
return true;
}
}
return false;
}
bool lineRectangleCollision(Vector2 linepoint1, Vector2 linepoint2,
Rectangle hitBox)
{
#region get rectangle verts
List<Vector2> recVerts = new List<Vector2>(4);
recVerts.Add(new Vector2(hitBox.X, hitBox.Y));
recVerts.Add(new Vector2(hitBox.X, hitBox.Y + hitBox.Height));
recVerts.Add(new Vector2(hitBox.X + hitBox.Width, hitBox.Y + hitBox.Height));
recVerts.Add(new Vector2(hitBox.X + hitBox.Width, hitBox.Y));
#endregion
if (
//testing left side of rectangle
lineLineCollision(linepoint1, linepoint2, recVerts[0], recVerts[1])
||
//testing top side
lineLineCollision(linepoint1, linepoint2, recVerts[0], recVerts[3])
||
//testing right side
lineLineCollision(linepoint1, linepoint2, recVerts[3], recVerts[2])
||
//testing bottom
lineLineCollision(linepoint1, linepoint2, recVerts[1], recVerts[2]))
{
return true;
}
else
return false;
}
It's probably horribly inefficient, but it's not like I'm losing frames all over the place.