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)
50 const Level& level = *World::current()->get_level();
51 TileManager& tilemanager = *TileManager::instance();
53 // we make the collision rectangle 1 pixel smaller
54 int starttilex = int(base.x+1) / 32;
55 int starttiley = int(base.y+1) / 32;
56 int max_x = int(base.x + base.width);
57 int max_y = int(base.y + base.height);
59 for(int x = starttilex; x*32 < max_x; ++x) {
60 for(int y = starttiley; y*32 < max_y; ++y) {
61 Tile* tile = tilemanager.get(level.get_tile_at(x, y));
62 if(tile && tile->solid)
70 void* collision_func(const base_type& base, tiletestfunction function)
72 const Level& level = *World::current()->get_level();
73 TileManager& tilemanager = *TileManager::instance();
75 int starttilex = int(base.x) / 32;
76 int starttiley = int(base.y) / 32;
77 int max_x = int(base.x + base.width);
78 int max_y = int(base.y + base.height);
80 for(int x = starttilex; x*32 < max_x; ++x) {
81 for(int y = starttiley; y*32 < max_y; ++y) {
82 Tile* tile = tilemanager.get(level.get_tile_at(x, y));
83 void* result = function(tile);
92 static void* test_goal_tile_function(Tile* tile)
94 if(tile && tile->goal)
99 Tile* collision_goal(const base_type& base)
101 return (Tile*) collision_func(base, test_goal_tile_function);
104 void collision_swept_object_map(base_type* old, base_type* current)
106 int steps; /* Used to speed up the collision tests, by stepping every 16pixels in the path. */
108 float lpath; /* Holds the longest path, which is either in X or Y direction. */
109 float xd,yd; /* Hold the smallest steps in X and Y directions. */
110 float temp, xt, yt; /* Temporary variable. */
116 if(old->x == current->x && old->y == current->y)
120 else if(old->x == current->x && old->y != current->y)
122 lpath = current->y - old->y;
136 else if(old->x != current->x && old->y == current->y)
138 lpath = current->x - old->x;
153 lpath = current->x - old->x;
156 if(current->y - old->y > lpath || old->y - current->y > lpath)
157 lpath = current->y - old->y;
161 xd = (current->x - old->x) / lpath;
162 yd = (current->y - old->y) / lpath;
165 steps = (int)(lpath / (float)16);
167 float orig_x = old->x;
168 float orig_y = old->y;
172 for(float i = 0; i <= lpath; old->x += xd, old->y += yd, ++i)
181 if(collision_object_map(*old))
186 current->y = old->y - yd;
187 while(collision_object_map(*current))
191 current->x = old->x - xd;
192 while(collision_object_map(*current))
198 current->x = old->x - xd;
199 current->y = old->y - yd;
200 while(collision_object_map(*current))
208 if(!collision_object_map(*current))
214 if(!collision_object_map(*current))
221 while(!collision_object_map(*current))
235 if((xd > 0 && current->x < orig_x) || (xd < 0 && current->x > orig_x))
237 if((yd > 0 && current->y < orig_y) || (yd < 0 && current->y > orig_y))
243 Tile* gettile(float x, float y)
245 return TileManager::instance()->get(World::current()->get_level()->gettileid(x, y));
248 bool issolid(float x, float y)
250 Tile* tile = gettile(x,y);
251 return tile && tile->solid;
254 bool isbrick(float x, float y)
256 Tile* tile = gettile(x,y);
257 return tile && tile->brick;
260 bool isice(float x, float y)
262 Tile* tile = gettile(x,y);
263 return tile && tile->ice;
266 bool isfullbox(float x, float y)
268 Tile* tile = gettile(x,y);
269 return tile && tile->fullbox;
272 bool isdistro(float x, float y)
274 Tile* tile = gettile(x,y);
275 return tile && tile->distro;