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"
32 bool rectcollision(const base_type& one, const base_type& two)
34 return (one.x >= two.x - one.width + 1 &&
35 one.x <= two.x + two.width - 1 &&
36 one.y >= two.y - one.height + 1 &&
37 one.y <= two.y + two.height - 1);
40 bool rectcollision_offset(const base_type& one, const base_type& two, float off_x, float off_y)
42 return (one.x >= two.x - one.width + off_x + 1 &&
43 one.x <= two.x + two.width + off_x - 1 &&
44 one.y >= two.y - one.height + off_y + 1 &&
45 one.y <= two.y + two.height + off_y - 1);
48 bool collision_object_map(const Rectangle& rect)
53 base.width = rect.get_width();
54 base.height = rect.get_height();
55 return collision_object_map(base);
58 bool collision_object_map(const base_type& base)
60 const TileMap& tilemap = *Sector::current()->solids;
62 // we make the collision rectangle 1 pixel smaller
63 int starttilex = int(base.x+1) / 32;
64 int starttiley = int(base.y+1) / 32;
65 int max_x = int(base.x + base.width);
66 int max_y = int(base.y + base.height);
68 for(int x = starttilex; x*32 < max_x; ++x) {
69 for(int y = starttiley; y*32 < max_y; ++y) {
70 const Tile* tile = tilemap.get_tile(x, y);
71 if(tile && tile->attributes & Tile::SOLID)
79 void* collision_func(const base_type& base, tiletestfunction function)
81 const TileMap& tilemap = *Sector::current()->solids;
83 int starttilex = int(base.x) / 32;
84 int starttiley = int(base.y) / 32;
85 int max_x = int(base.x + base.width);
86 int max_y = int(base.y + base.height);
88 for(int x = starttilex; x*32 < max_x; ++x) {
89 for(int y = starttiley; y*32 < max_y; ++y) {
90 const Tile* tile = tilemap.get_tile(x, y);
91 void* result = function(tile);
100 static void* test_goal_tile_function(const Tile* tile)
102 if(tile && (tile->attributes & Tile::GOAL))
103 return const_cast<void*> ((const void*) tile); // evil cast...
107 const Tile* collision_goal(const Rectangle& rect)
109 // too lazy to rewrite for now, so we transform to base_type...
113 base.width = rect.get_width();
114 base.height = rect.get_height();
115 return (const Tile*) collision_func(base, test_goal_tile_function);
118 void collision_swept_object_map(base_type* old, base_type* current)
120 int steps; /* Used to speed up the collision tests, by stepping every 16pixels in the path. */
122 float lpath; /* Holds the longest path, which is either in X or Y direction. */
123 float xd,yd; /* Hold the smallest steps in X and Y directions. */
124 float temp, xt, yt; /* Temporary variable. */
130 if(old->x == current->x && old->y == current->y)
134 else if(old->x == current->x && old->y != current->y)
136 lpath = current->y - old->y;
150 else if(old->x != current->x && old->y == current->y)
152 lpath = current->x - old->x;
167 lpath = current->x - old->x;
170 if(current->y - old->y > lpath || old->y - current->y > lpath)
171 lpath = current->y - old->y;
175 xd = (current->x - old->x) / lpath;
176 yd = (current->y - old->y) / lpath;
179 steps = (int)(lpath / (float)16);
181 float orig_x = old->x;
182 float orig_y = old->y;
186 for(float i = 0; i <= lpath; old->x += xd, old->y += yd, ++i)
195 if(collision_object_map(*old))
200 current->y = old->y - yd;
201 while(collision_object_map(*current))
205 current->x = old->x - xd;
206 while(collision_object_map(*current))
212 current->x = old->x - xd;
213 current->y = old->y - yd;
214 while(collision_object_map(*current))
222 if(!collision_object_map(*current))
228 if(!collision_object_map(*current))
235 while(!collision_object_map(*current))
249 if((xd > 0 && current->x < orig_x) || (xd < 0 && current->x > orig_x))
251 if((yd > 0 && current->y < orig_y) || (yd < 0 && current->y > orig_y))
257 const Tile* gettile(float x, float y)
259 const TileMap& tilemap = *Sector::current()->solids;
260 return tilemap.get_tile_at(Vector(x, y));
263 bool issolid(float x, float y)
265 const Tile* tile = gettile(x,y);
266 return tile && (tile->attributes & Tile::SOLID);
269 bool isbrick(float x, float y)
271 const Tile* tile = gettile(x,y);
272 return tile && (tile->attributes & Tile::BRICK);
275 bool isice(float x, float y)
277 const Tile* tile = gettile(x,y);
278 return tile && (tile->attributes & Tile::ICE);
281 bool isspike(float x, float y)
283 const Tile* tile = gettile(x,y);
284 return tile && (tile->attributes & Tile::SPIKE);
287 bool isfullbox(float x, float y)
289 const Tile* tile = gettile(x,y);
290 return tile && (tile->attributes & Tile::FULLBOX);
293 bool iscoin(float x, float y)
295 const Tile* tile = gettile(x,y);
296 return tile && (tile->attributes & Tile::COIN);