#include <vector>
#include <set>
#include "util/timetaker.h"
-#include "main.h" // g_profiler
#include "profiler.h"
+// float error is 10 - 9.96875 = 0.03125
+//#define COLL_ZERO 0.032 // broken unit tests
+#define COLL_ZERO 0
+
// Helper function:
// Checks for collision of a moving aabbox with a static aabbox
// Returns -1 if no collision, 0 if X collision, 1 if Y collision, 2 if Z collision
{
//TimeTaker tt("axisAlignedCollision");
- f32 xsize = (staticbox.MaxEdge.X - staticbox.MinEdge.X);
- f32 ysize = (staticbox.MaxEdge.Y - staticbox.MinEdge.Y);
- f32 zsize = (staticbox.MaxEdge.Z - staticbox.MinEdge.Z);
+ f32 xsize = (staticbox.MaxEdge.X - staticbox.MinEdge.X) - COLL_ZERO; // reduce box size for solve collision stuck (flying sand)
+ f32 ysize = (staticbox.MaxEdge.Y - staticbox.MinEdge.Y); // - COLL_ZERO; // Y - no sense for falling, but maybe try later
+ f32 zsize = (staticbox.MaxEdge.Z - staticbox.MinEdge.Z) - COLL_ZERO;
aabb3f relbox(
movingbox.MinEdge.X - staticbox.MinEdge.X,
{
dtime = - relbox.MaxEdge.X / speed.X;
if((relbox.MinEdge.Y + speed.Y * dtime < ysize) &&
- (relbox.MaxEdge.Y + speed.Y * dtime > 0) &&
+ (relbox.MaxEdge.Y + speed.Y * dtime > COLL_ZERO) &&
(relbox.MinEdge.Z + speed.Z * dtime < zsize) &&
- (relbox.MaxEdge.Z + speed.Z * dtime > 0))
+ (relbox.MaxEdge.Z + speed.Z * dtime > COLL_ZERO))
return 0;
}
else if(relbox.MinEdge.X > xsize)
{
dtime = (xsize - relbox.MinEdge.X) / speed.X;
if((relbox.MinEdge.Y + speed.Y * dtime < ysize) &&
- (relbox.MaxEdge.Y + speed.Y * dtime > 0) &&
+ (relbox.MaxEdge.Y + speed.Y * dtime > COLL_ZERO) &&
(relbox.MinEdge.Z + speed.Z * dtime < zsize) &&
- (relbox.MaxEdge.Z + speed.Z * dtime > 0))
+ (relbox.MaxEdge.Z + speed.Z * dtime > COLL_ZERO))
return 0;
}
else if(relbox.MaxEdge.X < 0)
{
dtime = - relbox.MaxEdge.Y / speed.Y;
if((relbox.MinEdge.X + speed.X * dtime < xsize) &&
- (relbox.MaxEdge.X + speed.X * dtime > 0) &&
+ (relbox.MaxEdge.X + speed.X * dtime > COLL_ZERO) &&
(relbox.MinEdge.Z + speed.Z * dtime < zsize) &&
- (relbox.MaxEdge.Z + speed.Z * dtime > 0))
+ (relbox.MaxEdge.Z + speed.Z * dtime > COLL_ZERO))
return 1;
}
else if(relbox.MinEdge.Y > ysize)
{
dtime = (ysize - relbox.MinEdge.Y) / speed.Y;
if((relbox.MinEdge.X + speed.X * dtime < xsize) &&
- (relbox.MaxEdge.X + speed.X * dtime > 0) &&
+ (relbox.MaxEdge.X + speed.X * dtime > COLL_ZERO) &&
(relbox.MinEdge.Z + speed.Z * dtime < zsize) &&
- (relbox.MaxEdge.Z + speed.Z * dtime > 0))
+ (relbox.MaxEdge.Z + speed.Z * dtime > COLL_ZERO))
return 1;
}
else if(relbox.MaxEdge.Y < 0)
{
dtime = - relbox.MaxEdge.Z / speed.Z;
if((relbox.MinEdge.X + speed.X * dtime < xsize) &&
- (relbox.MaxEdge.X + speed.X * dtime > 0) &&
+ (relbox.MaxEdge.X + speed.X * dtime > COLL_ZERO) &&
(relbox.MinEdge.Y + speed.Y * dtime < ysize) &&
- (relbox.MaxEdge.Y + speed.Y * dtime > 0))
+ (relbox.MaxEdge.Y + speed.Y * dtime > COLL_ZERO))
return 2;
}
//else if(relbox.MinEdge.Z > zsize)
{
dtime = (zsize - relbox.MinEdge.Z) / speed.Z;
if((relbox.MinEdge.X + speed.X * dtime < xsize) &&
- (relbox.MaxEdge.X + speed.X * dtime > 0) &&
+ (relbox.MaxEdge.X + speed.X * dtime > COLL_ZERO) &&
(relbox.MinEdge.Y + speed.Y * dtime < ysize) &&
- (relbox.MaxEdge.Y + speed.Y * dtime > 0))
+ (relbox.MaxEdge.Y + speed.Y * dtime > COLL_ZERO))
return 2;
}
//else if(relbox.MaxEdge.Z < 0)
{
//TimeTaker tt("wouldCollideWithCeiling");
- assert(y_increase >= 0);
+ assert(y_increase >= 0); // pre-condition
for(std::vector<aabb3f>::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) &&
collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
f32 pos_max_d, const aabb3f &box_0,
f32 stepheight, f32 dtime,
- v3f &pos_f, v3f &speed_f, v3f &accel_f)
+ v3f &pos_f, v3f &speed_f,
+ v3f &accel_f,ActiveObject* self,
+ bool collideWithObjects)
{
Map *map = &env->getMap();
//TimeTaker tt("collisionMoveSimple");
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++)
{
v3s16 p(x,y,z);
- try{
+
+ bool is_position_valid;
+ MapNode n = map->getNodeNoEx(p, &is_position_valid);
+
+ if (is_position_valid) {
// Object collides into walkable nodes
- MapNode n = map->getNode(p);
+
+ any_position_valid = true;
const ContentFeatures &f = gamedef->getNodeDefManager()->get(n);
if(f.walkable == false)
continue;
int n_bouncy_value = itemgroup_get(f.groups, "bouncy");
- std::vector<aabb3f> nodeboxes = n.getNodeBoxes(gamedef->ndef());
+ std::vector<aabb3f> nodeboxes = n.getCollisionBoxes(gamedef->ndef());
for(std::vector<aabb3f>::iterator
i = nodeboxes.begin();
- i != nodeboxes.end(); i++)
+ i != nodeboxes.end(); ++i)
{
aabb3f box = *i;
box.MinEdge += v3f(x, y, z)*BS;
is_object.push_back(false);
}
}
- catch(InvalidPositionException &e)
- {
+ else {
// Collide with unloaded nodes
aabb3f box = getNodeBox(p, BS);
cboxes.push_back(box);
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)
{
ScopeProfiler sp(g_profiler, "collisionMoveSimple objects avg", SPT_AVG);
//TimeTaker tt3("collisionMoveSimple collect object boxes");
/* add object boxes to cboxes */
-
- std::list<ActiveObject*> objects;
+ std::vector<ActiveObject*> objects;
#ifndef SERVER
ClientEnvironment *c_env = dynamic_cast<ClientEnvironment*>(env);
- if (c_env != 0)
- {
+ if (c_env != 0) {
f32 distance = speed_f.getLength();
std::vector<DistanceSortedActiveObject> clientobjects;
c_env->getActiveObjects(pos_f,distance * 1.5,clientobjects);
- for (int i=0; i < clientobjects.size(); i++)
- {
- objects.push_back((ActiveObject*)clientobjects[i].obj);
+ for (size_t i=0; i < clientobjects.size(); i++) {
+ if ((self == 0) || (self != clientobjects[i].obj)) {
+ objects.push_back((ActiveObject*)clientobjects[i].obj);
+ }
}
}
else
#endif
{
ServerEnvironment *s_env = dynamic_cast<ServerEnvironment*>(env);
- if (s_env != 0)
- {
+ if (s_env != 0) {
f32 distance = speed_f.getLength();
- std::set<u16> s_objects = s_env->getObjectsInsideRadius(pos_f,distance * 1.5);
- for (std::set<u16>::iterator iter = s_objects.begin(); iter != s_objects.end(); iter++)
- {
+ std::vector<u16> s_objects;
+ s_env->getObjectsInsideRadius(s_objects, pos_f, distance * 1.5);
+ for (std::vector<u16>::iterator iter = s_objects.begin(); iter != s_objects.end(); ++iter) {
ServerActiveObject *current = s_env->getActiveObject(*iter);
- objects.push_back((ActiveObject*)current);
+ if ((self == 0) || (self != current)) {
+ objects.push_back((ActiveObject*)current);
+ }
}
}
}
- for (std::list<ActiveObject*>::const_iterator iter = objects.begin();iter != objects.end(); ++iter)
- {
+ for (std::vector<ActiveObject*>::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))
- {
+ if (object->getCollisionBox(&object_collisionbox) &&
+ object->collideWithObjects()) {
cboxes.push_back(object_collisionbox);
is_unloaded.push_back(false);
is_step_up.push_back(false);
}
} //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
//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;
pos_f += speed_f * nearest_dtime;
dtime -= nearest_dtime;
}
-
+
bool is_collision = true;
if(is_unloaded[nearest_boxindex])
is_collision = false;
if (is_object[nearest_boxindex]) {
info.type = COLLISION_OBJECT;
}
- else
+ else {
info.type = COLLISION_NODE;
+ }
info.node_p = node_positions[nearest_boxindex];
info.bouncy = bouncy;
info.old_speed = speed_f;
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