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)
30 if(movement.x > DELTA) {
31 hit.depth = r1.p2.x - r2.p1.x;
34 xr = hit.depth / movement.x;
35 } else if(movement.x < -DELTA) {
36 hit.depth = r2.p2.x - r1.p1.x;
39 xr = hit.depth / -movement.x;
42 if(movement.y > -DELTA && movement.y < DELTA) {
47 if(movement.y > DELTA) {
48 float ydepth = r1.p2.y - r2.p1.y;
49 float yr = ydepth / movement.y;
55 } else if(movement.y < -DELTA) {
56 float ydepth = r2.p2.y - r1.p1.y;
57 float yr = ydepth / -movement.y;
68 //---------------------------------------------------------------------------
70 static void makePlane(const Vector& p1, const Vector& p2, Vector& n, float& c)
72 n = Vector(p2.y-p1.y, p1.x-p2.x);
74 float nval = n.norm();
80 Collision::rectangle_aatriangle(CollisionHit& hit, const Rectangle& rect,
81 const Vector& movement, const AATriangle& triangle)
83 if(!rectangle_rectangle(hit, rect, movement, (const Rectangle&) triangle))
89 switch(triangle.dir) {
90 case AATriangle::SOUTHWEST:
91 p1 = Vector(rect.p1.x, rect.p2.y);
92 makePlane(triangle.p1, triangle.p2, normal, c);
94 case AATriangle::NORTHEAST:
95 p1 = Vector(rect.p2.x, rect.p1.y);
96 makePlane(triangle.p2, triangle.p1, normal, c);
98 case AATriangle::SOUTHEAST:
100 makePlane(Vector(triangle.p1.x, triangle.p2.y),
101 Vector(triangle.p2.x, triangle.p1.y), normal, c);
103 case AATriangle::NORTHWEST:
105 makePlane(Vector(triangle.p2.x, triangle.p1.y),
106 Vector(triangle.p1.x, triangle.p2.y), normal, c);
110 float depth = -(normal * p1) - c;
113 if(depth < hit.depth) {