4 // Copyright (C) 2005 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.h"
29 #include "math/vector.h"
30 #include "math/aatriangle.h"
31 #include "math/rectangle.h"
32 #include "collision_hit.h"
34 static const float DELTA = .0001;
37 Collision::rectangle_rectangle(CollisionHit& hit, const Rectangle& r1,
38 const Vector& movement, const Rectangle& r2)
40 if(r1.p2.x < r2.p1.x || r1.p1.x > r2.p2.x)
42 if(r1.p2.y < r2.p1.y || r1.p1.y > r2.p2.y)
45 if(movement.x > DELTA) {
46 hit.depth = r1.p2.x - r2.p1.x;
47 hit.time = hit.depth / movement.x;
50 } else if(movement.x < -DELTA) {
51 hit.depth = r2.p2.x - r1.p1.x;
52 hit.time = hit.depth / -movement.x;
56 if(movement.y > -DELTA && movement.y < DELTA) {
66 if(movement.y > DELTA) {
67 float ydepth = r1.p2.y - r2.p1.y;
68 float yt = ydepth / movement.y;
75 } else if(movement.y < -DELTA) {
76 float ydepth = r2.p2.y - r1.p1.y;
77 float yt = ydepth / -movement.y;
89 //---------------------------------------------------------------------------
91 static void makePlane(const Vector& p1, const Vector& p2, Vector& n, float& c)
93 n = Vector(p2.y-p1.y, p1.x-p2.x);
95 float nval = n.norm();
101 Collision::rectangle_aatriangle(CollisionHit& hit, const Rectangle& rect,
102 const Vector& movement, const AATriangle& triangle)
104 if(!rectangle_rectangle(hit, rect, movement, (const Rectangle&) triangle))
111 switch(triangle.dir & AATriangle::DEFORM_MASK) {
116 case AATriangle::DEFORM1:
117 tp1 = Vector(triangle.p1.x, triangle.p1.y + triangle.get_height()/2);
120 case AATriangle::DEFORM2:
122 tp2 = Vector(triangle.p2.x, triangle.p1.y + triangle.get_height()/2);
124 case AATriangle::DEFORM3:
126 tp2 = Vector(triangle.p1.x + triangle.get_width()/2, triangle.p2.y);
128 case AATriangle::DEFORM4:
129 tp1 = Vector(triangle.p1.x + triangle.get_width()/2, triangle.p1.y);
136 switch(triangle.dir & AATriangle::DIRECTION_MASK) {
137 case AATriangle::SOUTHWEST:
138 p1 = Vector(rect.p1.x, rect.p2.y);
139 makePlane(tp1, tp2, normal, c);
141 case AATriangle::NORTHEAST:
142 p1 = Vector(rect.p2.x, rect.p1.y);
143 makePlane(tp2, tp1, normal, c);
145 case AATriangle::SOUTHEAST:
147 makePlane(Vector(tp1.x, tp2.y),
148 Vector(tp2.x, tp1.y), normal, c);
150 case AATriangle::NORTHWEST:
152 makePlane(Vector(tp2.x, tp1.y),
153 Vector(tp1.x, tp2.y), normal, c);
159 float n_p1 = -(normal * p1);
160 float depth = n_p1 - c;
163 float time = depth / -(normal * movement);
164 if(time < hit.time) {