10 #include "math/vector.h"
11 #include "math/aatriangle.h"
12 #include "math/rectangle.h"
13 #include "collision_hit.h"
18 static const float DELTA = .0001;
21 Collision::rectangle_rectangle(CollisionHit& hit, const Rectangle& r1,
22 const Vector& movement, const Rectangle& r2)
24 if(r1.p2.x < r2.p1.x || r1.p1.x > r2.p2.x)
26 if(r1.p2.y < r2.p1.y || r1.p1.y > r2.p2.y)
29 if(movement.x > DELTA) {
30 hit.depth = r1.p2.x - r2.p1.x;
31 hit.time = hit.depth / movement.x;
34 } else if(movement.x < -DELTA) {
35 hit.depth = r2.p2.x - r1.p1.x;
36 hit.time = hit.depth / -movement.x;
40 if(movement.y > -DELTA && movement.y < DELTA) {
46 if(movement.y > DELTA) {
47 float ydepth = r1.p2.y - r2.p1.y;
48 float yt = ydepth / movement.y;
55 } else if(movement.y < -DELTA) {
56 float ydepth = r2.p2.y - r1.p1.y;
57 float yt = ydepth / -movement.y;
69 //---------------------------------------------------------------------------
71 static void makePlane(const Vector& p1, const Vector& p2, Vector& n, float& c)
73 n = Vector(p2.y-p1.y, p1.x-p2.x);
75 float nval = n.norm();
81 Collision::rectangle_aatriangle(CollisionHit& hit, const Rectangle& rect,
82 const Vector& movement, const AATriangle& triangle)
84 if(!rectangle_rectangle(hit, rect, movement, (const Rectangle&) triangle))
90 switch(triangle.dir) {
91 case AATriangle::SOUTHWEST:
92 p1 = Vector(rect.p1.x, rect.p2.y);
93 makePlane(triangle.p1, triangle.p2, normal, c);
95 case AATriangle::NORTHEAST:
96 p1 = Vector(rect.p2.x, rect.p1.y);
97 makePlane(triangle.p2, triangle.p1, normal, c);
99 case AATriangle::SOUTHEAST:
101 makePlane(Vector(triangle.p1.x, triangle.p2.y),
102 Vector(triangle.p2.x, triangle.p1.y), normal, c);
104 case AATriangle::NORTHWEST:
106 makePlane(Vector(triangle.p2.x, triangle.p1.y),
107 Vector(triangle.p1.x, triangle.p2.y), normal, c);
111 float depth = -(normal * p1) - c;
114 if(depth < hit.depth) {