4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 #include "collision.hpp"
29 #include "math/vector.hpp"
30 #include "math/aatriangle.hpp"
31 #include "math/rect.hpp"
32 #include "collision_hit.hpp"
34 static const float DELTA = .0001;
37 Collision::intersects(const Rect& r1, const Rect& r2)
39 if(r1.p2.x < r2.p1.x || r1.p1.x > r2.p2.x)
41 if(r1.p2.y < r2.p1.y || r1.p1.y > r2.p2.y)
48 Collision::rectangle_rectangle(CollisionHit& hit, const Rect& r1,
49 const Vector& movement, const Rect& r2)
51 if(r1.p2.x < r2.p1.x || r1.p1.x > r2.p2.x)
53 if(r1.p2.y < r2.p1.y || r1.p1.y > r2.p2.y)
56 if(movement.x > DELTA) {
57 hit.depth = r1.p2.x - r2.p1.x;
58 hit.time = hit.depth / movement.x;
61 } else if(movement.x < -DELTA) {
62 hit.depth = r2.p2.x - r1.p1.x;
63 hit.time = hit.depth / -movement.x;
67 if(movement.y > -DELTA && movement.y < DELTA) {
77 if(movement.y > DELTA) {
78 float ydepth = r1.p2.y - r2.p1.y;
79 float yt = ydepth / movement.y;
86 } else if(movement.y < -DELTA) {
87 float ydepth = r2.p2.y - r1.p1.y;
88 float yt = ydepth / -movement.y;
100 //---------------------------------------------------------------------------
102 static void makePlane(const Vector& p1, const Vector& p2, Vector& n, float& c)
104 n = Vector(p2.y-p1.y, p1.x-p2.x);
106 float nval = n.norm();
112 Collision::rectangle_aatriangle(CollisionHit& hit, const Rect& rect,
113 const Vector& movement, const AATriangle& triangle)
115 if(!rectangle_rectangle(hit, rect, movement, (const Rect&) triangle))
122 switch(triangle.dir & AATriangle::DEFORM_MASK) {
127 case AATriangle::DEFORM1:
128 tp1 = Vector(triangle.p1.x, triangle.p1.y + triangle.get_height()/2);
131 case AATriangle::DEFORM2:
133 tp2 = Vector(triangle.p2.x, triangle.p1.y + triangle.get_height()/2);
135 case AATriangle::DEFORM3:
137 tp2 = Vector(triangle.p1.x + triangle.get_width()/2, triangle.p2.y);
139 case AATriangle::DEFORM4:
140 tp1 = Vector(triangle.p1.x + triangle.get_width()/2, triangle.p1.y);
147 switch(triangle.dir & AATriangle::DIRECTION_MASK) {
148 case AATriangle::SOUTHWEST:
149 p1 = Vector(rect.p1.x, rect.p2.y);
150 makePlane(tp1, tp2, normal, c);
152 case AATriangle::NORTHEAST:
153 p1 = Vector(rect.p2.x, rect.p1.y);
154 makePlane(tp2, tp1, normal, c);
156 case AATriangle::SOUTHEAST:
158 makePlane(Vector(tp1.x, tp2.y),
159 Vector(tp2.x, tp1.y), normal, c);
161 case AATriangle::NORTHWEST:
163 makePlane(Vector(tp2.x, tp1.y),
164 Vector(tp1.x, tp2.y), normal, c);
170 float n_p1 = -(normal * p1);
171 float depth = n_p1 - c;
174 float time = depth / -(normal * movement);
175 if(time < hit.time) {