4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.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
23 #include "collision.h"
36 bool rectcollision(const base_type& one, const base_type& two)
38 return (one.x >= two.x - one.width + 1 &&
39 one.x <= two.x + two.width - 1 &&
40 one.y >= two.y - one.height + 1 &&
41 one.y <= two.y + two.height - 1);
44 bool rectcollision_offset(const base_type& one, const base_type& two, float off_x, float off_y)
46 return (one.x >= two.x - one.width + off_x + 1 &&
47 one.x <= two.x + two.width + off_x - 1 &&
48 one.y >= two.y - one.height + off_y + 1 &&
49 one.y <= two.y + two.height + off_y - 1);
52 bool collision_object_map(const base_type& base)
54 const TileMap& tilemap = *Sector::current()->solids;
56 // we make the collision rectangle 1 pixel smaller
57 int starttilex = int(base.x+1) / 32;
58 int starttiley = int(base.y+1) / 32;
59 int max_x = int(base.x + base.width);
60 int max_y = int(base.y + base.height);
64 for(int x = starttilex; x*32 < max_x; ++x) {
65 for(int y = starttiley; y*32 < max_y; ++y) {
66 Tile* tile = tilemap.get_tile(x, y);
67 if(tile && tile->attributes & Tile::SOLID)
80 void* collision_func(const base_type& base, tiletestfunction function)
82 const TileMap& tilemap = *Sector::current()->solids;
84 int starttilex = int(base.x) / 32;
85 int starttiley = int(base.y) / 32;
86 int max_x = int(base.x + base.width);
87 int max_y = int(base.y + base.height);
89 for(int x = starttilex; x*32 < max_x; ++x) {
90 for(int y = starttiley; y*32 < max_y; ++y) {
91 Tile* tile = tilemap.get_tile(x, y);
92 void* result = function(tile);
101 static void* test_goal_tile_function(Tile* tile)
103 if(tile && (tile->attributes & Tile::GOAL))
108 Tile* collision_goal(const base_type& base)
110 return (Tile*) collision_func(base, test_goal_tile_function);
113 void collision_swept_object_map(base_type* old, base_type* current)
115 int steps; /* Used to speed up the collision tests, by stepping every 16pixels in the path. */
117 float lpath; /* Holds the longest path, which is either in X or Y direction. */
118 float xd,yd; /* Hold the smallest steps in X and Y directions. */
119 float temp, xt, yt; /* Temporary variable. */
125 if(old->x == current->x && old->y == current->y)
129 else if(old->x == current->x && old->y != current->y)
131 lpath = current->y - old->y;
145 else if(old->x != current->x && old->y == current->y)
147 lpath = current->x - old->x;
162 lpath = current->x - old->x;
165 if(current->y - old->y > lpath || old->y - current->y > lpath)
166 lpath = current->y - old->y;
170 xd = (current->x - old->x) / lpath;
171 yd = (current->y - old->y) / lpath;
174 steps = (int)(lpath / (float)16);
176 float orig_x = old->x;
177 float orig_y = old->y;
181 for(float i = 0; i <= lpath; old->x += xd, old->y += yd, ++i)
190 if(collision_object_map(*old))
192 if(tileinfo.tile->slope_angle != 0)
193 { // in case this is a slope, set the right Y position
195 if(tileinfo.tile->slope_angle > 0 && tileinfo.tile->slope_angle < M_PI/2)
196 current->y = tileinfo.y - current->height +
197 (tileinfo.x - current->x)*tan(M_PI/2 - tileinfo.tile->slope_angle)
200 if(tileinfo.tile->slope_angle > M_PI/2 && tileinfo.tile->slope_angle < M_PI)
201 current->y = tileinfo.y - current->height +
202 (current->x - tileinfo.x)*tan(M_PI - tileinfo.tile->slope_angle)
210 current->y = old->y - yd;
211 while(collision_object_map(*current))
215 current->x = old->x - xd;
216 while(collision_object_map(*current))
222 current->x = old->x - xd;
223 current->y = old->y - yd;
224 while(collision_object_map(*current))
232 if(!collision_object_map(*current))
238 if(!collision_object_map(*current))
245 while(!collision_object_map(*current))
260 if((xd > 0 && current->x < orig_x) || (xd < 0 && current->x > orig_x))
262 if((yd > 0 && current->y < orig_y) || (yd < 0 && current->y > orig_y))
268 Tile* gettile(float x, float y)
270 const TileMap& tilemap = *Sector::current()->solids;
271 return tilemap.get_tile_at(Vector(x, y));
274 bool issolid(float x, float y)
276 Tile* tile = gettile(x,y);
277 return tile && (tile->attributes & Tile::SOLID);
280 bool isbrick(float x, float y)
282 Tile* tile = gettile(x,y);
283 return tile && (tile->attributes & Tile::BRICK);
286 bool isice(float x, float y)
288 Tile* tile = gettile(x,y);
289 return tile && (tile->attributes & Tile::ICE);
292 bool isspike(float x, float y)
294 Tile* tile = gettile(x,y);
295 return tile && (tile->attributes & Tile::SPIKE);
298 bool isfullbox(float x, float y)
300 Tile* tile = gettile(x,y);
301 return tile && (tile->attributes & Tile::FULLBOX);
304 bool iscoin(float x, float y)
306 Tile* tile = gettile(x,y);
307 return tile && (tile->attributes & Tile::COIN);