return false;
}
+static inline void getNeighborConnectingFace(v3s16 p, INodeDefManager *nodedef,
+ Map *map, MapNode n, int v, int *neighbors)
+{
+ MapNode n2 = map->getNodeNoEx(p);
+ if (nodedef->nodeboxConnects(n, n2, v))
+ *neighbors |= v;
+}
collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
f32 pos_max_d, const aabb3f &box_0,
std::vector<v3s16> node_positions;
{
//TimeTaker tt2("collisionMoveSimple collect boxes");
- ScopeProfiler sp(g_profiler, "collisionMoveSimple collect boxes avg", SPT_AVG);
+ ScopeProfiler sp(g_profiler, "collisionMoveSimple collect boxes avg", SPT_AVG);
v3s16 oldpos_i = floatToInt(*pos_f, BS);
v3s16 newpos_i = floatToInt(*pos_f + *speed_f * dtime, BS);
bool any_position_valid = false;
- // The order is important here, must be y first
- for(s16 y = max_y; y >= min_y; y--)
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);
// Object collides into walkable nodes
any_position_valid = true;
- const ContentFeatures &f = gamedef->getNodeDefManager()->get(n);
+ INodeDefManager *nodedef = gamedef->getNodeDefManager();
+ const ContentFeatures &f = nodedef->get(n);
if(f.walkable == false)
continue;
int n_bouncy_value = itemgroup_get(f.groups, "bouncy");
- std::vector<aabb3f> nodeboxes = n.getCollisionBoxes(gamedef->ndef());
+ int neighbors = 0;
+ if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
+ v3s16 p2 = p;
+
+ p2.Y++;
+ getNeighborConnectingFace(p2, nodedef, map, n, 1, &neighbors);
+
+ p2 = p;
+ p2.Y--;
+ getNeighborConnectingFace(p2, nodedef, map, n, 2, &neighbors);
+
+ p2 = p;
+ p2.Z--;
+ getNeighborConnectingFace(p2, nodedef, map, n, 4, &neighbors);
+
+ p2 = p;
+ p2.X--;
+ getNeighborConnectingFace(p2, nodedef, map, n, 8, &neighbors);
+
+ p2 = p;
+ p2.Z++;
+ getNeighborConnectingFace(p2, nodedef, map, n, 16, &neighbors);
+
+ p2 = p;
+ p2.X++;
+ getNeighborConnectingFace(p2, nodedef, map, n, 32, &neighbors);
+ }
+ std::vector<aabb3f> nodeboxes;
+ n.getCollisionBoxes(gamedef->ndef(), &nodeboxes, neighbors);
for(std::vector<aabb3f>::iterator
i = nodeboxes.begin();
i != nodeboxes.end(); ++i)
// Do not move if world has not loaded yet, since custom node boxes
// are not available for collision detection.
- if (!any_position_valid)
+ if (!any_position_valid) {
+ *speed_f = v3f(0, 0, 0);
return result;
+ }
} // tt2
while(dtime > BS * 1e-10) {
//TimeTaker tt3("collisionMoveSimple dtime loop");
- ScopeProfiler sp(g_profiler, "collisionMoveSimple dtime loop avg", SPT_AVG);
+ ScopeProfiler sp(g_profiler, "collisionMoveSimple dtime loop avg", SPT_AVG);
// Avoid infinite loop
loopcount++;
if (loopcount >= 100) {
warningstream << "collisionMoveSimple: Loop count exceeded, aborting to avoid infiniite loop" << std::endl;
- dtime = 0;
break;
}
int nearest_collided = -1;
f32 nearest_dtime = dtime;
- u32 nearest_boxindex = -1;
+ int nearest_boxindex = -1;
/*
Go through every nodebox, find nearest collision
*/
for (u32 boxindex = 0; boxindex < cboxes.size(); boxindex++) {
+ // Ignore if already stepped up this nodebox.
+ if(is_step_up[boxindex])
+ continue;
+
// Find nearest collision of the two boxes (raytracing-like)
f32 dtime_tmp;
int collided = axisAlignedCollision(
cboxes[boxindex], movingbox, *speed_f, d, &dtime_tmp);
- // Ignore if already stepped up this nodebox.
- if (is_step_up[boxindex]) {
- pos_f->Y += (cboxes[boxindex].MaxEdge.Y - movingbox.MinEdge.Y);
- continue;
- }
-
if (collided == -1 || dtime_tmp >= nearest_dtime)
continue;
is_collision = false;
CollisionInfo info;
- if (is_object[nearest_boxindex]) {
+ if (is_object[nearest_boxindex])
info.type = COLLISION_OBJECT;
- result.standing_on_object = true;
- } else {
+ else
info.type = COLLISION_NODE;
- }
info.node_p = node_positions[nearest_boxindex];
info.bouncy = bouncy;
speed_f->X = 0;
result.collides = true;
result.collides_xz = true;
- } else if(nearest_collided == 1) { // Y
- if (fabs(speed_f->Y) > BS * 3) {
+ }
+ else if(nearest_collided == 1) { // Y
+ if (fabs(speed_f->Y) > BS * 3)
speed_f->Y *= bounce;
- } else {
+ else
speed_f->Y = 0;
- result.touching_ground = true;
- }
result.collides = true;
} else if(nearest_collided == 2) { // Z
if (fabs(speed_f->Z) > BS * 3)
}
}
+ /*
+ Final touches: Check if standing on ground, step up stairs.
+ */
+ aabb3f box = box_0;
+ box.MinEdge += *pos_f;
+ box.MaxEdge += *pos_f;
+ for (u32 boxindex = 0; boxindex < cboxes.size(); boxindex++) {
+ const aabb3f& cbox = cboxes[boxindex];
+
+ /*
+ See if the object is touching ground.
+
+ Object touches ground if object's minimum Y is near node's
+ maximum Y and object's X-Z-area overlaps with the node's
+ X-Z-area.
+
+ Use 0.15*BS so that it is easier to get on a node.
+ */
+ if (cbox.MaxEdge.X - d > box.MinEdge.X && cbox.MinEdge.X + d < box.MaxEdge.X &&
+ cbox.MaxEdge.Z - d > box.MinEdge.Z &&
+ cbox.MinEdge.Z + d < box.MaxEdge.Z) {
+ if (is_step_up[boxindex]) {
+ pos_f->Y += (cbox.MaxEdge.Y - box.MinEdge.Y);
+ box = box_0;
+ box.MinEdge += *pos_f;
+ box.MaxEdge += *pos_f;
+ }
+ if (fabs(cbox.MaxEdge.Y - box.MinEdge.Y) < 0.15 * BS) {
+ result.touching_ground = true;
+
+ if (is_object[boxindex])
+ result.standing_on_object = true;
+ if (is_unloaded[boxindex])
+ result.standing_on_unloaded = true;
+ }
+ }
+ }
+
return result;
}