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 TileMap& tilemap = *Sector::current()->solids;
49 // we make the collision rectangle 1 pixel smaller
50 int starttilex = int(base.x+1) / 32;
51 int starttiley = int(base.y+1) / 32;
52 int max_x = int(base.x + base.width);
53 int max_y = int(base.y + base.height);
55 for(int x = starttilex; x*32 < max_x; ++x) {
56 for(int y = starttiley; y*32 < max_y; ++y) {
57 Tile* tile = tilemap.get_tile(x, y);
58 if(tile && tile->attributes & Tile::SOLID)
66 void* collision_func(const base_type& base, tiletestfunction function)
68 const TileMap& tilemap = *Sector::current()->solids;
70 int starttilex = int(base.x) / 32;
71 int starttiley = int(base.y) / 32;
72 int max_x = int(base.x + base.width);
73 int max_y = int(base.y + base.height);
75 for(int x = starttilex; x*32 < max_x; ++x) {
76 for(int y = starttiley; y*32 < max_y; ++y) {
77 Tile* tile = tilemap.get_tile(x, y);
78 void* result = function(tile);
87 static void* test_goal_tile_function(Tile* tile)
89 if(tile && (tile->attributes & Tile::GOAL))
94 Tile* collision_goal(const base_type& base)
96 return (Tile*) collision_func(base, test_goal_tile_function);
99 void collision_swept_object_map(base_type* old, base_type* current)
101 int steps; /* Used to speed up the collision tests, by stepping every 16pixels in the path. */
103 float lpath; /* Holds the longest path, which is either in X or Y direction. */
104 float xd,yd; /* Hold the smallest steps in X and Y directions. */
105 float temp, xt, yt; /* Temporary variable. */
111 if(old->x == current->x && old->y == current->y)
115 else if(old->x == current->x && old->y != current->y)
117 lpath = current->y - old->y;
131 else if(old->x != current->x && old->y == current->y)
133 lpath = current->x - old->x;
148 lpath = current->x - old->x;
151 if(current->y - old->y > lpath || old->y - current->y > lpath)
152 lpath = current->y - old->y;
156 xd = (current->x - old->x) / lpath;
157 yd = (current->y - old->y) / lpath;
160 steps = (int)(lpath / (float)16);
162 float orig_x = old->x;
163 float orig_y = old->y;
167 for(float i = 0; i <= lpath; old->x += xd, old->y += yd, ++i)
176 if(collision_object_map(*old))
181 current->y = old->y - yd;
182 while(collision_object_map(*current))
186 current->x = old->x - xd;
187 while(collision_object_map(*current))
193 current->x = old->x - xd;
194 current->y = old->y - yd;
195 while(collision_object_map(*current))
203 if(!collision_object_map(*current))
209 if(!collision_object_map(*current))
216 while(!collision_object_map(*current))
230 if((xd > 0 && current->x < orig_x) || (xd < 0 && current->x > orig_x))
232 if((yd > 0 && current->y < orig_y) || (yd < 0 && current->y > orig_y))
238 Tile* gettile(float x, float y)
240 const TileMap& tilemap = *Sector::current()->solids;
241 return tilemap.get_tile_at(Vector(x, y));
244 bool issolid(float x, float y)
246 Tile* tile = gettile(x,y);
247 return tile && (tile->attributes & Tile::SOLID);
250 bool isbrick(float x, float y)
252 Tile* tile = gettile(x,y);
253 return tile && (tile->attributes & Tile::BRICK);
256 bool isice(float x, float y)
258 Tile* tile = gettile(x,y);
259 return tile && (tile->attributes & Tile::ICE);
262 bool isspike(float x, float y)
264 Tile* tile = gettile(x,y);
265 return tile && (tile->attributes & Tile::SPIKE);
268 bool isfullbox(float x, float y)
270 Tile* tile = gettile(x,y);
271 return tile && (tile->attributes & Tile::FULLBOX);
274 bool iscoin(float x, float y)
276 Tile* tile = gettile(x,y);
277 return tile && (tile->attributes & Tile::COIN);