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
22 #include "collision.h"
29 bool rectcollision(const base_type& one, const base_type& two)
31 return (one.x >= two.x - one.width + 1 &&
32 one.x <= two.x + two.width - 1 &&
33 one.y >= two.y - one.height + 1 &&
34 one.y <= two.y + two.height - 1);
37 bool rectcollision_offset(const base_type& one, const base_type& two, float off_x, float off_y)
39 return (one.x >= two.x - one.width + off_x + 1 &&
40 one.x <= two.x + two.width + off_x - 1 &&
41 one.y >= two.y - one.height + off_y + 1 &&
42 one.y <= two.y + two.height + off_y - 1);
45 bool collision_object_map(const base_type& base)
47 const Level& level = *World::current()->get_level();
48 TileManager& tilemanager = *TileManager::instance();
50 // we make the collision rectangle 1 pixel smaller
51 int starttilex = int(base.x+1) / 32;
52 int starttiley = int(base.y+1) / 32;
53 int max_x = int(base.x + base.width);
54 int max_y = int(base.y + base.height);
56 for(int x = starttilex; x*32 < max_x; ++x) {
57 for(int y = starttiley; y*32 < max_y; ++y) {
58 Tile* tile = tilemanager.get(level.get_tile_at(x, y));
59 if(tile && tile->solid)
67 void* collision_func(const base_type& base, tiletestfunction function)
69 const Level& level = *World::current()->get_level();
70 TileManager& tilemanager = *TileManager::instance();
72 int starttilex = int(base.x) / 32;
73 int starttiley = int(base.y) / 32;
74 int max_x = int(base.x + base.width);
75 int max_y = int(base.y + base.height);
77 for(int x = starttilex; x*32 < max_x; ++x) {
78 for(int y = starttiley; y*32 < max_y; ++y) {
79 Tile* tile = tilemanager.get(level.get_tile_at(x, y));
80 void* result = function(tile);
89 static void* test_goal_tile_function(Tile* tile)
91 if(tile && tile->goal)
96 Tile* collision_goal(const base_type& base)
98 return (Tile*) collision_func(base, test_goal_tile_function);
101 void collision_swept_object_map(base_type* old, base_type* current)
103 int steps; /* Used to speed up the collision tests, by stepping every 16pixels in the path. */
105 float lpath; /* Holds the longest path, which is either in X or Y direction. */
106 float xd,yd; /* Hold the smallest steps in X and Y directions. */
107 float temp, xt, yt; /* Temporary variable. */
113 if(old->x == current->x && old->y == current->y)
117 else if(old->x == current->x && old->y != current->y)
119 lpath = current->y - old->y;
133 else if(old->x != current->x && old->y == current->y)
135 lpath = current->x - old->x;
150 lpath = current->x - old->x;
153 if(current->y - old->y > lpath || old->y - current->y > lpath)
154 lpath = current->y - old->y;
158 xd = (current->x - old->x) / lpath;
159 yd = (current->y - old->y) / lpath;
162 steps = (int)(lpath / (float)16);
164 float orig_x = old->x;
165 float orig_y = old->y;
169 for(float i = 0; i <= lpath; old->x += xd, old->y += yd, ++i)
178 if(collision_object_map(*old))
183 current->y = old->y - yd;
184 while(collision_object_map(*current))
188 current->x = old->x - xd;
189 while(collision_object_map(*current))
195 current->x = old->x - xd;
196 current->y = old->y - yd;
197 while(collision_object_map(*current))
205 if(!collision_object_map(*current))
211 if(!collision_object_map(*current))
218 while(!collision_object_map(*current))
232 if((xd > 0 && current->x < orig_x) || (xd < 0 && current->x > orig_x))
234 if((yd > 0 && current->y < orig_y) || (yd < 0 && current->y > orig_y))
240 Tile* gettile(float x, float y)
242 return TileManager::instance()->get(World::current()->get_level()->gettileid(x, y));
245 bool issolid(float x, float y)
247 Tile* tile = gettile(x,y);
248 return tile && tile->solid;
251 bool isbrick(float x, float y)
253 Tile* tile = gettile(x,y);
254 return tile && tile->brick;
257 bool isice(float x, float y)
259 Tile* tile = gettile(x,y);
260 return tile && tile->ice;
263 bool isfullbox(float x, float y)
265 Tile* tile = gettile(x,y);
266 return tile && tile->fullbox;
269 bool isdistro(float x, float y)
271 Tile* tile = gettile(x,y);
272 return tile && tile->distro;