collision.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 <cmath>
  18. #include "mapblock.h"
  19. #include "map.h"
  20. #include "nodedef.h"
  21. #include "gamedef.h"
  22. #ifndef SERVER
  23. #include "client/clientenvironment.h"
  24. #include "client/localplayer.h"
  25. #endif
  26. #include "serverenvironment.h"
  27. #include "server/serveractiveobject.h"
  28. #include "util/timetaker.h"
  29. #include "profiler.h"
  30. #ifdef __FAST_MATH__
  31. #warning "-ffast-math is known to cause bugs in collision code, do not use!"
  32. #endif
  33. struct NearbyCollisionInfo {
  34. // node
  35. NearbyCollisionInfo(bool is_ul, int bouncy, const v3s16 &pos,
  36. const aabb3f &box) :
  37. is_unloaded(is_ul),
  38. obj(nullptr),
  39. bouncy(bouncy),
  40. position(pos),
  41. box(box)
  42. {}
  43. // object
  44. NearbyCollisionInfo(ActiveObject *obj, int bouncy,
  45. const aabb3f &box) :
  46. is_unloaded(false),
  47. obj(obj),
  48. bouncy(bouncy),
  49. box(box)
  50. {}
  51. inline bool isObject() const { return obj != nullptr; }
  52. bool is_unloaded;
  53. bool is_step_up = false;
  54. ActiveObject *obj;
  55. int bouncy;
  56. v3s16 position;
  57. aabb3f box;
  58. };
  59. // Helper functions:
  60. // Truncate floating point numbers to specified number of decimal places
  61. // in order to move all the floating point error to one side of the correct value
  62. static inline f32 truncate(const f32 val, const f32 factor)
  63. {
  64. return truncf(val * factor) / factor;
  65. }
  66. static inline v3f truncate(const v3f& vec, const f32 factor)
  67. {
  68. return v3f(
  69. truncate(vec.X, factor),
  70. truncate(vec.Y, factor),
  71. truncate(vec.Z, factor)
  72. );
  73. }
  74. // Helper function:
  75. // Checks for collision of a moving aabbox with a static aabbox
  76. // Returns -1 if no collision, 0 if X collision, 1 if Y collision, 2 if Z collision
  77. // The time after which the collision occurs is stored in dtime.
  78. CollisionAxis axisAlignedCollision(
  79. const aabb3f &staticbox, const aabb3f &movingbox,
  80. const v3f speed, f32 *dtime)
  81. {
  82. //TimeTaker tt("axisAlignedCollision");
  83. aabb3f relbox(
  84. (movingbox.MaxEdge.X - movingbox.MinEdge.X) + (staticbox.MaxEdge.X - staticbox.MinEdge.X), // sum of the widths
  85. (movingbox.MaxEdge.Y - movingbox.MinEdge.Y) + (staticbox.MaxEdge.Y - staticbox.MinEdge.Y),
  86. (movingbox.MaxEdge.Z - movingbox.MinEdge.Z) + (staticbox.MaxEdge.Z - staticbox.MinEdge.Z),
  87. std::max(movingbox.MaxEdge.X, staticbox.MaxEdge.X) - std::min(movingbox.MinEdge.X, staticbox.MinEdge.X), //outer bounding 'box' dimensions
  88. std::max(movingbox.MaxEdge.Y, staticbox.MaxEdge.Y) - std::min(movingbox.MinEdge.Y, staticbox.MinEdge.Y),
  89. std::max(movingbox.MaxEdge.Z, staticbox.MaxEdge.Z) - std::min(movingbox.MinEdge.Z, staticbox.MinEdge.Z)
  90. );
  91. const f32 dtime_max = *dtime;
  92. f32 inner_margin; // the distance of clipping recovery
  93. f32 distance;
  94. f32 time;
  95. if (speed.Y) {
  96. distance = relbox.MaxEdge.Y - relbox.MinEdge.Y;
  97. *dtime = distance / std::abs(speed.Y);
  98. time = std::max(*dtime, 0.0f);
  99. if (*dtime <= dtime_max) {
  100. inner_margin = std::max(-0.5f * (staticbox.MaxEdge.Y - staticbox.MinEdge.Y), -2.0f);
  101. if ((speed.Y > 0 && staticbox.MinEdge.Y - movingbox.MaxEdge.Y > inner_margin) ||
  102. (speed.Y < 0 && movingbox.MinEdge.Y - staticbox.MaxEdge.Y > inner_margin)) {
  103. if (
  104. (std::max(movingbox.MaxEdge.X + speed.X * time, staticbox.MaxEdge.X)
  105. - std::min(movingbox.MinEdge.X + speed.X * time, staticbox.MinEdge.X)
  106. - relbox.MinEdge.X < 0) &&
  107. (std::max(movingbox.MaxEdge.Z + speed.Z * time, staticbox.MaxEdge.Z)
  108. - std::min(movingbox.MinEdge.Z + speed.Z * time, staticbox.MinEdge.Z)
  109. - relbox.MinEdge.Z < 0)
  110. )
  111. return COLLISION_AXIS_Y;
  112. }
  113. }
  114. else {
  115. return COLLISION_AXIS_NONE;
  116. }
  117. }
  118. // NO else if here
  119. if (speed.X) {
  120. distance = relbox.MaxEdge.X - relbox.MinEdge.X;
  121. *dtime = distance / std::abs(speed.X);
  122. time = std::max(*dtime, 0.0f);
  123. if (*dtime <= dtime_max) {
  124. inner_margin = std::max(-0.5f * (staticbox.MaxEdge.X - staticbox.MinEdge.X), -2.0f);
  125. if ((speed.X > 0 && staticbox.MinEdge.X - movingbox.MaxEdge.X > inner_margin) ||
  126. (speed.X < 0 && movingbox.MinEdge.X - staticbox.MaxEdge.X > inner_margin)) {
  127. if (
  128. (std::max(movingbox.MaxEdge.Y + speed.Y * time, staticbox.MaxEdge.Y)
  129. - std::min(movingbox.MinEdge.Y + speed.Y * time, staticbox.MinEdge.Y)
  130. - relbox.MinEdge.Y < 0) &&
  131. (std::max(movingbox.MaxEdge.Z + speed.Z * time, staticbox.MaxEdge.Z)
  132. - std::min(movingbox.MinEdge.Z + speed.Z * time, staticbox.MinEdge.Z)
  133. - relbox.MinEdge.Z < 0)
  134. )
  135. return COLLISION_AXIS_X;
  136. }
  137. } else {
  138. return COLLISION_AXIS_NONE;
  139. }
  140. }
  141. // NO else if here
  142. if (speed.Z) {
  143. distance = relbox.MaxEdge.Z - relbox.MinEdge.Z;
  144. *dtime = distance / std::abs(speed.Z);
  145. time = std::max(*dtime, 0.0f);
  146. if (*dtime <= dtime_max) {
  147. inner_margin = std::max(-0.5f * (staticbox.MaxEdge.Z - staticbox.MinEdge.Z), -2.0f);
  148. if ((speed.Z > 0 && staticbox.MinEdge.Z - movingbox.MaxEdge.Z > inner_margin) ||
  149. (speed.Z < 0 && movingbox.MinEdge.Z - staticbox.MaxEdge.Z > inner_margin)) {
  150. if (
  151. (std::max(movingbox.MaxEdge.X + speed.X * time, staticbox.MaxEdge.X)
  152. - std::min(movingbox.MinEdge.X + speed.X * time, staticbox.MinEdge.X)
  153. - relbox.MinEdge.X < 0) &&
  154. (std::max(movingbox.MaxEdge.Y + speed.Y * time, staticbox.MaxEdge.Y)
  155. - std::min(movingbox.MinEdge.Y + speed.Y * time, staticbox.MinEdge.Y)
  156. - relbox.MinEdge.Y < 0)
  157. )
  158. return COLLISION_AXIS_Z;
  159. }
  160. }
  161. }
  162. return COLLISION_AXIS_NONE;
  163. }
  164. // Helper function:
  165. // Checks if moving the movingbox up by the given distance would hit a ceiling.
  166. bool wouldCollideWithCeiling(
  167. const std::vector<NearbyCollisionInfo> &cinfo,
  168. const aabb3f &movingbox,
  169. f32 y_increase, f32 d)
  170. {
  171. //TimeTaker tt("wouldCollideWithCeiling");
  172. assert(y_increase >= 0); // pre-condition
  173. for (const auto &it : cinfo) {
  174. const aabb3f &staticbox = it.box;
  175. if ((movingbox.MaxEdge.Y - d <= staticbox.MinEdge.Y) &&
  176. (movingbox.MaxEdge.Y + y_increase > staticbox.MinEdge.Y) &&
  177. (movingbox.MinEdge.X < staticbox.MaxEdge.X) &&
  178. (movingbox.MaxEdge.X > staticbox.MinEdge.X) &&
  179. (movingbox.MinEdge.Z < staticbox.MaxEdge.Z) &&
  180. (movingbox.MaxEdge.Z > staticbox.MinEdge.Z))
  181. return true;
  182. }
  183. return false;
  184. }
  185. static inline void getNeighborConnectingFace(const v3s16 &p,
  186. const NodeDefManager *nodedef, Map *map, MapNode n, int v, int *neighbors)
  187. {
  188. MapNode n2 = map->getNode(p);
  189. if (nodedef->nodeboxConnects(n, n2, v))
  190. *neighbors |= v;
  191. }
  192. collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
  193. f32 pos_max_d, const aabb3f &box_0,
  194. f32 stepheight, f32 dtime,
  195. v3f *pos_f, v3f *speed_f,
  196. v3f accel_f, ActiveObject *self,
  197. bool collideWithObjects)
  198. {
  199. #define PROFILER_NAME(text) (s_env ? ("Server: " text) : ("Client: " text))
  200. static bool time_notification_done = false;
  201. Map *map = &env->getMap();
  202. ServerEnvironment *s_env = dynamic_cast<ServerEnvironment*>(env);
  203. ScopeProfiler sp(g_profiler, PROFILER_NAME("collisionMoveSimple()"), SPT_AVG, PRECISION_MICRO);
  204. collisionMoveResult result;
  205. /*
  206. Calculate new velocity
  207. */
  208. if (dtime > DTIME_LIMIT) {
  209. if (!time_notification_done) {
  210. time_notification_done = true;
  211. warningstream << "collisionMoveSimple: maximum step interval exceeded,"
  212. " lost movement details!"<<std::endl;
  213. }
  214. dtime = DTIME_LIMIT;
  215. } else {
  216. time_notification_done = false;
  217. }
  218. v3f dpos_f = (*speed_f + accel_f * 0.5f * dtime) * dtime;
  219. v3f newpos_f = *pos_f + dpos_f;
  220. *speed_f += accel_f * dtime;
  221. // If the object is static, there are no collisions
  222. if (dpos_f == v3f())
  223. return result;
  224. // Limit speed for avoiding hangs
  225. speed_f->Y = rangelim(speed_f->Y, -5000, 5000);
  226. speed_f->X = rangelim(speed_f->X, -5000, 5000);
  227. speed_f->Z = rangelim(speed_f->Z, -5000, 5000);
  228. *speed_f = truncate(*speed_f, 10000.0f);
  229. /*
  230. Collect node boxes in movement range
  231. */
  232. std::vector<NearbyCollisionInfo> cinfo;
  233. {
  234. //TimeTaker tt2("collisionMoveSimple collect boxes");
  235. ScopeProfiler sp2(g_profiler, PROFILER_NAME("collision collect boxes"), SPT_AVG, PRECISION_MICRO);
  236. v3f minpos_f(
  237. MYMIN(pos_f->X, newpos_f.X),
  238. MYMIN(pos_f->Y, newpos_f.Y) + 0.01f * BS, // bias rounding, player often at +/-n.5
  239. MYMIN(pos_f->Z, newpos_f.Z)
  240. );
  241. v3f maxpos_f(
  242. MYMAX(pos_f->X, newpos_f.X),
  243. MYMAX(pos_f->Y, newpos_f.Y),
  244. MYMAX(pos_f->Z, newpos_f.Z)
  245. );
  246. v3s16 min = floatToInt(minpos_f + box_0.MinEdge, BS) - v3s16(1, 1, 1);
  247. v3s16 max = floatToInt(maxpos_f + box_0.MaxEdge, BS) + v3s16(1, 1, 1);
  248. bool any_position_valid = false;
  249. v3s16 p;
  250. for (p.X = min.X; p.X <= max.X; p.X++)
  251. for (p.Y = min.Y; p.Y <= max.Y; p.Y++)
  252. for (p.Z = min.Z; p.Z <= max.Z; p.Z++) {
  253. bool is_position_valid;
  254. MapNode n = map->getNode(p, &is_position_valid);
  255. if (is_position_valid && n.getContent() != CONTENT_IGNORE) {
  256. // Object collides into walkable nodes
  257. any_position_valid = true;
  258. const NodeDefManager *nodedef = gamedef->getNodeDefManager();
  259. const ContentFeatures &f = nodedef->get(n);
  260. if (!f.walkable)
  261. continue;
  262. // Negative bouncy may have a meaning, but we need +value here.
  263. int n_bouncy_value = abs(itemgroup_get(f.groups, "bouncy"));
  264. int neighbors = 0;
  265. if (f.drawtype == NDT_NODEBOX &&
  266. f.node_box.type == NODEBOX_CONNECTED) {
  267. v3s16 p2 = p;
  268. p2.Y++;
  269. getNeighborConnectingFace(p2, nodedef, map, n, 1, &neighbors);
  270. p2 = p;
  271. p2.Y--;
  272. getNeighborConnectingFace(p2, nodedef, map, n, 2, &neighbors);
  273. p2 = p;
  274. p2.Z--;
  275. getNeighborConnectingFace(p2, nodedef, map, n, 4, &neighbors);
  276. p2 = p;
  277. p2.X--;
  278. getNeighborConnectingFace(p2, nodedef, map, n, 8, &neighbors);
  279. p2 = p;
  280. p2.Z++;
  281. getNeighborConnectingFace(p2, nodedef, map, n, 16, &neighbors);
  282. p2 = p;
  283. p2.X++;
  284. getNeighborConnectingFace(p2, nodedef, map, n, 32, &neighbors);
  285. }
  286. std::vector<aabb3f> nodeboxes;
  287. n.getCollisionBoxes(gamedef->ndef(), &nodeboxes, neighbors);
  288. // Calculate float position only once
  289. v3f posf = intToFloat(p, BS);
  290. for (auto box : nodeboxes) {
  291. box.MinEdge += posf;
  292. box.MaxEdge += posf;
  293. cinfo.emplace_back(false, n_bouncy_value, p, box);
  294. }
  295. } else {
  296. // Collide with unloaded nodes (position invalid) and loaded
  297. // CONTENT_IGNORE nodes (position valid)
  298. aabb3f box = getNodeBox(p, BS);
  299. cinfo.emplace_back(true, 0, p, box);
  300. }
  301. }
  302. // Do not move if world has not loaded yet, since custom node boxes
  303. // are not available for collision detection.
  304. // This also intentionally occurs in the case of the object being positioned
  305. // solely on loaded CONTENT_IGNORE nodes, no matter where they come from.
  306. if (!any_position_valid) {
  307. *speed_f = v3f(0, 0, 0);
  308. return result;
  309. }
  310. } // tt2
  311. if(collideWithObjects)
  312. {
  313. /* add object boxes to cinfo */
  314. std::vector<ActiveObject*> objects;
  315. #ifndef SERVER
  316. ClientEnvironment *c_env = dynamic_cast<ClientEnvironment*>(env);
  317. if (c_env != 0) {
  318. // Calculate distance by speed, add own extent and 1.5m of tolerance
  319. f32 distance = speed_f->getLength() * dtime +
  320. box_0.getExtent().getLength() + 1.5f * BS;
  321. std::vector<DistanceSortedActiveObject> clientobjects;
  322. c_env->getActiveObjects(*pos_f, distance, clientobjects);
  323. for (auto &clientobject : clientobjects) {
  324. // Do collide with everything but itself and the parent CAO
  325. if (!self || (self != clientobject.obj &&
  326. self != clientobject.obj->getParent())) {
  327. objects.push_back((ActiveObject*) clientobject.obj);
  328. }
  329. }
  330. }
  331. else
  332. #endif
  333. {
  334. if (s_env != NULL) {
  335. // Calculate distance by speed, add own extent and 1.5m of tolerance
  336. f32 distance = speed_f->getLength() * dtime +
  337. box_0.getExtent().getLength() + 1.5f * BS;
  338. // search for objects which are not us, or we are not its parent
  339. // we directly use the callback to populate the result to prevent
  340. // a useless result loop here
  341. auto include_obj_cb = [self, &objects] (ServerActiveObject *obj) {
  342. if (!obj->isGone() &&
  343. (!self || (self != obj && self != obj->getParent()))) {
  344. objects.push_back((ActiveObject *)obj);
  345. }
  346. return false;
  347. };
  348. std::vector<ServerActiveObject *> s_objects;
  349. s_env->getObjectsInsideRadius(s_objects, *pos_f, distance, include_obj_cb);
  350. }
  351. }
  352. for (std::vector<ActiveObject*>::const_iterator iter = objects.begin();
  353. iter != objects.end(); ++iter) {
  354. ActiveObject *object = *iter;
  355. if (object && object->collideWithObjects()) {
  356. aabb3f object_collisionbox;
  357. if (object->getCollisionBox(&object_collisionbox))
  358. cinfo.emplace_back(object, 0, object_collisionbox);
  359. }
  360. }
  361. #ifndef SERVER
  362. if (self && c_env) {
  363. LocalPlayer *lplayer = c_env->getLocalPlayer();
  364. if (lplayer->getParent() == nullptr) {
  365. aabb3f lplayer_collisionbox = lplayer->getCollisionbox();
  366. v3f lplayer_pos = lplayer->getPosition();
  367. lplayer_collisionbox.MinEdge += lplayer_pos;
  368. lplayer_collisionbox.MaxEdge += lplayer_pos;
  369. ActiveObject *obj = (ActiveObject*) lplayer->getCAO();
  370. cinfo.emplace_back(obj, 0, lplayer_collisionbox);
  371. }
  372. }
  373. #endif
  374. } //tt3
  375. /*
  376. Collision detection
  377. */
  378. f32 d = 0.0f;
  379. int loopcount = 0;
  380. while(dtime > BS * 1e-10f) {
  381. // Avoid infinite loop
  382. loopcount++;
  383. if (loopcount >= 100) {
  384. warningstream << "collisionMoveSimple: Loop count exceeded, aborting to avoid infiniite loop" << std::endl;
  385. break;
  386. }
  387. aabb3f movingbox = box_0;
  388. movingbox.MinEdge += *pos_f;
  389. movingbox.MaxEdge += *pos_f;
  390. CollisionAxis nearest_collided = COLLISION_AXIS_NONE;
  391. f32 nearest_dtime = dtime;
  392. int nearest_boxindex = -1;
  393. /*
  394. Go through every nodebox, find nearest collision
  395. */
  396. for (u32 boxindex = 0; boxindex < cinfo.size(); boxindex++) {
  397. const NearbyCollisionInfo &box_info = cinfo[boxindex];
  398. // Ignore if already stepped up this nodebox.
  399. if (box_info.is_step_up)
  400. continue;
  401. // Find nearest collision of the two boxes (raytracing-like)
  402. f32 dtime_tmp = nearest_dtime;
  403. CollisionAxis collided = axisAlignedCollision(box_info.box,
  404. movingbox, *speed_f, &dtime_tmp);
  405. if (collided == -1 || dtime_tmp >= nearest_dtime)
  406. continue;
  407. nearest_dtime = dtime_tmp;
  408. nearest_collided = collided;
  409. nearest_boxindex = boxindex;
  410. }
  411. if (nearest_collided == COLLISION_AXIS_NONE) {
  412. // No collision with any collision box.
  413. *pos_f += truncate(*speed_f * dtime, 100.0f);
  414. dtime = 0; // Set to 0 to avoid "infinite" loop due to small FP numbers
  415. } else {
  416. // Otherwise, a collision occurred.
  417. NearbyCollisionInfo &nearest_info = cinfo[nearest_boxindex];
  418. const aabb3f& cbox = nearest_info.box;
  419. //movingbox except moved to the horizontal position it would be after step up
  420. aabb3f stepbox = movingbox;
  421. stepbox.MinEdge.X += speed_f->X * dtime;
  422. stepbox.MinEdge.Z += speed_f->Z * dtime;
  423. stepbox.MaxEdge.X += speed_f->X * dtime;
  424. stepbox.MaxEdge.Z += speed_f->Z * dtime;
  425. // Check for stairs.
  426. bool step_up = (nearest_collided != COLLISION_AXIS_Y) && // must not be Y direction
  427. (movingbox.MinEdge.Y < cbox.MaxEdge.Y) &&
  428. (movingbox.MinEdge.Y + stepheight > cbox.MaxEdge.Y) &&
  429. (!wouldCollideWithCeiling(cinfo, stepbox,
  430. cbox.MaxEdge.Y - movingbox.MinEdge.Y,
  431. d));
  432. // Get bounce multiplier
  433. float bounce = -(float)nearest_info.bouncy / 100.0f;
  434. // Move to the point of collision and reduce dtime by nearest_dtime
  435. if (nearest_dtime < 0) {
  436. // Handle negative nearest_dtime
  437. if (!step_up) {
  438. if (nearest_collided == COLLISION_AXIS_X)
  439. pos_f->X += speed_f->X * nearest_dtime;
  440. if (nearest_collided == COLLISION_AXIS_Y)
  441. pos_f->Y += speed_f->Y * nearest_dtime;
  442. if (nearest_collided == COLLISION_AXIS_Z)
  443. pos_f->Z += speed_f->Z * nearest_dtime;
  444. }
  445. } else {
  446. *pos_f += truncate(*speed_f * nearest_dtime, 100.0f);
  447. dtime -= nearest_dtime;
  448. }
  449. bool is_collision = true;
  450. if (nearest_info.is_unloaded)
  451. is_collision = false;
  452. CollisionInfo info;
  453. if (nearest_info.isObject())
  454. info.type = COLLISION_OBJECT;
  455. else
  456. info.type = COLLISION_NODE;
  457. info.node_p = nearest_info.position;
  458. info.object = nearest_info.obj;
  459. info.old_speed = *speed_f;
  460. info.plane = nearest_collided;
  461. // Set the speed component that caused the collision to zero
  462. if (step_up) {
  463. // Special case: Handle stairs
  464. nearest_info.is_step_up = true;
  465. is_collision = false;
  466. } else if (nearest_collided == COLLISION_AXIS_X) {
  467. if (fabs(speed_f->X) > BS * 3)
  468. speed_f->X *= bounce;
  469. else
  470. speed_f->X = 0;
  471. result.collides = true;
  472. } else if (nearest_collided == COLLISION_AXIS_Y) {
  473. if(fabs(speed_f->Y) > BS * 3)
  474. speed_f->Y *= bounce;
  475. else
  476. speed_f->Y = 0;
  477. result.collides = true;
  478. } else if (nearest_collided == COLLISION_AXIS_Z) {
  479. if (fabs(speed_f->Z) > BS * 3)
  480. speed_f->Z *= bounce;
  481. else
  482. speed_f->Z = 0;
  483. result.collides = true;
  484. }
  485. info.new_speed = *speed_f;
  486. if (info.new_speed.getDistanceFrom(info.old_speed) < 0.1f * BS)
  487. is_collision = false;
  488. if (is_collision) {
  489. info.axis = nearest_collided;
  490. result.collisions.push_back(info);
  491. }
  492. }
  493. }
  494. /*
  495. Final touches: Check if standing on ground, step up stairs.
  496. */
  497. aabb3f box = box_0;
  498. box.MinEdge += *pos_f;
  499. box.MaxEdge += *pos_f;
  500. for (const auto &box_info : cinfo) {
  501. const aabb3f &cbox = box_info.box;
  502. /*
  503. See if the object is touching ground.
  504. Object touches ground if object's minimum Y is near node's
  505. maximum Y and object's X-Z-area overlaps with the node's
  506. X-Z-area.
  507. */
  508. if (cbox.MaxEdge.X - d > box.MinEdge.X && cbox.MinEdge.X + d < box.MaxEdge.X &&
  509. cbox.MaxEdge.Z - d > box.MinEdge.Z &&
  510. cbox.MinEdge.Z + d < box.MaxEdge.Z) {
  511. if (box_info.is_step_up) {
  512. pos_f->Y += cbox.MaxEdge.Y - box.MinEdge.Y;
  513. box = box_0;
  514. box.MinEdge += *pos_f;
  515. box.MaxEdge += *pos_f;
  516. }
  517. if (std::fabs(cbox.MaxEdge.Y - box.MinEdge.Y) < 0.05f) {
  518. result.touching_ground = true;
  519. if (box_info.isObject())
  520. result.standing_on_object = true;
  521. }
  522. }
  523. }
  524. return result;
  525. }