collision.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. static bool time_notification_done = false;
  200. Map *map = &env->getMap();
  201. ScopeProfiler sp(g_profiler, "collisionMoveSimple()", SPT_AVG);
  202. collisionMoveResult result;
  203. /*
  204. Calculate new velocity
  205. */
  206. if (dtime > 0.5f) {
  207. if (!time_notification_done) {
  208. time_notification_done = true;
  209. infostream << "collisionMoveSimple: maximum step interval exceeded,"
  210. " lost movement details!"<<std::endl;
  211. }
  212. dtime = 0.5f;
  213. } else {
  214. time_notification_done = false;
  215. }
  216. *speed_f += accel_f * dtime;
  217. // If there is no speed, there are no collisions
  218. if (speed_f->getLength() == 0)
  219. return result;
  220. // Limit speed for avoiding hangs
  221. speed_f->Y = rangelim(speed_f->Y, -5000, 5000);
  222. speed_f->X = rangelim(speed_f->X, -5000, 5000);
  223. speed_f->Z = rangelim(speed_f->Z, -5000, 5000);
  224. *speed_f = truncate(*speed_f, 10000.0f);
  225. /*
  226. Collect node boxes in movement range
  227. */
  228. std::vector<NearbyCollisionInfo> cinfo;
  229. {
  230. //TimeTaker tt2("collisionMoveSimple collect boxes");
  231. ScopeProfiler sp2(g_profiler, "collisionMoveSimple(): collect boxes", SPT_AVG);
  232. v3f newpos_f = *pos_f + *speed_f * dtime;
  233. v3f minpos_f(
  234. MYMIN(pos_f->X, newpos_f.X),
  235. MYMIN(pos_f->Y, newpos_f.Y) + 0.01f * BS, // bias rounding, player often at +/-n.5
  236. MYMIN(pos_f->Z, newpos_f.Z)
  237. );
  238. v3f maxpos_f(
  239. MYMAX(pos_f->X, newpos_f.X),
  240. MYMAX(pos_f->Y, newpos_f.Y),
  241. MYMAX(pos_f->Z, newpos_f.Z)
  242. );
  243. v3s16 min = floatToInt(minpos_f + box_0.MinEdge, BS) - v3s16(1, 1, 1);
  244. v3s16 max = floatToInt(maxpos_f + box_0.MaxEdge, BS) + v3s16(1, 1, 1);
  245. bool any_position_valid = false;
  246. v3s16 p;
  247. for (p.X = min.X; p.X <= max.X; p.X++)
  248. for (p.Y = min.Y; p.Y <= max.Y; p.Y++)
  249. for (p.Z = min.Z; p.Z <= max.Z; p.Z++) {
  250. bool is_position_valid;
  251. MapNode n = map->getNode(p, &is_position_valid);
  252. if (is_position_valid && n.getContent() != CONTENT_IGNORE) {
  253. // Object collides into walkable nodes
  254. any_position_valid = true;
  255. const NodeDefManager *nodedef = gamedef->getNodeDefManager();
  256. const ContentFeatures &f = nodedef->get(n);
  257. if (!f.walkable)
  258. continue;
  259. // Negative bouncy may have a meaning, but we need +value here.
  260. int n_bouncy_value = abs(itemgroup_get(f.groups, "bouncy"));
  261. int neighbors = 0;
  262. if (f.drawtype == NDT_NODEBOX &&
  263. f.node_box.type == NODEBOX_CONNECTED) {
  264. v3s16 p2 = p;
  265. p2.Y++;
  266. getNeighborConnectingFace(p2, nodedef, map, n, 1, &neighbors);
  267. p2 = p;
  268. p2.Y--;
  269. getNeighborConnectingFace(p2, nodedef, map, n, 2, &neighbors);
  270. p2 = p;
  271. p2.Z--;
  272. getNeighborConnectingFace(p2, nodedef, map, n, 4, &neighbors);
  273. p2 = p;
  274. p2.X--;
  275. getNeighborConnectingFace(p2, nodedef, map, n, 8, &neighbors);
  276. p2 = p;
  277. p2.Z++;
  278. getNeighborConnectingFace(p2, nodedef, map, n, 16, &neighbors);
  279. p2 = p;
  280. p2.X++;
  281. getNeighborConnectingFace(p2, nodedef, map, n, 32, &neighbors);
  282. }
  283. std::vector<aabb3f> nodeboxes;
  284. n.getCollisionBoxes(gamedef->ndef(), &nodeboxes, neighbors);
  285. // Calculate float position only once
  286. v3f posf = intToFloat(p, BS);
  287. for (auto box : nodeboxes) {
  288. box.MinEdge += posf;
  289. box.MaxEdge += posf;
  290. cinfo.emplace_back(false, n_bouncy_value, p, box);
  291. }
  292. } else {
  293. // Collide with unloaded nodes (position invalid) and loaded
  294. // CONTENT_IGNORE nodes (position valid)
  295. aabb3f box = getNodeBox(p, BS);
  296. cinfo.emplace_back(true, 0, p, box);
  297. }
  298. }
  299. // Do not move if world has not loaded yet, since custom node boxes
  300. // are not available for collision detection.
  301. // This also intentionally occurs in the case of the object being positioned
  302. // solely on loaded CONTENT_IGNORE nodes, no matter where they come from.
  303. if (!any_position_valid) {
  304. *speed_f = v3f(0, 0, 0);
  305. return result;
  306. }
  307. } // tt2
  308. if(collideWithObjects)
  309. {
  310. /* add object boxes to cinfo */
  311. std::vector<ActiveObject*> objects;
  312. #ifndef SERVER
  313. ClientEnvironment *c_env = dynamic_cast<ClientEnvironment*>(env);
  314. if (c_env != 0) {
  315. // Calculate distance by speed, add own extent and 1.5m of tolerance
  316. f32 distance = speed_f->getLength() * dtime +
  317. box_0.getExtent().getLength() + 1.5f * BS;
  318. std::vector<DistanceSortedActiveObject> clientobjects;
  319. c_env->getActiveObjects(*pos_f, distance, clientobjects);
  320. for (auto &clientobject : clientobjects) {
  321. // Do collide with everything but itself and the parent CAO
  322. if (!self || (self != clientobject.obj &&
  323. self != clientobject.obj->getParent())) {
  324. objects.push_back((ActiveObject*) clientobject.obj);
  325. }
  326. }
  327. }
  328. else
  329. #endif
  330. {
  331. ServerEnvironment *s_env = dynamic_cast<ServerEnvironment*>(env);
  332. if (s_env != NULL) {
  333. // Calculate distance by speed, add own extent and 1.5m of tolerance
  334. f32 distance = speed_f->getLength() * dtime +
  335. box_0.getExtent().getLength() + 1.5f * BS;
  336. // search for objects which are not us, or we are not its parent
  337. // we directly use the callback to populate the result to prevent
  338. // a useless result loop here
  339. auto include_obj_cb = [self, &objects] (ServerActiveObject *obj) {
  340. if (!obj->isGone() &&
  341. (!self || (self != obj && self != obj->getParent()))) {
  342. objects.push_back((ActiveObject *)obj);
  343. }
  344. return false;
  345. };
  346. std::vector<ServerActiveObject *> s_objects;
  347. s_env->getObjectsInsideRadius(s_objects, *pos_f, distance, include_obj_cb);
  348. }
  349. }
  350. for (std::vector<ActiveObject*>::const_iterator iter = objects.begin();
  351. iter != objects.end(); ++iter) {
  352. ActiveObject *object = *iter;
  353. if (object && object->collideWithObjects()) {
  354. aabb3f object_collisionbox;
  355. if (object->getCollisionBox(&object_collisionbox))
  356. cinfo.emplace_back(object, 0, object_collisionbox);
  357. }
  358. }
  359. #ifndef SERVER
  360. if (self && c_env) {
  361. LocalPlayer *lplayer = c_env->getLocalPlayer();
  362. if (lplayer->getParent() == nullptr) {
  363. aabb3f lplayer_collisionbox = lplayer->getCollisionbox();
  364. v3f lplayer_pos = lplayer->getPosition();
  365. lplayer_collisionbox.MinEdge += lplayer_pos;
  366. lplayer_collisionbox.MaxEdge += lplayer_pos;
  367. ActiveObject *obj = (ActiveObject*) lplayer->getCAO();
  368. cinfo.emplace_back(obj, 0, lplayer_collisionbox);
  369. }
  370. }
  371. #endif
  372. } //tt3
  373. /*
  374. Collision detection
  375. */
  376. f32 d = 0.0f;
  377. int loopcount = 0;
  378. while(dtime > BS * 1e-10f) {
  379. // Avoid infinite loop
  380. loopcount++;
  381. if (loopcount >= 100) {
  382. warningstream << "collisionMoveSimple: Loop count exceeded, aborting to avoid infiniite loop" << std::endl;
  383. break;
  384. }
  385. aabb3f movingbox = box_0;
  386. movingbox.MinEdge += *pos_f;
  387. movingbox.MaxEdge += *pos_f;
  388. CollisionAxis nearest_collided = COLLISION_AXIS_NONE;
  389. f32 nearest_dtime = dtime;
  390. int nearest_boxindex = -1;
  391. /*
  392. Go through every nodebox, find nearest collision
  393. */
  394. for (u32 boxindex = 0; boxindex < cinfo.size(); boxindex++) {
  395. const NearbyCollisionInfo &box_info = cinfo[boxindex];
  396. // Ignore if already stepped up this nodebox.
  397. if (box_info.is_step_up)
  398. continue;
  399. // Find nearest collision of the two boxes (raytracing-like)
  400. f32 dtime_tmp = nearest_dtime;
  401. CollisionAxis collided = axisAlignedCollision(box_info.box,
  402. movingbox, *speed_f, &dtime_tmp);
  403. if (collided == -1 || dtime_tmp >= nearest_dtime)
  404. continue;
  405. nearest_dtime = dtime_tmp;
  406. nearest_collided = collided;
  407. nearest_boxindex = boxindex;
  408. }
  409. if (nearest_collided == COLLISION_AXIS_NONE) {
  410. // No collision with any collision box.
  411. *pos_f += truncate(*speed_f * dtime, 100.0f);
  412. dtime = 0; // Set to 0 to avoid "infinite" loop due to small FP numbers
  413. } else {
  414. // Otherwise, a collision occurred.
  415. NearbyCollisionInfo &nearest_info = cinfo[nearest_boxindex];
  416. const aabb3f& cbox = nearest_info.box;
  417. //movingbox except moved to the horizontal position it would be after step up
  418. aabb3f stepbox = movingbox;
  419. stepbox.MinEdge.X += speed_f->X * dtime;
  420. stepbox.MinEdge.Z += speed_f->Z * dtime;
  421. stepbox.MaxEdge.X += speed_f->X * dtime;
  422. stepbox.MaxEdge.Z += speed_f->Z * dtime;
  423. // Check for stairs.
  424. bool step_up = (nearest_collided != COLLISION_AXIS_Y) && // must not be Y direction
  425. (movingbox.MinEdge.Y < cbox.MaxEdge.Y) &&
  426. (movingbox.MinEdge.Y + stepheight > cbox.MaxEdge.Y) &&
  427. (!wouldCollideWithCeiling(cinfo, stepbox,
  428. cbox.MaxEdge.Y - movingbox.MinEdge.Y,
  429. d));
  430. // Get bounce multiplier
  431. float bounce = -(float)nearest_info.bouncy / 100.0f;
  432. // Move to the point of collision and reduce dtime by nearest_dtime
  433. if (nearest_dtime < 0) {
  434. // Handle negative nearest_dtime
  435. if (!step_up) {
  436. if (nearest_collided == COLLISION_AXIS_X)
  437. pos_f->X += speed_f->X * nearest_dtime;
  438. if (nearest_collided == COLLISION_AXIS_Y)
  439. pos_f->Y += speed_f->Y * nearest_dtime;
  440. if (nearest_collided == COLLISION_AXIS_Z)
  441. pos_f->Z += speed_f->Z * nearest_dtime;
  442. }
  443. } else {
  444. *pos_f += truncate(*speed_f * nearest_dtime, 100.0f);
  445. dtime -= nearest_dtime;
  446. }
  447. bool is_collision = true;
  448. if (nearest_info.is_unloaded)
  449. is_collision = false;
  450. CollisionInfo info;
  451. if (nearest_info.isObject())
  452. info.type = COLLISION_OBJECT;
  453. else
  454. info.type = COLLISION_NODE;
  455. info.node_p = nearest_info.position;
  456. info.object = nearest_info.obj;
  457. info.old_speed = *speed_f;
  458. info.plane = nearest_collided;
  459. // Set the speed component that caused the collision to zero
  460. if (step_up) {
  461. // Special case: Handle stairs
  462. nearest_info.is_step_up = true;
  463. is_collision = false;
  464. } else if (nearest_collided == COLLISION_AXIS_X) {
  465. if (fabs(speed_f->X) > BS * 3)
  466. speed_f->X *= bounce;
  467. else
  468. speed_f->X = 0;
  469. result.collides = true;
  470. } else if (nearest_collided == COLLISION_AXIS_Y) {
  471. if(fabs(speed_f->Y) > BS * 3)
  472. speed_f->Y *= bounce;
  473. else
  474. speed_f->Y = 0;
  475. result.collides = true;
  476. } else if (nearest_collided == COLLISION_AXIS_Z) {
  477. if (fabs(speed_f->Z) > BS * 3)
  478. speed_f->Z *= bounce;
  479. else
  480. speed_f->Z = 0;
  481. result.collides = true;
  482. }
  483. info.new_speed = *speed_f;
  484. if (info.new_speed.getDistanceFrom(info.old_speed) < 0.1f * BS)
  485. is_collision = false;
  486. if (is_collision) {
  487. info.axis = nearest_collided;
  488. result.collisions.push_back(info);
  489. }
  490. }
  491. }
  492. /*
  493. Final touches: Check if standing on ground, step up stairs.
  494. */
  495. aabb3f box = box_0;
  496. box.MinEdge += *pos_f;
  497. box.MaxEdge += *pos_f;
  498. for (const auto &box_info : cinfo) {
  499. const aabb3f &cbox = box_info.box;
  500. /*
  501. See if the object is touching ground.
  502. Object touches ground if object's minimum Y is near node's
  503. maximum Y and object's X-Z-area overlaps with the node's
  504. X-Z-area.
  505. */
  506. if (cbox.MaxEdge.X - d > box.MinEdge.X && cbox.MinEdge.X + d < box.MaxEdge.X &&
  507. cbox.MaxEdge.Z - d > box.MinEdge.Z &&
  508. cbox.MinEdge.Z + d < box.MaxEdge.Z) {
  509. if (box_info.is_step_up) {
  510. pos_f->Y += cbox.MaxEdge.Y - box.MinEdge.Y;
  511. box = box_0;
  512. box.MinEdge += *pos_f;
  513. box.MaxEdge += *pos_f;
  514. }
  515. if (std::fabs(cbox.MaxEdge.Y - box.MinEdge.Y) < 0.05f) {
  516. result.touching_ground = true;
  517. if (box_info.isObject())
  518. result.standing_on_object = true;
  519. }
  520. }
  521. }
  522. return result;
  523. }