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
25 #include "collision.h"
33 bool rectcollision(const base_type& one, const base_type& two)
35 return (one.x >= two.x - one.width + 1 &&
36 one.x <= two.x + two.width - 1 &&
37 one.y >= two.y - one.height + 1 &&
38 one.y <= two.y + two.height - 1);
41 bool rectcollision_offset(const base_type& one, const base_type& two, float off_x, float off_y)
43 return (one.x >= two.x - one.width + off_x + 1 &&
44 one.x <= two.x + two.width + off_x - 1 &&
45 one.y >= two.y - one.height + off_y + 1 &&
46 one.y <= two.y + two.height + off_y - 1);
49 bool collision_object_map(const Rectangle& rect)
54 base.width = rect.get_width();
55 base.height = rect.get_height();
56 return collision_object_map(base);
59 bool collision_object_map(const base_type& base)
61 const TileMap& tilemap = *Sector::current()->solids;
63 // we make the collision rectangle 1 pixel smaller
64 int starttilex = int(base.x+1) / 32;
65 int starttiley = int(base.y+1) / 32;
66 int max_x = int(base.x + base.width);
67 int max_y = int(base.y + base.height);
69 for(int x = starttilex; x*32 < max_x; ++x) {
70 for(int y = starttiley; y*32 < max_y; ++y) {
71 const Tile* tile = tilemap.get_tile(x, y);
72 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 const Tile* tile = tilemap.get_tile(x, y);
92 void* result = function(tile);
101 static void* test_goal_tile_function(const Tile* tile)
103 if(tile && (tile->attributes & Tile::GOAL))
104 return const_cast<void*> ((const void*) tile); // evil cast...
108 const Tile* collision_goal(const Rectangle& rect)
110 // too lazy to rewrite for now, so we transform to base_type...
114 base.width = rect.get_width();
115 base.height = rect.get_height();
116 return (const Tile*) collision_func(base, test_goal_tile_function);
119 void collision_swept_object_map(base_type* old, base_type* current)
121 int steps; /* Used to speed up the collision tests, by stepping every 16pixels in the path. */
123 float lpath; /* Holds the longest path, which is either in X or Y direction. */
124 float xd,yd; /* Hold the smallest steps in X and Y directions. */
125 float temp, xt, yt; /* Temporary variable. */
131 if(old->x == current->x && old->y == current->y)
135 else if(old->x == current->x && old->y != current->y)
137 lpath = current->y - old->y;
151 else if(old->x != current->x && old->y == current->y)
153 lpath = current->x - old->x;
168 lpath = current->x - old->x;
171 if(current->y - old->y > lpath || old->y - current->y > lpath)
172 lpath = current->y - old->y;
176 xd = (current->x - old->x) / lpath;
177 yd = (current->y - old->y) / lpath;
180 steps = (int)(lpath / (float)16);
182 float orig_x = old->x;
183 float orig_y = old->y;
187 for(float i = 0; i <= lpath; old->x += xd, old->y += yd, ++i)
196 if(collision_object_map(*old))
201 current->y = old->y - yd;
202 while(collision_object_map(*current))
206 current->x = old->x - xd;
207 while(collision_object_map(*current))
213 current->x = old->x - xd;
214 current->y = old->y - yd;
215 while(collision_object_map(*current))
223 if(!collision_object_map(*current))
229 if(!collision_object_map(*current))
236 while(!collision_object_map(*current))
250 if((xd > 0 && current->x < orig_x) || (xd < 0 && current->x > orig_x))
252 if((yd > 0 && current->y < orig_y) || (yd < 0 && current->y > orig_y))
258 const Tile* gettile(float x, float y)
260 const TileMap& tilemap = *Sector::current()->solids;
261 return tilemap.get_tile_at(Vector(x, y));
264 bool issolid(float x, float y)
266 const Tile* tile = gettile(x,y);
267 return tile && (tile->attributes & Tile::SOLID);
270 bool isbrick(float x, float y)
272 const Tile* tile = gettile(x,y);
273 return tile && (tile->attributes & Tile::BRICK);
276 bool isice(float x, float y)
278 const Tile* tile = gettile(x,y);
279 return tile && (tile->attributes & Tile::ICE);
282 bool isspike(float x, float y)
284 const Tile* tile = gettile(x,y);
285 return tile && (tile->attributes & Tile::SPIKE);
288 bool isfullbox(float x, float y)
290 const Tile* tile = gettile(x,y);
291 return tile && (tile->attributes & Tile::FULLBOX);
294 bool iscoin(float x, float y)
296 const Tile* tile = gettile(x,y);
297 return tile && (tile->attributes & Tile::COIN);