2 // C Implementation: collision
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
14 #include "collision.h"
20 bool rectcollision(base_type* one, base_type* two)
22 return (one->x >= two->x - one->width + 1 &&
23 one->x <= two->x + two->width - 1 &&
24 one->y >= two->y - one->height + 1 &&
25 one->y <= two->y + two->height - 1);
28 bool rectcollision_offset(base_type* one, base_type* two, float off_x, float off_y)
30 return (one->x >= two->x - one->width +off_x + 1 &&
31 one->x <= two->x + two->width + off_x - 1 &&
32 one->y >= two->y - one->height + off_y + 1 &&
33 one->y <= two->y + two->height + off_y - 1);
36 bool collision_object_map(base_type* pbase)
38 int v = (int)pbase->height / 16;
39 int h = (int)pbase->width / 16;
41 if(issolid(pbase->x + 1, pbase->y + 1) ||
42 issolid(pbase->x + pbase->width -1, pbase->y + 1) ||
43 issolid(pbase->x +1, pbase->y + pbase->height -1) ||
44 issolid(pbase->x + pbase->width -1, pbase->y + pbase->height - 1))
47 for(int i = 1; i < h; ++i)
49 if(issolid(pbase->x + i*16,pbase->y + 1))
53 for(int i = 1; i < h; ++i)
55 if( issolid(pbase->x + i*16,pbase->y + pbase->height - 1))
59 for(int i = 1; i < v; ++i)
61 if( issolid(pbase->x + 1, pbase->y + i*16))
64 for(int i = 1; i < v; ++i)
66 if( issolid(pbase->x + pbase->width - 1, pbase->y + i*16))
74 void collision_swept_object_map(base_type* old, base_type* current)
76 int steps; /* Used to speed up the collision tests, by stepping every 16pixels in the path. */
78 float lpath; /* Holds the longest path, which is either in X or Y direction. */
79 float xd,yd; /* Hold the smallest steps in X and Y directions. */
80 float temp, xt, yt; /* Temporary variable. */
86 if(old->x == current->x && old->y == current->y)
90 else if(old->x == current->x && old->y != current->y)
92 lpath = current->y - old->y;
106 else if(old->x != current->x && old->y == current->y)
108 lpath = current->x - old->x;
123 lpath = current->x - old->x;
126 if(current->y - old->y > lpath || old->y - current->y > lpath)
127 lpath = current->y - old->y;
131 xd = (current->x - old->x) / lpath;
132 yd = (current->y - old->y) / lpath;
135 steps = (int)(lpath / (float)16);
140 for(float i = 0; i <= lpath; old->x += xd, old->y += yd, ++i)
149 if(collision_object_map(old))
154 current->y = old->y - yd;
155 while(collision_object_map(current))
159 current->x = old->x - xd;
160 while(collision_object_map(current))
166 current->x = old->x - xd;
167 current->y = old->y - yd;
168 while(collision_object_map(current))
176 if(!collision_object_map(current))
182 if(!collision_object_map(current))
189 while(!collision_object_map(current))
207 Tile* gettile(float x, float y)
209 return TileManager::instance()->get(World::current()->get_level()->gettileid(x, y));
212 bool issolid(float x, float y)
214 Tile* tile = gettile(x,y);
215 return tile && tile->solid;
218 bool isbrick(float x, float y)
220 Tile* tile = gettile(x,y);
221 return tile && tile->brick;
224 bool isice(float x, float y)
226 Tile* tile = gettile(x,y);
227 return tile && tile->ice;
230 bool isfullbox(float x, float y)
232 Tile* tile = gettile(x,y);
233 return tile && tile->fullbox;
236 bool isdistro(float x, float y)
238 Tile* tile = gettile(x,y);
239 return tile && tile->distro;