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"
21 bool rectcollision(base_type* one, base_type* two)
23 return (one->x >= two->x - one->width + 1 &&
24 one->x <= two->x + two->width - 1 &&
25 one->y >= two->y - one->height + 1 &&
26 one->y <= two->y + two->height - 1);
29 bool rectcollision_offset(base_type* one, base_type* two, float off_x, float off_y)
31 return (one->x >= two->x - one->width +off_x + 1 &&
32 one->x <= two->x + two->width + off_x - 1 &&
33 one->y >= two->y - one->height + off_y + 1 &&
34 one->y <= two->y + two->height + off_y - 1);
37 bool collision_object_map(base_type* pbase)
39 int v = (int)pbase->height / 16;
40 int h = (int)pbase->width / 16;
42 if(issolid(pbase->x + 1, pbase->y + 1) ||
43 issolid(pbase->x + pbase->width -1, pbase->y + 1) ||
44 issolid(pbase->x +1, pbase->y + pbase->height -1) ||
45 issolid(pbase->x + pbase->width -1, pbase->y + pbase->height - 1))
48 for(int i = 1; i < h; ++i)
50 if(issolid(pbase->x + i*16,pbase->y + 1))
54 for(int i = 1; i < h; ++i)
56 if( issolid(pbase->x + i*16,pbase->y + pbase->height - 1))
60 for(int i = 1; i < v; ++i)
62 if( issolid(pbase->x + 1, pbase->y + i*16))
65 for(int i = 1; i < v; ++i)
67 if( issolid(pbase->x + pbase->width - 1, pbase->y + i*16))
75 void collision_swept_object_map(base_type* old, base_type* current)
77 int steps; /* Used to speed up the collision tests, by stepping every 16pixels in the path. */
79 float lpath; /* Holds the longest path, which is either in X or Y direction. */
80 float xd,yd; /* Hold the smallest steps in X and Y directions. */
81 float temp, xt, yt; /* Temporary variable. */
87 if(old->x == current->x && old->y == current->y)
91 else if(old->x == current->x && old->y != current->y)
93 lpath = current->y - old->y;
107 else if(old->x != current->x && old->y == current->y)
109 lpath = current->x - old->x;
124 lpath = current->x - old->x;
127 if(current->y - old->y > lpath || old->y - current->y > lpath)
128 lpath = current->y - old->y;
132 xd = (current->x - old->x) / lpath;
133 yd = (current->y - old->y) / lpath;
136 steps = (int)(lpath / (float)16);
141 for(float i = 0; i <= lpath; old->x += xd, old->y += yd, ++i)
150 if(collision_object_map(old))
155 current->y = old->y - yd;
156 while(collision_object_map(current))
160 current->x = old->x - xd;
161 while(collision_object_map(current))
167 current->x = old->x - xd;
168 current->y = old->y - yd;
169 while(collision_object_map(current))
177 if(!collision_object_map(current))
183 if(!collision_object_map(current))
190 while(!collision_object_map(current))
208 Tile* gettile(float x, float y)
210 return TileManager::instance()->get(World::current()->get_level()->gettileid(x, y));
213 bool issolid(float x, float y)
215 Tile* tile = gettile(x,y);
216 return tile && tile->solid;
219 bool isbrick(float x, float y)
221 Tile* tile = gettile(x,y);
222 return tile && tile->brick;
225 bool isice(float x, float y)
227 Tile* tile = gettile(x,y);
228 return tile && tile->ice;
231 bool isfullbox(float x, float y)
233 Tile* tile = gettile(x,y);
234 return tile && tile->fullbox;
237 bool isdistro(float x, float y)
239 Tile* tile = gettile(x,y);
240 return tile && tile->distro;