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) {
50 if(movement.y > DELTA) {
51 float ydepth = r1.p2.y - r2.p1.y;
52 float yt = ydepth / movement.y;
59 } else if(movement.y < -DELTA) {
60 float ydepth = r2.p2.y - r1.p1.y;
61 float yt = ydepth / -movement.y;
73 //---------------------------------------------------------------------------
75 static void makePlane(const Vector& p1, const Vector& p2, Vector& n, float& c)
77 n = Vector(p2.y-p1.y, p1.x-p2.x);
79 float nval = n.norm();
85 Collision::rectangle_aatriangle(CollisionHit& hit, const Rectangle& rect,
86 const Vector& movement, const AATriangle& triangle)
88 if(!rectangle_rectangle(hit, rect, movement, (const Rectangle&) triangle))
95 switch(triangle.dir & AATriangle::DEFORM_MASK) {
100 case AATriangle::DEFORM1:
101 tp1 = Vector(triangle.p1.x, triangle.p1.y + triangle.get_height()/2);
104 case AATriangle::DEFORM2:
106 tp2 = Vector(triangle.p2.x, triangle.p1.y + triangle.get_height()/2);
108 case AATriangle::DEFORM3:
110 tp2 = Vector(triangle.p1.x + triangle.get_width()/2, triangle.p2.y);
112 case AATriangle::DEFORM4:
113 tp1 = Vector(triangle.p1.x + triangle.get_width()/2, triangle.p1.y);
120 switch(triangle.dir & AATriangle::DIRECTION_MASK) {
121 case AATriangle::SOUTHWEST:
122 p1 = Vector(rect.p1.x, rect.p2.y);
123 makePlane(tp1, tp2, normal, c);
125 case AATriangle::NORTHEAST:
126 p1 = Vector(rect.p2.x, rect.p1.y);
127 makePlane(tp2, tp1, normal, c);
129 case AATriangle::SOUTHEAST:
131 makePlane(Vector(tp1.x, tp2.y),
132 Vector(tp2.x, tp1.y), normal, c);
134 case AATriangle::NORTHWEST:
136 makePlane(Vector(tp2.x, tp1.y),
137 Vector(tp1.x, tp2.y), normal, c);
143 float n_p1 = -(normal * p1);
144 float depth = n_p1 - c;
147 float time = depth / -(normal * movement);
148 if(time < hit.time) {