collision.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "collision.h"
  17. #include "mapblock.h"
  18. #include "map.h"
  19. #include "nodedef.h"
  20. #include "gamedef.h"
  21. #include "log.h"
  22. #include "environment.h"
  23. #include "serverobject.h"
  24. #include <vector>
  25. #include <set>
  26. #include "util/timetaker.h"
  27. #include "main.h" // g_profiler
  28. #include "profiler.h"
  29. // float error is 10 - 9.96875 = 0.03125
  30. //#define COLL_ZERO 0.032 // broken unit tests
  31. #define COLL_ZERO 0
  32. // Helper function:
  33. // Checks for collision of a moving aabbox with a static aabbox
  34. // Returns -1 if no collision, 0 if X collision, 1 if Y collision, 2 if Z collision
  35. // The time after which the collision occurs is stored in dtime.
  36. int axisAlignedCollision(
  37. const aabb3f &staticbox, const aabb3f &movingbox,
  38. const v3f &speed, f32 d, f32 &dtime)
  39. {
  40. //TimeTaker tt("axisAlignedCollision");
  41. f32 xsize = (staticbox.MaxEdge.X - staticbox.MinEdge.X) - COLL_ZERO; // reduce box size for solve collision stuck (flying sand)
  42. f32 ysize = (staticbox.MaxEdge.Y - staticbox.MinEdge.Y); // - COLL_ZERO; // Y - no sense for falling, but maybe try later
  43. f32 zsize = (staticbox.MaxEdge.Z - staticbox.MinEdge.Z) - COLL_ZERO;
  44. aabb3f relbox(
  45. movingbox.MinEdge.X - staticbox.MinEdge.X,
  46. movingbox.MinEdge.Y - staticbox.MinEdge.Y,
  47. movingbox.MinEdge.Z - staticbox.MinEdge.Z,
  48. movingbox.MaxEdge.X - staticbox.MinEdge.X,
  49. movingbox.MaxEdge.Y - staticbox.MinEdge.Y,
  50. movingbox.MaxEdge.Z - staticbox.MinEdge.Z
  51. );
  52. if(speed.X > 0) // Check for collision with X- plane
  53. {
  54. if(relbox.MaxEdge.X <= d)
  55. {
  56. dtime = - relbox.MaxEdge.X / speed.X;
  57. if((relbox.MinEdge.Y + speed.Y * dtime < ysize) &&
  58. (relbox.MaxEdge.Y + speed.Y * dtime > COLL_ZERO) &&
  59. (relbox.MinEdge.Z + speed.Z * dtime < zsize) &&
  60. (relbox.MaxEdge.Z + speed.Z * dtime > COLL_ZERO))
  61. return 0;
  62. }
  63. else if(relbox.MinEdge.X > xsize)
  64. {
  65. return -1;
  66. }
  67. }
  68. else if(speed.X < 0) // Check for collision with X+ plane
  69. {
  70. if(relbox.MinEdge.X >= xsize - d)
  71. {
  72. dtime = (xsize - relbox.MinEdge.X) / speed.X;
  73. if((relbox.MinEdge.Y + speed.Y * dtime < ysize) &&
  74. (relbox.MaxEdge.Y + speed.Y * dtime > COLL_ZERO) &&
  75. (relbox.MinEdge.Z + speed.Z * dtime < zsize) &&
  76. (relbox.MaxEdge.Z + speed.Z * dtime > COLL_ZERO))
  77. return 0;
  78. }
  79. else if(relbox.MaxEdge.X < 0)
  80. {
  81. return -1;
  82. }
  83. }
  84. // NO else if here
  85. if(speed.Y > 0) // Check for collision with Y- plane
  86. {
  87. if(relbox.MaxEdge.Y <= d)
  88. {
  89. dtime = - relbox.MaxEdge.Y / speed.Y;
  90. if((relbox.MinEdge.X + speed.X * dtime < xsize) &&
  91. (relbox.MaxEdge.X + speed.X * dtime > COLL_ZERO) &&
  92. (relbox.MinEdge.Z + speed.Z * dtime < zsize) &&
  93. (relbox.MaxEdge.Z + speed.Z * dtime > COLL_ZERO))
  94. return 1;
  95. }
  96. else if(relbox.MinEdge.Y > ysize)
  97. {
  98. return -1;
  99. }
  100. }
  101. else if(speed.Y < 0) // Check for collision with Y+ plane
  102. {
  103. if(relbox.MinEdge.Y >= ysize - d)
  104. {
  105. dtime = (ysize - relbox.MinEdge.Y) / speed.Y;
  106. if((relbox.MinEdge.X + speed.X * dtime < xsize) &&
  107. (relbox.MaxEdge.X + speed.X * dtime > COLL_ZERO) &&
  108. (relbox.MinEdge.Z + speed.Z * dtime < zsize) &&
  109. (relbox.MaxEdge.Z + speed.Z * dtime > COLL_ZERO))
  110. return 1;
  111. }
  112. else if(relbox.MaxEdge.Y < 0)
  113. {
  114. return -1;
  115. }
  116. }
  117. // NO else if here
  118. if(speed.Z > 0) // Check for collision with Z- plane
  119. {
  120. if(relbox.MaxEdge.Z <= d)
  121. {
  122. dtime = - relbox.MaxEdge.Z / speed.Z;
  123. if((relbox.MinEdge.X + speed.X * dtime < xsize) &&
  124. (relbox.MaxEdge.X + speed.X * dtime > COLL_ZERO) &&
  125. (relbox.MinEdge.Y + speed.Y * dtime < ysize) &&
  126. (relbox.MaxEdge.Y + speed.Y * dtime > COLL_ZERO))
  127. return 2;
  128. }
  129. //else if(relbox.MinEdge.Z > zsize)
  130. //{
  131. // return -1;
  132. //}
  133. }
  134. else if(speed.Z < 0) // Check for collision with Z+ plane
  135. {
  136. if(relbox.MinEdge.Z >= zsize - d)
  137. {
  138. dtime = (zsize - relbox.MinEdge.Z) / speed.Z;
  139. if((relbox.MinEdge.X + speed.X * dtime < xsize) &&
  140. (relbox.MaxEdge.X + speed.X * dtime > COLL_ZERO) &&
  141. (relbox.MinEdge.Y + speed.Y * dtime < ysize) &&
  142. (relbox.MaxEdge.Y + speed.Y * dtime > COLL_ZERO))
  143. return 2;
  144. }
  145. //else if(relbox.MaxEdge.Z < 0)
  146. //{
  147. // return -1;
  148. //}
  149. }
  150. return -1;
  151. }
  152. // Helper function:
  153. // Checks if moving the movingbox up by the given distance would hit a ceiling.
  154. bool wouldCollideWithCeiling(
  155. const std::vector<aabb3f> &staticboxes,
  156. const aabb3f &movingbox,
  157. f32 y_increase, f32 d)
  158. {
  159. //TimeTaker tt("wouldCollideWithCeiling");
  160. assert(y_increase >= 0);
  161. for(std::vector<aabb3f>::const_iterator
  162. i = staticboxes.begin();
  163. i != staticboxes.end(); i++)
  164. {
  165. const aabb3f& staticbox = *i;
  166. if((movingbox.MaxEdge.Y - d <= staticbox.MinEdge.Y) &&
  167. (movingbox.MaxEdge.Y + y_increase > staticbox.MinEdge.Y) &&
  168. (movingbox.MinEdge.X < staticbox.MaxEdge.X) &&
  169. (movingbox.MaxEdge.X > staticbox.MinEdge.X) &&
  170. (movingbox.MinEdge.Z < staticbox.MaxEdge.Z) &&
  171. (movingbox.MaxEdge.Z > staticbox.MinEdge.Z))
  172. return true;
  173. }
  174. return false;
  175. }
  176. collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
  177. f32 pos_max_d, const aabb3f &box_0,
  178. f32 stepheight, f32 dtime,
  179. v3f &pos_f, v3f &speed_f,
  180. v3f &accel_f,ActiveObject* self,
  181. bool collideWithObjects)
  182. {
  183. Map *map = &env->getMap();
  184. //TimeTaker tt("collisionMoveSimple");
  185. ScopeProfiler sp(g_profiler, "collisionMoveSimple avg", SPT_AVG);
  186. collisionMoveResult result;
  187. /*
  188. Calculate new velocity
  189. */
  190. if( dtime > 0.5 ) {
  191. infostream<<"collisionMoveSimple: WARNING: maximum step interval exceeded, lost movement details!"<<std::endl;
  192. dtime = 0.5;
  193. }
  194. speed_f += accel_f * dtime;
  195. // If there is no speed, there are no collisions
  196. if(speed_f.getLength() == 0)
  197. return result;
  198. // Limit speed for avoiding hangs
  199. speed_f.Y=rangelim(speed_f.Y,-5000,5000);
  200. speed_f.X=rangelim(speed_f.X,-5000,5000);
  201. speed_f.Z=rangelim(speed_f.Z,-5000,5000);
  202. /*
  203. Collect node boxes in movement range
  204. */
  205. std::vector<aabb3f> cboxes;
  206. std::vector<bool> is_unloaded;
  207. std::vector<bool> is_step_up;
  208. std::vector<bool> is_object;
  209. std::vector<int> bouncy_values;
  210. std::vector<v3s16> node_positions;
  211. {
  212. //TimeTaker tt2("collisionMoveSimple collect boxes");
  213. ScopeProfiler sp(g_profiler, "collisionMoveSimple collect boxes avg", SPT_AVG);
  214. v3s16 oldpos_i = floatToInt(pos_f, BS);
  215. v3s16 newpos_i = floatToInt(pos_f + speed_f * dtime, BS);
  216. s16 min_x = MYMIN(oldpos_i.X, newpos_i.X) + (box_0.MinEdge.X / BS) - 1;
  217. s16 min_y = MYMIN(oldpos_i.Y, newpos_i.Y) + (box_0.MinEdge.Y / BS) - 1;
  218. s16 min_z = MYMIN(oldpos_i.Z, newpos_i.Z) + (box_0.MinEdge.Z / BS) - 1;
  219. s16 max_x = MYMAX(oldpos_i.X, newpos_i.X) + (box_0.MaxEdge.X / BS) + 1;
  220. s16 max_y = MYMAX(oldpos_i.Y, newpos_i.Y) + (box_0.MaxEdge.Y / BS) + 1;
  221. s16 max_z = MYMAX(oldpos_i.Z, newpos_i.Z) + (box_0.MaxEdge.Z / BS) + 1;
  222. for(s16 x = min_x; x <= max_x; x++)
  223. for(s16 y = min_y; y <= max_y; y++)
  224. for(s16 z = min_z; z <= max_z; z++)
  225. {
  226. v3s16 p(x,y,z);
  227. try{
  228. // Object collides into walkable nodes
  229. MapNode n = map->getNode(p);
  230. const ContentFeatures &f = gamedef->getNodeDefManager()->get(n);
  231. if(f.walkable == false)
  232. continue;
  233. int n_bouncy_value = itemgroup_get(f.groups, "bouncy");
  234. std::vector<aabb3f> nodeboxes = n.getNodeBoxes(gamedef->ndef());
  235. for(std::vector<aabb3f>::iterator
  236. i = nodeboxes.begin();
  237. i != nodeboxes.end(); i++)
  238. {
  239. aabb3f box = *i;
  240. box.MinEdge += v3f(x, y, z)*BS;
  241. box.MaxEdge += v3f(x, y, z)*BS;
  242. cboxes.push_back(box);
  243. is_unloaded.push_back(false);
  244. is_step_up.push_back(false);
  245. bouncy_values.push_back(n_bouncy_value);
  246. node_positions.push_back(p);
  247. is_object.push_back(false);
  248. }
  249. }
  250. catch(InvalidPositionException &e)
  251. {
  252. // Collide with unloaded nodes
  253. aabb3f box = getNodeBox(p, BS);
  254. cboxes.push_back(box);
  255. is_unloaded.push_back(true);
  256. is_step_up.push_back(false);
  257. bouncy_values.push_back(0);
  258. node_positions.push_back(p);
  259. is_object.push_back(false);
  260. }
  261. }
  262. } // tt2
  263. if(collideWithObjects)
  264. {
  265. ScopeProfiler sp(g_profiler, "collisionMoveSimple objects avg", SPT_AVG);
  266. //TimeTaker tt3("collisionMoveSimple collect object boxes");
  267. /* add object boxes to cboxes */
  268. std::list<ActiveObject*> objects;
  269. #ifndef SERVER
  270. ClientEnvironment *c_env = dynamic_cast<ClientEnvironment*>(env);
  271. if (c_env != 0)
  272. {
  273. f32 distance = speed_f.getLength();
  274. std::vector<DistanceSortedActiveObject> clientobjects;
  275. c_env->getActiveObjects(pos_f,distance * 1.5,clientobjects);
  276. for (size_t i=0; i < clientobjects.size(); i++)
  277. {
  278. if ((self == 0) || (self != clientobjects[i].obj)) {
  279. objects.push_back((ActiveObject*)clientobjects[i].obj);
  280. }
  281. }
  282. }
  283. else
  284. #endif
  285. {
  286. ServerEnvironment *s_env = dynamic_cast<ServerEnvironment*>(env);
  287. if (s_env != 0)
  288. {
  289. f32 distance = speed_f.getLength();
  290. std::set<u16> s_objects = s_env->getObjectsInsideRadius(pos_f,distance * 1.5);
  291. for (std::set<u16>::iterator iter = s_objects.begin(); iter != s_objects.end(); iter++)
  292. {
  293. ServerActiveObject *current = s_env->getActiveObject(*iter);
  294. if ((self == 0) || (self != current)) {
  295. objects.push_back((ActiveObject*)current);
  296. }
  297. }
  298. }
  299. }
  300. for (std::list<ActiveObject*>::const_iterator iter = objects.begin();iter != objects.end(); ++iter)
  301. {
  302. ActiveObject *object = *iter;
  303. if (object != NULL)
  304. {
  305. aabb3f object_collisionbox;
  306. if (object->getCollisionBox(&object_collisionbox) &&
  307. object->collideWithObjects())
  308. {
  309. cboxes.push_back(object_collisionbox);
  310. is_unloaded.push_back(false);
  311. is_step_up.push_back(false);
  312. bouncy_values.push_back(0);
  313. node_positions.push_back(v3s16(0,0,0));
  314. is_object.push_back(true);
  315. }
  316. }
  317. }
  318. } //tt3
  319. assert(cboxes.size() == is_unloaded.size());
  320. assert(cboxes.size() == is_step_up.size());
  321. assert(cboxes.size() == bouncy_values.size());
  322. assert(cboxes.size() == node_positions.size());
  323. assert(cboxes.size() == is_object.size());
  324. /*
  325. Collision detection
  326. */
  327. /*
  328. Collision uncertainty radius
  329. Make it a bit larger than the maximum distance of movement
  330. */
  331. f32 d = pos_max_d * 1.1;
  332. // A fairly large value in here makes moving smoother
  333. //f32 d = 0.15*BS;
  334. // This should always apply, otherwise there are glitches
  335. assert(d > pos_max_d);
  336. int loopcount = 0;
  337. while(dtime > BS*1e-10)
  338. {
  339. //TimeTaker tt3("collisionMoveSimple dtime loop");
  340. ScopeProfiler sp(g_profiler, "collisionMoveSimple dtime loop avg", SPT_AVG);
  341. // Avoid infinite loop
  342. loopcount++;
  343. if(loopcount >= 100)
  344. {
  345. infostream<<"collisionMoveSimple: WARNING: Loop count exceeded, aborting to avoid infiniite loop"<<std::endl;
  346. dtime = 0;
  347. break;
  348. }
  349. aabb3f movingbox = box_0;
  350. movingbox.MinEdge += pos_f;
  351. movingbox.MaxEdge += pos_f;
  352. int nearest_collided = -1;
  353. f32 nearest_dtime = dtime;
  354. u32 nearest_boxindex = -1;
  355. /*
  356. Go through every nodebox, find nearest collision
  357. */
  358. for(u32 boxindex = 0; boxindex < cboxes.size(); boxindex++)
  359. {
  360. // Ignore if already stepped up this nodebox.
  361. if(is_step_up[boxindex])
  362. continue;
  363. // Find nearest collision of the two boxes (raytracing-like)
  364. f32 dtime_tmp;
  365. int collided = axisAlignedCollision(
  366. cboxes[boxindex], movingbox, speed_f, d, dtime_tmp);
  367. if(collided == -1 || dtime_tmp >= nearest_dtime)
  368. continue;
  369. nearest_dtime = dtime_tmp;
  370. nearest_collided = collided;
  371. nearest_boxindex = boxindex;
  372. }
  373. if(nearest_collided == -1)
  374. {
  375. // No collision with any collision box.
  376. pos_f += speed_f * dtime;
  377. dtime = 0; // Set to 0 to avoid "infinite" loop due to small FP numbers
  378. }
  379. else
  380. {
  381. // Otherwise, a collision occurred.
  382. const aabb3f& cbox = cboxes[nearest_boxindex];
  383. // Check for stairs.
  384. bool step_up = (nearest_collided != 1) && // must not be Y direction
  385. (movingbox.MinEdge.Y < cbox.MaxEdge.Y) &&
  386. (movingbox.MinEdge.Y + stepheight > cbox.MaxEdge.Y) &&
  387. (!wouldCollideWithCeiling(cboxes, movingbox,
  388. cbox.MaxEdge.Y - movingbox.MinEdge.Y,
  389. d));
  390. // Get bounce multiplier
  391. bool bouncy = (bouncy_values[nearest_boxindex] >= 1);
  392. float bounce = -(float)bouncy_values[nearest_boxindex] / 100.0;
  393. // Move to the point of collision and reduce dtime by nearest_dtime
  394. if(nearest_dtime < 0)
  395. {
  396. // Handle negative nearest_dtime (can be caused by the d allowance)
  397. if(!step_up)
  398. {
  399. if(nearest_collided == 0)
  400. pos_f.X += speed_f.X * nearest_dtime;
  401. if(nearest_collided == 1)
  402. pos_f.Y += speed_f.Y * nearest_dtime;
  403. if(nearest_collided == 2)
  404. pos_f.Z += speed_f.Z * nearest_dtime;
  405. }
  406. }
  407. else
  408. {
  409. pos_f += speed_f * nearest_dtime;
  410. dtime -= nearest_dtime;
  411. }
  412. bool is_collision = true;
  413. if(is_unloaded[nearest_boxindex])
  414. is_collision = false;
  415. CollisionInfo info;
  416. if (is_object[nearest_boxindex]) {
  417. info.type = COLLISION_OBJECT;
  418. }
  419. else {
  420. info.type = COLLISION_NODE;
  421. }
  422. info.node_p = node_positions[nearest_boxindex];
  423. info.bouncy = bouncy;
  424. info.old_speed = speed_f;
  425. // Set the speed component that caused the collision to zero
  426. if(step_up)
  427. {
  428. // Special case: Handle stairs
  429. is_step_up[nearest_boxindex] = true;
  430. is_collision = false;
  431. }
  432. else if(nearest_collided == 0) // X
  433. {
  434. if(fabs(speed_f.X) > BS*3)
  435. speed_f.X *= bounce;
  436. else
  437. speed_f.X = 0;
  438. result.collides = true;
  439. result.collides_xz = true;
  440. }
  441. else if(nearest_collided == 1) // Y
  442. {
  443. if(fabs(speed_f.Y) > BS*3)
  444. speed_f.Y *= bounce;
  445. else
  446. speed_f.Y = 0;
  447. result.collides = true;
  448. }
  449. else if(nearest_collided == 2) // Z
  450. {
  451. if(fabs(speed_f.Z) > BS*3)
  452. speed_f.Z *= bounce;
  453. else
  454. speed_f.Z = 0;
  455. result.collides = true;
  456. result.collides_xz = true;
  457. }
  458. info.new_speed = speed_f;
  459. if(info.new_speed.getDistanceFrom(info.old_speed) < 0.1*BS)
  460. is_collision = false;
  461. if(is_collision){
  462. result.collisions.push_back(info);
  463. }
  464. }
  465. }
  466. /*
  467. Final touches: Check if standing on ground, step up stairs.
  468. */
  469. aabb3f box = box_0;
  470. box.MinEdge += pos_f;
  471. box.MaxEdge += pos_f;
  472. for(u32 boxindex = 0; boxindex < cboxes.size(); boxindex++)
  473. {
  474. const aabb3f& cbox = cboxes[boxindex];
  475. /*
  476. See if the object is touching ground.
  477. Object touches ground if object's minimum Y is near node's
  478. maximum Y and object's X-Z-area overlaps with the node's
  479. X-Z-area.
  480. Use 0.15*BS so that it is easier to get on a node.
  481. */
  482. if(
  483. cbox.MaxEdge.X-d > box.MinEdge.X &&
  484. cbox.MinEdge.X+d < box.MaxEdge.X &&
  485. cbox.MaxEdge.Z-d > box.MinEdge.Z &&
  486. cbox.MinEdge.Z+d < box.MaxEdge.Z
  487. ){
  488. if(is_step_up[boxindex])
  489. {
  490. pos_f.Y += (cbox.MaxEdge.Y - box.MinEdge.Y);
  491. box = box_0;
  492. box.MinEdge += pos_f;
  493. box.MaxEdge += pos_f;
  494. }
  495. if(fabs(cbox.MaxEdge.Y-box.MinEdge.Y) < 0.15*BS)
  496. {
  497. result.touching_ground = true;
  498. if(is_unloaded[boxindex])
  499. result.standing_on_unloaded = true;
  500. }
  501. }
  502. }
  503. return result;
  504. }
  505. #if 0
  506. // This doesn't seem to work and isn't used
  507. collisionMoveResult collisionMovePrecise(Map *map, IGameDef *gamedef,
  508. f32 pos_max_d, const aabb3f &box_0,
  509. f32 stepheight, f32 dtime,
  510. v3f &pos_f, v3f &speed_f, v3f &accel_f)
  511. {
  512. //TimeTaker tt("collisionMovePrecise");
  513. ScopeProfiler sp(g_profiler, "collisionMovePrecise avg", SPT_AVG);
  514. collisionMoveResult final_result;
  515. // If there is no speed, there are no collisions
  516. if(speed_f.getLength() == 0)
  517. return final_result;
  518. // Don't allow overly huge dtime
  519. if(dtime > 2.0)
  520. dtime = 2.0;
  521. f32 dtime_downcount = dtime;
  522. u32 loopcount = 0;
  523. do
  524. {
  525. loopcount++;
  526. // Maximum time increment (for collision detection etc)
  527. // time = distance / speed
  528. f32 dtime_max_increment = 1.0;
  529. if(speed_f.getLength() != 0)
  530. dtime_max_increment = pos_max_d / speed_f.getLength();
  531. // Maximum time increment is 10ms or lower
  532. if(dtime_max_increment > 0.01)
  533. dtime_max_increment = 0.01;
  534. f32 dtime_part;
  535. if(dtime_downcount > dtime_max_increment)
  536. {
  537. dtime_part = dtime_max_increment;
  538. dtime_downcount -= dtime_part;
  539. }
  540. else
  541. {
  542. dtime_part = dtime_downcount;
  543. /*
  544. Setting this to 0 (no -=dtime_part) disables an infinite loop
  545. when dtime_part is so small that dtime_downcount -= dtime_part
  546. does nothing
  547. */
  548. dtime_downcount = 0;
  549. }
  550. collisionMoveResult result = collisionMoveSimple(map, gamedef,
  551. pos_max_d, box_0, stepheight, dtime_part,
  552. pos_f, speed_f, accel_f);
  553. if(result.touching_ground)
  554. final_result.touching_ground = true;
  555. if(result.collides)
  556. final_result.collides = true;
  557. if(result.collides_xz)
  558. final_result.collides_xz = true;
  559. if(result.standing_on_unloaded)
  560. final_result.standing_on_unloaded = true;
  561. }
  562. while(dtime_downcount > 0.001);
  563. return final_result;
  564. }
  565. #endif