3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "collision.h"
27 #include "util/timetaker.h"
28 #include "main.h" // g_profiler
32 // Checks for collision of a moving aabbox with a static aabbox
33 // Returns -1 if no collision, 0 if X collision, 1 if Y collision, 2 if Z collision
34 // The time after which the collision occurs is stored in dtime.
35 int axisAlignedCollision(
36 const aabb3f &staticbox, const aabb3f &movingbox,
37 const v3f &speed, f32 d, f32 &dtime)
39 //TimeTaker tt("axisAlignedCollision");
41 f32 xsize = (staticbox.MaxEdge.X - staticbox.MinEdge.X);
42 f32 ysize = (staticbox.MaxEdge.Y - staticbox.MinEdge.Y);
43 f32 zsize = (staticbox.MaxEdge.Z - staticbox.MinEdge.Z);
46 movingbox.MinEdge.X - staticbox.MinEdge.X,
47 movingbox.MinEdge.Y - staticbox.MinEdge.Y,
48 movingbox.MinEdge.Z - staticbox.MinEdge.Z,
49 movingbox.MaxEdge.X - staticbox.MinEdge.X,
50 movingbox.MaxEdge.Y - staticbox.MinEdge.Y,
51 movingbox.MaxEdge.Z - staticbox.MinEdge.Z
54 if(speed.X > 0) // Check for collision with X- plane
56 if(relbox.MaxEdge.X <= d)
58 dtime = - relbox.MaxEdge.X / speed.X;
59 if((relbox.MinEdge.Y + speed.Y * dtime < ysize) &&
60 (relbox.MaxEdge.Y + speed.Y * dtime > 0) &&
61 (relbox.MinEdge.Z + speed.Z * dtime < zsize) &&
62 (relbox.MaxEdge.Z + speed.Z * dtime > 0))
65 else if(relbox.MinEdge.X > xsize)
70 else if(speed.X < 0) // Check for collision with X+ plane
72 if(relbox.MinEdge.X >= xsize - d)
74 dtime = (xsize - relbox.MinEdge.X) / speed.X;
75 if((relbox.MinEdge.Y + speed.Y * dtime < ysize) &&
76 (relbox.MaxEdge.Y + speed.Y * dtime > 0) &&
77 (relbox.MinEdge.Z + speed.Z * dtime < zsize) &&
78 (relbox.MaxEdge.Z + speed.Z * dtime > 0))
81 else if(relbox.MaxEdge.X < 0)
89 if(speed.Y > 0) // Check for collision with Y- plane
91 if(relbox.MaxEdge.Y <= d)
93 dtime = - relbox.MaxEdge.Y / speed.Y;
94 if((relbox.MinEdge.X + speed.X * dtime < xsize) &&
95 (relbox.MaxEdge.X + speed.X * dtime > 0) &&
96 (relbox.MinEdge.Z + speed.Z * dtime < zsize) &&
97 (relbox.MaxEdge.Z + speed.Z * dtime > 0))
100 else if(relbox.MinEdge.Y > ysize)
105 else if(speed.Y < 0) // Check for collision with Y+ plane
107 if(relbox.MinEdge.Y >= ysize - d)
109 dtime = (ysize - relbox.MinEdge.Y) / speed.Y;
110 if((relbox.MinEdge.X + speed.X * dtime < xsize) &&
111 (relbox.MaxEdge.X + speed.X * dtime > 0) &&
112 (relbox.MinEdge.Z + speed.Z * dtime < zsize) &&
113 (relbox.MaxEdge.Z + speed.Z * dtime > 0))
116 else if(relbox.MaxEdge.Y < 0)
124 if(speed.Z > 0) // Check for collision with Z- plane
126 if(relbox.MaxEdge.Z <= d)
128 dtime = - relbox.MaxEdge.Z / speed.Z;
129 if((relbox.MinEdge.X + speed.X * dtime < xsize) &&
130 (relbox.MaxEdge.X + speed.X * dtime > 0) &&
131 (relbox.MinEdge.Y + speed.Y * dtime < ysize) &&
132 (relbox.MaxEdge.Y + speed.Y * dtime > 0))
135 //else if(relbox.MinEdge.Z > zsize)
140 else if(speed.Z < 0) // Check for collision with Z+ plane
142 if(relbox.MinEdge.Z >= zsize - d)
144 dtime = (zsize - relbox.MinEdge.Z) / speed.Z;
145 if((relbox.MinEdge.X + speed.X * dtime < xsize) &&
146 (relbox.MaxEdge.X + speed.X * dtime > 0) &&
147 (relbox.MinEdge.Y + speed.Y * dtime < ysize) &&
148 (relbox.MaxEdge.Y + speed.Y * dtime > 0))
151 //else if(relbox.MaxEdge.Z < 0)
161 // Checks if moving the movingbox up by the given distance would hit a ceiling.
162 bool wouldCollideWithCeiling(
163 const std::vector<aabb3f> &staticboxes,
164 const aabb3f &movingbox,
165 f32 y_increase, f32 d)
167 //TimeTaker tt("wouldCollideWithCeiling");
169 assert(y_increase >= 0);
171 for(std::vector<aabb3f>::const_iterator
172 i = staticboxes.begin();
173 i != staticboxes.end(); i++)
175 const aabb3f& staticbox = *i;
176 if((movingbox.MaxEdge.Y - d <= staticbox.MinEdge.Y) &&
177 (movingbox.MaxEdge.Y + y_increase > staticbox.MinEdge.Y) &&
178 (movingbox.MinEdge.X < staticbox.MaxEdge.X) &&
179 (movingbox.MaxEdge.X > staticbox.MinEdge.X) &&
180 (movingbox.MinEdge.Z < staticbox.MaxEdge.Z) &&
181 (movingbox.MaxEdge.Z > staticbox.MinEdge.Z))
189 collisionMoveResult collisionMoveSimple(Map *map, IGameDef *gamedef,
190 f32 pos_max_d, const aabb3f &box_0,
191 f32 stepheight, f32 dtime,
192 v3f &pos_f, v3f &speed_f, v3f &accel_f)
194 //TimeTaker tt("collisionMoveSimple");
195 ScopeProfiler sp(g_profiler, "collisionMoveSimple avg", SPT_AVG);
197 collisionMoveResult result;
200 Calculate new velocity
203 infostream<<"collisionMoveSimple: WARNING: maximum step interval exceeded, lost movement details!"<<std::endl;
206 speed_f += accel_f * dtime;
208 // If there is no speed, there are no collisions
209 if(speed_f.getLength() == 0)
213 Collect node boxes in movement range
215 std::vector<aabb3f> cboxes;
216 std::vector<bool> is_unloaded;
217 std::vector<bool> is_step_up;
218 std::vector<int> bouncy_values;
219 std::vector<v3s16> node_positions;
221 //TimeTaker tt2("collisionMoveSimple collect boxes");
222 ScopeProfiler sp(g_profiler, "collisionMoveSimple collect boxes avg", SPT_AVG);
224 v3s16 oldpos_i = floatToInt(pos_f, BS);
225 v3s16 newpos_i = floatToInt(pos_f + speed_f * dtime, BS);
226 s16 min_x = MYMIN(oldpos_i.X, newpos_i.X) + (box_0.MinEdge.X / BS) - 1;
227 s16 min_y = MYMIN(oldpos_i.Y, newpos_i.Y) + (box_0.MinEdge.Y / BS) - 1;
228 s16 min_z = MYMIN(oldpos_i.Z, newpos_i.Z) + (box_0.MinEdge.Z / BS) - 1;
229 s16 max_x = MYMAX(oldpos_i.X, newpos_i.X) + (box_0.MaxEdge.X / BS) + 1;
230 s16 max_y = MYMAX(oldpos_i.Y, newpos_i.Y) + (box_0.MaxEdge.Y / BS) + 1;
231 s16 max_z = MYMAX(oldpos_i.Z, newpos_i.Z) + (box_0.MaxEdge.Z / BS) + 1;
233 for(s16 x = min_x; x <= max_x; x++)
234 for(s16 y = min_y; y <= max_y; y++)
235 for(s16 z = min_z; z <= max_z; z++)
239 // Object collides into walkable nodes
240 MapNode n = map->getNode(p);
241 const ContentFeatures &f = gamedef->getNodeDefManager()->get(n);
242 if(f.walkable == false)
244 int n_bouncy_value = itemgroup_get(f.groups, "bouncy");
246 std::vector<aabb3f> nodeboxes = n.getNodeBoxes(gamedef->ndef());
247 for(std::vector<aabb3f>::iterator
248 i = nodeboxes.begin();
249 i != nodeboxes.end(); i++)
252 box.MinEdge += v3f(x, y, z)*BS;
253 box.MaxEdge += v3f(x, y, z)*BS;
254 cboxes.push_back(box);
255 is_unloaded.push_back(false);
256 is_step_up.push_back(false);
257 bouncy_values.push_back(n_bouncy_value);
258 node_positions.push_back(p);
261 catch(InvalidPositionException &e)
263 // Collide with unloaded nodes
264 aabb3f box = getNodeBox(p, BS);
265 cboxes.push_back(box);
266 is_unloaded.push_back(true);
267 is_step_up.push_back(false);
268 bouncy_values.push_back(0);
269 node_positions.push_back(p);
274 assert(cboxes.size() == is_unloaded.size());
275 assert(cboxes.size() == is_step_up.size());
276 assert(cboxes.size() == bouncy_values.size());
277 assert(cboxes.size() == node_positions.size());
284 Collision uncertainty radius
285 Make it a bit larger than the maximum distance of movement
287 f32 d = pos_max_d * 1.1;
288 // A fairly large value in here makes moving smoother
291 // This should always apply, otherwise there are glitches
292 assert(d > pos_max_d);
296 while(dtime > BS*1e-10)
298 //TimeTaker tt3("collisionMoveSimple dtime loop");
299 ScopeProfiler sp(g_profiler, "collisionMoveSimple dtime loop avg", SPT_AVG);
301 // Avoid infinite loop
305 infostream<<"collisionMoveSimple: WARNING: Loop count exceeded, aborting to avoid infiniite loop"<<std::endl;
310 aabb3f movingbox = box_0;
311 movingbox.MinEdge += pos_f;
312 movingbox.MaxEdge += pos_f;
314 int nearest_collided = -1;
315 f32 nearest_dtime = dtime;
316 u32 nearest_boxindex = -1;
319 Go through every nodebox, find nearest collision
321 for(u32 boxindex = 0; boxindex < cboxes.size(); boxindex++)
323 // Ignore if already stepped up this nodebox.
324 if(is_step_up[boxindex])
327 // Find nearest collision of the two boxes (raytracing-like)
329 int collided = axisAlignedCollision(
330 cboxes[boxindex], movingbox, speed_f, d, dtime_tmp);
332 if(collided == -1 || dtime_tmp >= nearest_dtime)
335 nearest_dtime = dtime_tmp;
336 nearest_collided = collided;
337 nearest_boxindex = boxindex;
340 if(nearest_collided == -1)
342 // No collision with any collision box.
343 pos_f += speed_f * dtime;
344 dtime = 0; // Set to 0 to avoid "infinite" loop due to small FP numbers
348 // Otherwise, a collision occurred.
350 const aabb3f& cbox = cboxes[nearest_boxindex];
353 bool step_up = (nearest_collided != 1) && // must not be Y direction
354 (movingbox.MinEdge.Y < cbox.MaxEdge.Y) &&
355 (movingbox.MinEdge.Y + stepheight > cbox.MaxEdge.Y) &&
356 (!wouldCollideWithCeiling(cboxes, movingbox,
357 cbox.MaxEdge.Y - movingbox.MinEdge.Y,
360 // Get bounce multiplier
361 bool bouncy = (bouncy_values[nearest_boxindex] >= 1);
362 float bounce = -(float)bouncy_values[nearest_boxindex] / 100.0;
364 // Move to the point of collision and reduce dtime by nearest_dtime
365 if(nearest_dtime < 0)
367 // Handle negative nearest_dtime (can be caused by the d allowance)
370 if(nearest_collided == 0)
371 pos_f.X += speed_f.X * nearest_dtime;
372 if(nearest_collided == 1)
373 pos_f.Y += speed_f.Y * nearest_dtime;
374 if(nearest_collided == 2)
375 pos_f.Z += speed_f.Z * nearest_dtime;
380 pos_f += speed_f * nearest_dtime;
381 dtime -= nearest_dtime;
384 bool is_collision = true;
385 if(is_unloaded[nearest_boxindex])
386 is_collision = false;
389 info.type = COLLISION_NODE;
390 info.node_p = node_positions[nearest_boxindex];
391 info.bouncy = bouncy;
392 info.old_speed = speed_f;
394 // Set the speed component that caused the collision to zero
397 // Special case: Handle stairs
398 is_step_up[nearest_boxindex] = true;
399 is_collision = false;
401 else if(nearest_collided == 0) // X
403 if(fabs(speed_f.X) > BS*3)
407 result.collides = true;
408 result.collides_xz = true;
410 else if(nearest_collided == 1) // Y
412 if(fabs(speed_f.Y) > BS*3)
416 result.collides = true;
418 else if(nearest_collided == 2) // Z
420 if(fabs(speed_f.Z) > BS*3)
424 result.collides = true;
425 result.collides_xz = true;
428 info.new_speed = speed_f;
429 if(info.new_speed.getDistanceFrom(info.old_speed) < 0.1*BS)
430 is_collision = false;
433 result.collisions.push_back(info);
439 Final touches: Check if standing on ground, step up stairs.
442 box.MinEdge += pos_f;
443 box.MaxEdge += pos_f;
444 for(u32 boxindex = 0; boxindex < cboxes.size(); boxindex++)
446 const aabb3f& cbox = cboxes[boxindex];
449 See if the object is touching ground.
451 Object touches ground if object's minimum Y is near node's
452 maximum Y and object's X-Z-area overlaps with the node's
455 Use 0.15*BS so that it is easier to get on a node.
458 cbox.MaxEdge.X-d > box.MinEdge.X &&
459 cbox.MinEdge.X+d < box.MaxEdge.X &&
460 cbox.MaxEdge.Z-d > box.MinEdge.Z &&
461 cbox.MinEdge.Z+d < box.MaxEdge.Z
463 if(is_step_up[boxindex])
465 pos_f.Y += (cbox.MaxEdge.Y - box.MinEdge.Y);
467 box.MinEdge += pos_f;
468 box.MaxEdge += pos_f;
470 if(fabs(cbox.MaxEdge.Y-box.MinEdge.Y) < 0.15*BS)
472 result.touching_ground = true;
473 if(is_unloaded[boxindex])
474 result.standing_on_unloaded = true;
483 // This doesn't seem to work and isn't used
484 collisionMoveResult collisionMovePrecise(Map *map, IGameDef *gamedef,
485 f32 pos_max_d, const aabb3f &box_0,
486 f32 stepheight, f32 dtime,
487 v3f &pos_f, v3f &speed_f, v3f &accel_f)
489 //TimeTaker tt("collisionMovePrecise");
490 ScopeProfiler sp(g_profiler, "collisionMovePrecise avg", SPT_AVG);
492 collisionMoveResult final_result;
494 // If there is no speed, there are no collisions
495 if(speed_f.getLength() == 0)
498 // Don't allow overly huge dtime
502 f32 dtime_downcount = dtime;
509 // Maximum time increment (for collision detection etc)
510 // time = distance / speed
511 f32 dtime_max_increment = 1.0;
512 if(speed_f.getLength() != 0)
513 dtime_max_increment = pos_max_d / speed_f.getLength();
515 // Maximum time increment is 10ms or lower
516 if(dtime_max_increment > 0.01)
517 dtime_max_increment = 0.01;
520 if(dtime_downcount > dtime_max_increment)
522 dtime_part = dtime_max_increment;
523 dtime_downcount -= dtime_part;
527 dtime_part = dtime_downcount;
529 Setting this to 0 (no -=dtime_part) disables an infinite loop
530 when dtime_part is so small that dtime_downcount -= dtime_part
536 collisionMoveResult result = collisionMoveSimple(map, gamedef,
537 pos_max_d, box_0, stepheight, dtime_part,
538 pos_f, speed_f, accel_f);
540 if(result.touching_ground)
541 final_result.touching_ground = true;
543 final_result.collides = true;
544 if(result.collides_xz)
545 final_result.collides_xz = true;
546 if(result.standing_on_unloaded)
547 final_result.standing_on_unloaded = true;
549 while(dtime_downcount > 0.001);