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