X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fcollision.cpp;h=5d52202d9b3a7935ca40ff9ec5ad941184834f03;hb=e0b57c1140554fccbf3e57a036cc4100887ab8f1;hp=9e0c85531cf4ced2cf987e17f4bcb9eca17e8d0a;hpb=5b8855e83c0d1cc7aef21492e7fe862b7d06917e;p=oweals%2Fminetest.git diff --git a/src/collision.cpp b/src/collision.cpp index 9e0c85531..5d52202d9 100644 --- a/src/collision.cpp +++ b/src/collision.cpp @@ -28,7 +28,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include "util/timetaker.h" -#include "main.h" // g_profiler #include "profiler.h" // float error is 10 - 9.96875 = 0.03125 @@ -173,11 +172,11 @@ bool wouldCollideWithCeiling( { //TimeTaker tt("wouldCollideWithCeiling"); - assert(y_increase >= 0); + assert(y_increase >= 0); // pre-condition for(std::vector::const_iterator i = staticboxes.begin(); - i != staticboxes.end(); i++) + i != staticboxes.end(); ++i) { const aabb3f& staticbox = *i; if((movingbox.MaxEdge.Y - d <= staticbox.MinEdge.Y) && @@ -246,6 +245,8 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, s16 max_y = MYMAX(oldpos_i.Y, newpos_i.Y) + (box_0.MaxEdge.Y / BS) + 1; s16 max_z = MYMAX(oldpos_i.Z, newpos_i.Z) + (box_0.MaxEdge.Z / BS) + 1; + bool any_position_valid = false; + for(s16 x = min_x; x <= max_x; x++) for(s16 y = min_y; y <= max_y; y++) for(s16 z = min_z; z <= max_z; z++) @@ -258,6 +259,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, if (is_position_valid) { // Object collides into walkable nodes + any_position_valid = true; const ContentFeatures &f = gamedef->getNodeDefManager()->get(n); if(f.walkable == false) continue; @@ -266,7 +268,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, std::vector nodeboxes = n.getCollisionBoxes(gamedef->ndef()); for(std::vector::iterator i = nodeboxes.begin(); - i != nodeboxes.end(); i++) + i != nodeboxes.end(); ++i) { aabb3f box = *i; box.MinEdge += v3f(x, y, z)*BS; @@ -290,6 +292,12 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, is_object.push_back(false); } } + + // Do not move if world has not loaded yet, since custom node boxes + // are not available for collision detection. + if (!any_position_valid) + return result; + } // tt2 if(collideWithObjects) @@ -299,17 +307,14 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, /* add object boxes to cboxes */ - - std::list objects; + std::vector objects; #ifndef SERVER ClientEnvironment *c_env = dynamic_cast(env); - if (c_env != 0) - { + if (c_env != 0) { f32 distance = speed_f.getLength(); std::vector clientobjects; c_env->getActiveObjects(pos_f,distance * 1.5,clientobjects); - for (size_t i=0; i < clientobjects.size(); i++) - { + for (size_t i=0; i < clientobjects.size(); i++) { if ((self == 0) || (self != clientobjects[i].obj)) { objects.push_back((ActiveObject*)clientobjects[i].obj); } @@ -319,12 +324,11 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, #endif { ServerEnvironment *s_env = dynamic_cast(env); - if (s_env != 0) - { + if (s_env != 0) { f32 distance = speed_f.getLength(); - std::set s_objects = s_env->getObjectsInsideRadius(pos_f,distance * 1.5); - for (std::set::iterator iter = s_objects.begin(); iter != s_objects.end(); iter++) - { + std::vector s_objects; + s_env->getObjectsInsideRadius(s_objects, pos_f, distance * 1.5); + for (std::vector::iterator iter = s_objects.begin(); iter != s_objects.end(); ++iter) { ServerActiveObject *current = s_env->getActiveObject(*iter); if ((self == 0) || (self != current)) { objects.push_back((ActiveObject*)current); @@ -333,16 +337,14 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, } } - for (std::list::const_iterator iter = objects.begin();iter != objects.end(); ++iter) - { + for (std::vector::const_iterator iter = objects.begin(); + iter != objects.end(); ++iter) { ActiveObject *object = *iter; - if (object != NULL) - { + if (object != NULL) { aabb3f object_collisionbox; if (object->getCollisionBox(&object_collisionbox) && - object->collideWithObjects()) - { + object->collideWithObjects()) { cboxes.push_back(object_collisionbox); is_unloaded.push_back(false); is_step_up.push_back(false); @@ -354,11 +356,11 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, } } //tt3 - assert(cboxes.size() == is_unloaded.size()); - assert(cboxes.size() == is_step_up.size()); - assert(cboxes.size() == bouncy_values.size()); - assert(cboxes.size() == node_positions.size()); - assert(cboxes.size() == is_object.size()); + assert(cboxes.size() == is_unloaded.size()); // post-condition + assert(cboxes.size() == is_step_up.size()); // post-condition + assert(cboxes.size() == bouncy_values.size()); // post-condition + assert(cboxes.size() == node_positions.size()); // post-condition + assert(cboxes.size() == is_object.size()); // post-condition /* Collision detection @@ -373,7 +375,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, //f32 d = 0.15*BS; // This should always apply, otherwise there are glitches - assert(d > pos_max_d); + assert(d > pos_max_d); // invariant int loopcount = 0; @@ -464,7 +466,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, pos_f += speed_f * nearest_dtime; dtime -= nearest_dtime; } - + bool is_collision = true; if(is_unloaded[nearest_boxindex]) is_collision = false; @@ -567,76 +569,3 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef, return result; } - -#if 0 -// This doesn't seem to work and isn't used -collisionMoveResult collisionMovePrecise(Map *map, IGameDef *gamedef, - f32 pos_max_d, const aabb3f &box_0, - f32 stepheight, f32 dtime, - v3f &pos_f, v3f &speed_f, v3f &accel_f) -{ - //TimeTaker tt("collisionMovePrecise"); - ScopeProfiler sp(g_profiler, "collisionMovePrecise avg", SPT_AVG); - - collisionMoveResult final_result; - - // If there is no speed, there are no collisions - if(speed_f.getLength() == 0) - return final_result; - - // Don't allow overly huge dtime - if(dtime > 2.0) - dtime = 2.0; - - f32 dtime_downcount = dtime; - - u32 loopcount = 0; - do - { - loopcount++; - - // Maximum time increment (for collision detection etc) - // time = distance / speed - f32 dtime_max_increment = 1.0; - if(speed_f.getLength() != 0) - dtime_max_increment = pos_max_d / speed_f.getLength(); - - // Maximum time increment is 10ms or lower - if(dtime_max_increment > 0.01) - dtime_max_increment = 0.01; - - f32 dtime_part; - if(dtime_downcount > dtime_max_increment) - { - dtime_part = dtime_max_increment; - dtime_downcount -= dtime_part; - } - else - { - dtime_part = dtime_downcount; - /* - Setting this to 0 (no -=dtime_part) disables an infinite loop - when dtime_part is so small that dtime_downcount -= dtime_part - does nothing - */ - dtime_downcount = 0; - } - - collisionMoveResult result = collisionMoveSimple(map, gamedef, - pos_max_d, box_0, stepheight, dtime_part, - pos_f, speed_f, accel_f); - - if(result.touching_ground) - final_result.touching_ground = true; - if(result.collides) - final_result.collides = true; - if(result.collides_xz) - final_result.collides_xz = true; - if(result.standing_on_unloaded) - final_result.standing_on_unloaded = true; - } - while(dtime_downcount > 0.001); - - return final_result; -} -#endif