void Particle::step(float dtime) { m_time += dtime; if (m_collisiondetection) { core::aabbox3d<f32> box = m_collisionbox; v3f p_pos = m_pos*BS; v3f p_velocity = m_velocity*BS; v3f p_acceleration = m_acceleration*BS; collisionMoveSimple(m_env, m_gamedef, BS*0.5, box, 0, dtime, p_pos, p_velocity, p_acceleration); m_pos = p_pos/BS; m_velocity = p_velocity/BS; m_acceleration = p_acceleration/BS; } else { m_velocity += m_acceleration * dtime; m_pos += m_velocity * dtime; } // Update lighting updateLight(); // Update model updateVertices(); }
void step(float dtime, bool send_recommended) { ScopeProfiler sp2(g_profiler, "step avg", SPT_AVG); assert(m_env); const float interval = 0.2; if(m_move_interval.step(dtime, interval)==false) return; dtime = interval; core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.); collisionMoveResult moveresult; // Apply gravity m_speed_f += v3f(0, -dtime*9.81*BS, 0); // Maximum movement without glitches f32 pos_max_d = BS*0.25; // Limit speed if(m_speed_f.getLength()*dtime > pos_max_d) m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime); v3f pos_f = getBasePosition(); v3f pos_f_old = pos_f; v3f accel_f = v3f(0,0,0); f32 stepheight = 0; IGameDef *gamedef = m_env->getGameDef(); moveresult = collisionMoveSimple(&m_env->getMap(), gamedef, pos_max_d, box, stepheight, dtime, pos_f, m_speed_f, accel_f); if(send_recommended == false) return; if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS) { setBasePosition(pos_f); m_last_sent_position = pos_f; std::ostringstream os(std::ios::binary); // command (0 = update position) writeU8(os, 0); // pos writeV3F1000(os, m_base_position); // create message and add to list ActiveObjectMessage aom(getId(), false, os.str()); m_messages_out.push_back(aom); } if(m_itemstring_changed) { m_itemstring_changed = false; std::ostringstream os(std::ios::binary); // command (1 = update itemstring) writeU8(os, 1); // itemstring os<<serializeString(m_itemstring); // create message and add to list ActiveObjectMessage aom(getId(), false, os.str()); m_messages_out.push_back(aom); } }
void Particle::step(float dtime) { m_time += dtime; if (m_collisiondetection) { aabb3f box = m_collisionbox; v3f p_pos = m_pos * BS; v3f p_velocity = m_velocity * BS; collisionMoveResult r = collisionMoveSimple(m_env, m_gamedef, BS * 0.5, box, 0, dtime, &p_pos, &p_velocity, m_acceleration * BS); if (m_collision_removal && r.collides) { // force expiration of the particle m_expiration = -1.0; } else { m_pos = p_pos / BS; m_velocity = p_velocity / BS; } } else { m_velocity += m_acceleration * dtime; m_pos += m_velocity * dtime; } // Update lighting updateLight(); // Update model updateVertices(); }
collisionMoveResult collisionMovePrecise(Map *map, IGameDef *gamedef, f32 pos_max_d, const core::aabbox3d<f32> &box_0, f32 dtime, v3f &pos_f, v3f &speed_f) { collisionMoveResult final_result; // Maximum time increment (for collision detection etc) // time = distance / speed f32 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; // Don't allow overly huge dtime if(dtime > 2.0) dtime = 2.0; f32 dtime_downcount = dtime; u32 loopcount = 0; do { loopcount++; 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, dtime_part, pos_f, speed_f); if(result.touching_ground) final_result.touching_ground = true; if(result.collides) final_result.collides = true; } while(dtime_downcount > 0.001); return final_result; }
void ItemSAO::step(float dtime, bool send_recommended) { ScopeProfiler sp2(g_profiler, "ItemSAO::step avg", SPT_AVG); assert(m_env); const float interval = 0.2; if(m_move_interval.step(dtime, interval)==false) return; dtime = interval; core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.); collisionMoveResult moveresult; // Apply gravity m_speed_f += v3f(0, -dtime*9.81*BS, 0); // Maximum movement without glitches f32 pos_max_d = BS*0.25; // Limit speed if(m_speed_f.getLength()*dtime > pos_max_d) m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime); v3f pos_f = getBasePosition(); v3f pos_f_old = pos_f; IGameDef *gamedef = m_env->getGameDef(); moveresult = collisionMoveSimple(&m_env->getMap(), gamedef, pos_max_d, box, dtime, pos_f, m_speed_f); if(send_recommended == false) return; if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS) { setBasePosition(pos_f); m_last_sent_position = pos_f; std::ostringstream os(std::ios::binary); char buf[6]; // command (0 = update position) buf[0] = 0; os.write(buf, 1); // pos writeS32((u8*)buf, m_base_position.X*1000); os.write(buf, 4); writeS32((u8*)buf, m_base_position.Y*1000); os.write(buf, 4); writeS32((u8*)buf, m_base_position.Z*1000); os.write(buf, 4); // create message and add to list ActiveObjectMessage aom(getId(), false, os.str()); m_messages_out.push_back(aom); } }
void Particle::step(float dtime) { m_time += dtime; if (m_collisiondetection) { aabb3f box = m_collisionbox; v3f p_pos = m_pos * BS; v3f p_velocity = m_velocity * BS; collisionMoveResult r = collisionMoveSimple(m_env, m_gamedef, BS * 0.5, box, 0, dtime, &p_pos, &p_velocity, m_acceleration * BS); if (m_collision_removal && r.collides) { // force expiration of the particle m_expiration = -1.0; } else { m_pos = p_pos / BS; m_velocity = p_velocity / BS; } } else { m_velocity += m_acceleration * dtime; m_pos += m_velocity * dtime; } if (m_animation.type != TAT_NONE) { m_animation_time += dtime; int frame_length_i, frame_count; m_animation.determineParams( m_material.getTexture(0)->getSize(), &frame_count, &frame_length_i, NULL); float frame_length = frame_length_i / 1000.0; while (m_animation_time > frame_length) { m_animation_frame++; m_animation_time -= frame_length; } } // Update lighting updateLight(); // Update model updateVertices(); }
void LuaEntitySAO::step(float dtime, bool send_recommended) { if(!m_properties_sent) { m_properties_sent = true; std::string str = getPropertyPacket(); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push(aom); } // If attached, check that our parent is still there. If it isn't, detach. if(m_attachment_parent_id && !isAttached()) { m_attachment_parent_id = 0; m_attachment_bone = ""; m_attachment_position = v3f(0,0,0); m_attachment_rotation = v3f(0,0,0); sendPosition(false, true); } m_last_sent_position_timer += dtime; // Each frame, parent position is copied if the object is attached, otherwise it's calculated normally // If the object gets detached this comes into effect automatically from the last known origin if(isAttached()) { v3f pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition(); m_base_position = pos; m_velocity = v3f(0,0,0); m_acceleration = v3f(0,0,0); } else { if(m_prop.physical){ core::aabbox3d<f32> box = m_prop.collisionbox; box.MinEdge *= BS; box.MaxEdge *= BS; collisionMoveResult moveresult; f32 pos_max_d = BS*0.25; // Distance per iteration v3f p_pos = m_base_position; v3f p_velocity = m_velocity; v3f p_acceleration = m_acceleration; moveresult = collisionMoveSimple(m_env,m_env->getGameDef(), pos_max_d, box, m_prop.stepheight, dtime, p_pos, p_velocity, p_acceleration, this, m_prop.collideWithObjects); // Apply results m_base_position = p_pos; m_velocity = p_velocity; m_acceleration = p_acceleration; } else { m_base_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration; m_velocity += dtime * m_acceleration; } if((m_prop.automatic_face_movement_dir) && (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)){ m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI + m_prop.automatic_face_movement_dir_offset; } } if(m_registered){ m_env->getScriptIface()->luaentity_Step(m_id, dtime); } if(send_recommended == false) return; if(!isAttached()) { // TODO: force send when acceleration changes enough? float minchange = 0.2*BS; if(m_last_sent_position_timer > 1.0){ minchange = 0.01*BS; } else if(m_last_sent_position_timer > 0.2){ minchange = 0.05*BS; } float move_d = m_base_position.getDistanceFrom(m_last_sent_position); move_d += m_last_sent_move_precision; float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity); if(move_d > minchange || vel_d > minchange || fabs(m_yaw - m_last_sent_yaw) > 1.0){ sendPosition(true, false); } } if(m_armor_groups_sent == false){ m_armor_groups_sent = true; std::string str = gob_cmd_update_armor_groups( m_armor_groups); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push(aom); } if(m_animation_sent == false){ m_animation_sent = true; std::string str = gob_cmd_update_animation( m_animation_range, m_animation_speed, m_animation_blend, m_animation_loop); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push(aom); } if(m_bone_position_sent == false){ m_bone_position_sent = true; for(std::map<std::string, core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){ std::string str = gob_cmd_update_bone_position((*ii).first, (*ii).second.X, (*ii).second.Y); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push(aom); } } if(m_attachment_sent == false){ m_attachment_sent = true; std::string str = gob_cmd_update_attachment(m_attachment_parent_id, m_attachment_bone, m_attachment_position, m_attachment_rotation); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push(aom); } }
void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d, std::vector<CollisionInfo> *collision_info) { Map *map = &env->getMap(); INodeDefManager *nodemgr = m_gamedef->ndef(); v3f position = getPosition(); // Copy parent position if local player is attached if(isAttached) { setPosition(overridePosition); m_sneak_node_exists = false; return; } // Skip collision detection if noclip mode is used bool fly_allowed = m_gamedef->checkLocalPrivilege("fly"); bool noclip = m_gamedef->checkLocalPrivilege("noclip") && g_settings->getBool("noclip"); bool free_move = noclip && fly_allowed && g_settings->getBool("free_move"); if (free_move) { position += m_speed * dtime; setPosition(position); m_sneak_node_exists = false; return; } /* Collision detection */ bool is_valid_position; MapNode node; v3s16 pp; /* Check if player is in liquid (the oscillating value) */ // If in liquid, the threshold of coming out is at higher y if (in_liquid) { pp = floatToInt(position + v3f(0,BS*0.1,0), BS); node = map->getNodeNoEx(pp, &is_valid_position); if (is_valid_position) { in_liquid = nodemgr->get(node.getContent()).isLiquid(); liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity; } else { in_liquid = false; } } // If not in liquid, the threshold of going in is at lower y else { pp = floatToInt(position + v3f(0,BS*0.5,0), BS); node = map->getNodeNoEx(pp, &is_valid_position); if (is_valid_position) { in_liquid = nodemgr->get(node.getContent()).isLiquid(); liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity; } else { in_liquid = false; } } /* Check if player is in liquid (the stable value) */ pp = floatToInt(position + v3f(0,0,0), BS); node = map->getNodeNoEx(pp, &is_valid_position); if (is_valid_position) { in_liquid_stable = nodemgr->get(node.getContent()).isLiquid(); } else { in_liquid_stable = false; } /* Check if player is climbing */ pp = floatToInt(position + v3f(0,0.5*BS,0), BS); v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS); node = map->getNodeNoEx(pp, &is_valid_position); bool is_valid_position2; MapNode node2 = map->getNodeNoEx(pp2, &is_valid_position2); if (!(is_valid_position && is_valid_position2)) { is_climbing = false; } else { is_climbing = (nodemgr->get(node.getContent()).climbable || nodemgr->get(node2.getContent()).climbable) && !free_move; } /* Collision uncertainty radius Make it a bit larger than the maximum distance of movement */ //f32 d = pos_max_d * 1.1; // A fairly large value in here makes moving smoother f32 d = 0.15*BS; // This should always apply, otherwise there are glitches sanity_check(d > pos_max_d); // Maximum distance over border for sneaking f32 sneak_max = BS*0.4; /* If sneaking, keep in range from the last walked node and don't fall off from it */ if (control.sneak && m_sneak_node_exists && !(fly_allowed && g_settings->getBool("free_move")) && !in_liquid && physics_override_sneak) { f32 maxd = 0.5 * BS + sneak_max; v3f lwn_f = intToFloat(m_sneak_node, BS); position.X = rangelim(position.X, lwn_f.X-maxd, lwn_f.X+maxd); position.Z = rangelim(position.Z, lwn_f.Z-maxd, lwn_f.Z+maxd); if (!is_climbing) { // Move up if necessary f32 new_y = (lwn_f.Y - 0.5 * BS) + m_sneak_node_bb_ymax; if (position.Y < new_y) position.Y = new_y; /* Collision seems broken, since player is sinking when sneaking over the edges of current sneaking_node. TODO (when fixed): Set Y-speed only to 0 when position.Y < new_y. */ if (m_speed.Y < 0) m_speed.Y = 0; } } // this shouldn't be hardcoded but transmitted from server float player_stepheight = touching_ground ? (BS*0.6) : (BS*0.2); #ifdef __ANDROID__ player_stepheight += (0.5 * BS); #endif v3f accel_f = v3f(0,0,0); collisionMoveResult result = collisionMoveSimple(env, m_gamedef, pos_max_d, m_collisionbox, player_stepheight, dtime, &position, &m_speed, accel_f); /* If the player's feet touch the topside of any node, this is set to true. Player is allowed to jump when this is true. */ bool touching_ground_was = touching_ground; touching_ground = result.touching_ground; //bool standing_on_unloaded = result.standing_on_unloaded; /* Check the nodes under the player to see from which node the player is sneaking from, if any. If the node from under the player has been removed, the player falls. */ f32 position_y_mod = 0.05 * BS; if (m_sneak_node_bb_ymax > 0) position_y_mod = m_sneak_node_bb_ymax - position_y_mod; v3s16 current_node = floatToInt(position - v3f(0, position_y_mod, 0), BS); if (m_sneak_node_exists && nodemgr->get(map->getNodeNoEx(m_old_node_below)).name == "air" && m_old_node_below_type != "air") { // Old node appears to have been removed; that is, // it wasn't air before but now it is m_need_to_get_new_sneak_node = false; m_sneak_node_exists = false; } else if (nodemgr->get(map->getNodeNoEx(current_node)).name != "air") { // We are on something, so make sure to recalculate the sneak // node. m_need_to_get_new_sneak_node = true; } if (m_need_to_get_new_sneak_node && physics_override_sneak) { m_sneak_node_bb_ymax = 0; v3s16 pos_i_bottom = floatToInt(position - v3f(0, position_y_mod, 0), BS); v2f player_p2df(position.X, position.Z); f32 min_distance_f = 100000.0 * BS; // If already seeking from some node, compare to it. /*if(m_sneak_node_exists) { v3f sneaknode_pf = intToFloat(m_sneak_node, BS); v2f sneaknode_p2df(sneaknode_pf.X, sneaknode_pf.Z); f32 d_horiz_f = player_p2df.getDistanceFrom(sneaknode_p2df); f32 d_vert_f = fabs(sneaknode_pf.Y + BS*0.5 - position.Y); // Ignore if player is not on the same level (likely dropped) if(d_vert_f < 0.15*BS) min_distance_f = d_horiz_f; }*/ v3s16 new_sneak_node = m_sneak_node; for(s16 x=-1; x<=1; x++) for(s16 z=-1; z<=1; z++) { v3s16 p = pos_i_bottom + v3s16(x,0,z); v3f pf = intToFloat(p, BS); v2f node_p2df(pf.X, pf.Z); f32 distance_f = player_p2df.getDistanceFrom(node_p2df); f32 max_axis_distance_f = MYMAX( fabs(player_p2df.X-node_p2df.X), fabs(player_p2df.Y-node_p2df.Y)); if(distance_f > min_distance_f || max_axis_distance_f > 0.5*BS + sneak_max + 0.1*BS) continue; // The node to be sneaked on has to be walkable node = map->getNodeNoEx(p, &is_valid_position); if (!is_valid_position || nodemgr->get(node).walkable == false) continue; // And the node above it has to be nonwalkable node = map->getNodeNoEx(p + v3s16(0,1,0), &is_valid_position); if (!is_valid_position || nodemgr->get(node).walkable) { continue; } if (!physics_override_sneak_glitch) { node =map->getNodeNoEx(p + v3s16(0,2,0), &is_valid_position); if (!is_valid_position || nodemgr->get(node).walkable) continue; } min_distance_f = distance_f; new_sneak_node = p; } bool sneak_node_found = (min_distance_f < 100000.0 * BS * 0.9); m_sneak_node = new_sneak_node; m_sneak_node_exists = sneak_node_found; if (sneak_node_found) { f32 cb_max = 0; MapNode n = map->getNodeNoEx(m_sneak_node); std::vector<aabb3f> nodeboxes = n.getCollisionBoxes(nodemgr); for (std::vector<aabb3f>::iterator it = nodeboxes.begin(); it != nodeboxes.end(); ++it) { aabb3f box = *it; if (box.MaxEdge.Y > cb_max) cb_max = box.MaxEdge.Y; } m_sneak_node_bb_ymax = cb_max; } /* If sneaking, the player's collision box can be in air, so this has to be set explicitly */ if(sneak_node_found && control.sneak) touching_ground = true; } /* Set new position */ setPosition(position); /* Report collisions */ // Dont report if flying if(collision_info && !(g_settings->getBool("free_move") && fly_allowed)) { for(size_t i=0; i<result.collisions.size(); i++) { const CollisionInfo &info = result.collisions[i]; collision_info->push_back(info); } } if(!result.standing_on_object && !touching_ground_was && touching_ground) { MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround"); m_gamedef->event()->put(e); // Set camera impact value to be used for view bobbing camera_impact = getSpeed().Y * -1; } { camera_barely_in_ceiling = false; v3s16 camera_np = floatToInt(getEyePosition(), BS); MapNode n = map->getNodeNoEx(camera_np); if(n.getContent() != CONTENT_IGNORE){ if(nodemgr->get(n).walkable && nodemgr->get(n).solidness == 2){ camera_barely_in_ceiling = true; } } } /* Update the node last under the player */ m_old_node_below = floatToInt(position - v3f(0,BS/2,0), BS); m_old_node_below_type = nodemgr->get(map->getNodeNoEx(m_old_node_below)).name; /* Check properties of the node on which the player is standing */ const ContentFeatures &f = nodemgr->get(map->getNodeNoEx(getStandingNodePos())); // Determine if jumping is possible m_can_jump = touching_ground && !in_liquid; if(itemgroup_get(f.groups, "disable_jump")) m_can_jump = false; // Jump key pressed while jumping off from a bouncy block if (m_can_jump && control.jump && itemgroup_get(f.groups, "bouncy") && m_speed.Y >= -0.5 * BS) { float jumpspeed = movement_speed_jump * physics_override_jump; if (m_speed.Y > 1) { // Reduce boost when speed already is high m_speed.Y += jumpspeed / (1 + (m_speed.Y / 16 )); } else { m_speed.Y += jumpspeed; } setSpeed(m_speed); m_can_jump = false; } }
void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d, std::list<CollisionInfo> *collision_info) { INodeDefManager *nodemgr = m_gamedef->ndef(); v3f position = getPosition(); v3f old_speed = m_speed; // Copy parent position if local player is attached if(isAttached) { setPosition(overridePosition); return; } // Skip collision detection if noclip mode is used bool fly_allowed = m_gamedef->checkLocalPrivilege("fly"); bool noclip = m_gamedef->checkLocalPrivilege("noclip") && g_settings->getBool("noclip"); bool free_move = noclip && fly_allowed && g_settings->getBool("free_move"); if(free_move) { position += m_speed * dtime; setPosition(position); return; } /* Collision detection */ /* Check if player is in liquid (the oscillating value) */ try{ // If in liquid, the threshold of coming out is at higher y if(in_liquid) { v3s16 pp = floatToInt(position + v3f(0,BS*0.1,0), BS); in_liquid = nodemgr->get(map.getNode(pp).getContent()).isLiquid(); liquid_viscosity = nodemgr->get(map.getNode(pp).getContent()).liquid_viscosity; } // If not in liquid, the threshold of going in is at lower y else { v3s16 pp = floatToInt(position + v3f(0,BS*0.5,0), BS); in_liquid = nodemgr->get(map.getNode(pp).getContent()).isLiquid(); liquid_viscosity = nodemgr->get(map.getNode(pp).getContent()).liquid_viscosity; } } catch(InvalidPositionException &e) { in_liquid = false; } /* Check if player is in liquid (the stable value) */ try{ v3s16 pp = floatToInt(position + v3f(0,0,0), BS); in_liquid_stable = nodemgr->get(map.getNode(pp).getContent()).isLiquid(); } catch(InvalidPositionException &e) { in_liquid_stable = false; } /* Check if player is climbing */ try { v3s16 pp = floatToInt(position + v3f(0,0.5*BS,0), BS); v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS); is_climbing = ((nodemgr->get(map.getNode(pp).getContent()).climbable || nodemgr->get(map.getNode(pp2).getContent()).climbable) && !free_move); } catch(InvalidPositionException &e) { is_climbing = false; } /* Collision uncertainty radius Make it a bit larger than the maximum distance of movement */ //f32 d = pos_max_d * 1.1; // A fairly large value in here makes moving smoother f32 d = 0.15*BS; // This should always apply, otherwise there are glitches assert(d > pos_max_d); float player_radius = BS*0.30; float player_height = BS*1.55; // Maximum distance over border for sneaking f32 sneak_max = BS*0.4; /* If sneaking, keep in range from the last walked node and don't fall off from it */ if(control.sneak && m_sneak_node_exists && !(fly_allowed && g_settings->getBool("free_move")) && !in_liquid) { f32 maxd = 0.5*BS + sneak_max; v3f lwn_f = intToFloat(m_sneak_node, BS); position.X = rangelim(position.X, lwn_f.X-maxd, lwn_f.X+maxd); position.Z = rangelim(position.Z, lwn_f.Z-maxd, lwn_f.Z+maxd); if(!is_climbing) { f32 min_y = lwn_f.Y + 0.5*BS; if(position.Y < min_y) { position.Y = min_y; if(m_speed.Y < 0) m_speed.Y = 0; } } } /* Calculate player collision box (new and old) */ core::aabbox3d<f32> playerbox( -player_radius, 0.0, -player_radius, player_radius, player_height, player_radius ); float player_stepheight = touching_ground ? (BS*0.6) : (BS*0.2); v3f accel_f = v3f(0,0,0); collisionMoveResult result = collisionMoveSimple(&map, m_gamedef, pos_max_d, playerbox, player_stepheight, dtime, position, m_speed, accel_f); /* If the player's feet touch the topside of any node, this is set to true. Player is allowed to jump when this is true. */ bool touching_ground_was = touching_ground; touching_ground = result.touching_ground; //bool standing_on_unloaded = result.standing_on_unloaded; /* Check the nodes under the player to see from which node the player is sneaking from, if any. If the node from under the player has been removed, the player falls. */ v3s16 current_node = floatToInt(position - v3f(0,BS/2,0), BS); if(m_sneak_node_exists && nodemgr->get(map.getNodeNoEx(m_old_node_below)).name == "air" && m_old_node_below_type != "air") { // Old node appears to have been removed; that is, // it wasn't air before but now it is m_need_to_get_new_sneak_node = false; m_sneak_node_exists = false; } else if(nodemgr->get(map.getNodeNoEx(current_node)).name != "air") { // We are on something, so make sure to recalculate the sneak // node. m_need_to_get_new_sneak_node = true; } if(m_need_to_get_new_sneak_node) { v3s16 pos_i_bottom = floatToInt(position - v3f(0,BS/2,0), BS); v2f player_p2df(position.X, position.Z); f32 min_distance_f = 100000.0*BS; // If already seeking from some node, compare to it. /*if(m_sneak_node_exists) { v3f sneaknode_pf = intToFloat(m_sneak_node, BS); v2f sneaknode_p2df(sneaknode_pf.X, sneaknode_pf.Z); f32 d_horiz_f = player_p2df.getDistanceFrom(sneaknode_p2df); f32 d_vert_f = fabs(sneaknode_pf.Y + BS*0.5 - position.Y); // Ignore if player is not on the same level (likely dropped) if(d_vert_f < 0.15*BS) min_distance_f = d_horiz_f; }*/ v3s16 new_sneak_node = m_sneak_node; for(s16 x=-1; x<=1; x++) for(s16 z=-1; z<=1; z++) { v3s16 p = pos_i_bottom + v3s16(x,0,z); v3f pf = intToFloat(p, BS); v2f node_p2df(pf.X, pf.Z); f32 distance_f = player_p2df.getDistanceFrom(node_p2df); f32 max_axis_distance_f = MYMAX( fabs(player_p2df.X-node_p2df.X), fabs(player_p2df.Y-node_p2df.Y)); if(distance_f > min_distance_f || max_axis_distance_f > 0.5*BS + sneak_max + 0.1*BS) continue; try{ // The node to be sneaked on has to be walkable if(nodemgr->get(map.getNode(p)).walkable == false) continue; // And the node above it has to be nonwalkable if(nodemgr->get(map.getNode(p+v3s16(0,1,0))).walkable == true) continue; } catch(InvalidPositionException &e) { continue; } min_distance_f = distance_f; new_sneak_node = p; } bool sneak_node_found = (min_distance_f < 100000.0*BS*0.9); m_sneak_node = new_sneak_node; m_sneak_node_exists = sneak_node_found; /* If sneaking, the player's collision box can be in air, so this has to be set explicitly */ if(sneak_node_found && control.sneak) touching_ground = true; } /* Set new position */ setPosition(position); /* Report collisions */ bool bouncy_jump = false; // Dont report if flying if(collision_info && !(g_settings->getBool("free_move") && fly_allowed)) { for(size_t i=0; i<result.collisions.size(); i++){ const CollisionInfo &info = result.collisions[i]; collision_info->push_back(info); if(info.new_speed.Y - info.old_speed.Y > 0.1*BS && info.bouncy) bouncy_jump = true; } } if(bouncy_jump && control.jump){ m_speed.Y += movement_speed_jump*BS; touching_ground = false; MtEvent *e = new SimpleTriggerEvent("PlayerJump"); m_gamedef->event()->put(e); } if(!touching_ground_was && touching_ground){ MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround"); m_gamedef->event()->put(e); } { camera_barely_in_ceiling = false; v3s16 camera_np = floatToInt(getEyePosition(), BS); MapNode n = map.getNodeNoEx(camera_np); if(n.getContent() != CONTENT_IGNORE){ if(nodemgr->get(n).walkable && nodemgr->get(n).solidness == 2){ camera_barely_in_ceiling = true; } } } /* Update the node last under the player */ m_old_node_below = floatToInt(position - v3f(0,BS/2,0), BS); m_old_node_below_type = nodemgr->get(map.getNodeNoEx(m_old_node_below)).name; /* Check properties of the node on which the player is standing */ const ContentFeatures &f = nodemgr->get(map.getNodeNoEx(getStandingNodePos())); // Determine if jumping is possible m_can_jump = touching_ground && !in_liquid; if(itemgroup_get(f.groups, "disable_jump")) m_can_jump = false; }
// 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; }
void FireflySAO::step(float dtime, bool send_recommended) { ScopeProfiler sp2(g_profiler, "FireflySAO::step avg", SPT_AVG); assert(m_env); if(m_is_active == false) { if(m_inactive_interval.step(dtime, 0.5)==false) return; } /* The AI */ // Apply (less) gravity m_speed_f.Y -= dtime*3*BS; /* Move around if some player is close */ bool player_is_close = false; // Check connected players core::list<Player*> players = m_env->getPlayers(true); core::list<Player*>::Iterator i; for(i = players.begin(); i != players.end(); i++) { Player *player = *i; v3f playerpos = player->getPosition(); if(m_base_position.getDistanceFrom(playerpos) < BS*10.0) { player_is_close = true; break; } } m_is_active = player_is_close; if(player_is_close == false) { m_speed_f.X = 0; m_speed_f.Z = 0; } else { // Move around v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI)); f32 speed = BS/2; m_speed_f.X = speed * dir.X; m_speed_f.Z = speed * dir.Z; if(m_touching_ground && (m_oldpos - m_base_position).getLength() < dtime*speed/2) { m_counter1 -= dtime; if(m_counter1 < 0.0) { m_counter1 += 1.0; m_speed_f.Y = 5.0*BS; } } { m_counter2 -= dtime; if(m_counter2 < 0.0) { m_counter2 += (float)(myrand()%100)/100*3.0; m_yaw += ((float)(myrand()%200)-100)/100*180; m_yaw = wrapDegrees(m_yaw); } } } m_oldpos = m_base_position; /* Move it, with collision detection */ core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.); collisionMoveResult moveresult; // Maximum movement without glitches f32 pos_max_d = BS*0.25; // Limit speed if(m_speed_f.getLength()*dtime > pos_max_d) m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime); v3f pos_f = getBasePosition(); v3f pos_f_old = pos_f; IGameDef *gamedef = m_env->getGameDef(); moveresult = collisionMoveSimple(&m_env->getMap(), gamedef, pos_max_d, box, dtime, pos_f, m_speed_f); m_touching_ground = moveresult.touching_ground; setBasePosition(pos_f); if(send_recommended == false) return; if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS) { m_last_sent_position = pos_f; std::ostringstream os(std::ios::binary); // command (0 = update position) writeU8(os, 0); // pos writeV3F1000(os, m_base_position); // yaw writeF1000(os, m_yaw); // create message and add to list ActiveObjectMessage aom(getId(), false, os.str()); m_messages_out.push_back(aom); } }
void LuaEntitySAO::step(float dtime, bool send_recommended) { if(!m_properties_sent) { m_properties_sent = true; std::string str = getPropertyPacket(); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push_back(aom); } m_last_sent_position_timer += dtime; if(m_prop.physical){ core::aabbox3d<f32> box = m_prop.collisionbox; box.MinEdge *= BS; box.MaxEdge *= BS; collisionMoveResult moveresult; f32 pos_max_d = BS*0.25; // Distance per iteration f32 stepheight = 0; // Maximum climbable step height v3f p_pos = m_base_position; v3f p_velocity = m_velocity; v3f p_acceleration = m_acceleration; IGameDef *gamedef = m_env->getGameDef(); moveresult = collisionMoveSimple(&m_env->getMap(), gamedef, pos_max_d, box, stepheight, dtime, p_pos, p_velocity, p_acceleration); // Apply results m_base_position = p_pos; m_velocity = p_velocity; m_acceleration = p_acceleration; } else { m_base_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration; m_velocity += dtime * m_acceleration; } if(m_registered){ lua_State *L = m_env->getLua(); scriptapi_luaentity_step(L, m_id, dtime); } if(send_recommended == false) return; // TODO: force send when acceleration changes enough? float minchange = 0.2*BS; if(m_last_sent_position_timer > 1.0){ minchange = 0.01*BS; } else if(m_last_sent_position_timer > 0.2){ minchange = 0.05*BS; } float move_d = m_base_position.getDistanceFrom(m_last_sent_position); move_d += m_last_sent_move_precision; float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity); if(move_d > minchange || vel_d > minchange || fabs(m_yaw - m_last_sent_yaw) > 1.0){ sendPosition(true, false); } if(m_armor_groups_sent == false){ m_armor_groups_sent = true; std::string str = gob_cmd_update_armor_groups( m_armor_groups); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push_back(aom); } }
void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d, std::vector<CollisionInfo> *collision_info) { if (!collision_info || collision_info->empty()) { // Node below the feet, update each ClientEnvironment::step() m_standing_node = floatToInt(m_position, BS) - v3s16(0, 1, 0); } // Temporary option for old move code if (!physics_override_new_move) { old_move(dtime, env, pos_max_d, collision_info); return; } Map *map = &env->getMap(); INodeDefManager *nodemgr = m_client->ndef(); v3f position = getPosition(); // Copy parent position if local player is attached if (isAttached) { setPosition(overridePosition); return; } // Skip collision detection if noclip mode is used bool fly_allowed = m_client->checkLocalPrivilege("fly"); bool noclip = m_client->checkLocalPrivilege("noclip") && g_settings->getBool("noclip"); bool free_move = g_settings->getBool("free_move") && fly_allowed; if (noclip && free_move) { position += m_speed * dtime; setPosition(position); return; } /* Collision detection */ bool is_valid_position; MapNode node; v3s16 pp; /* Check if player is in liquid (the oscillating value) */ // If in liquid, the threshold of coming out is at higher y if (in_liquid) { pp = floatToInt(position + v3f(0,BS*0.1,0), BS); node = map->getNodeNoEx(pp, &is_valid_position); if (is_valid_position) { in_liquid = nodemgr->get(node.getContent()).isLiquid(); liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity; } else { in_liquid = false; } } // If not in liquid, the threshold of going in is at lower y else { pp = floatToInt(position + v3f(0,BS*0.5,0), BS); node = map->getNodeNoEx(pp, &is_valid_position); if (is_valid_position) { in_liquid = nodemgr->get(node.getContent()).isLiquid(); liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity; } else { in_liquid = false; } } /* Check if player is in liquid (the stable value) */ pp = floatToInt(position + v3f(0,0,0), BS); node = map->getNodeNoEx(pp, &is_valid_position); if (is_valid_position) { in_liquid_stable = nodemgr->get(node.getContent()).isLiquid(); } else { in_liquid_stable = false; } /* Check if player is climbing */ pp = floatToInt(position + v3f(0,0.5*BS,0), BS); v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS); node = map->getNodeNoEx(pp, &is_valid_position); bool is_valid_position2; MapNode node2 = map->getNodeNoEx(pp2, &is_valid_position2); if (!(is_valid_position && is_valid_position2)) { is_climbing = false; } else { is_climbing = (nodemgr->get(node.getContent()).climbable || nodemgr->get(node2.getContent()).climbable) && !free_move; } /* Collision uncertainty radius Make it a bit larger than the maximum distance of movement */ //f32 d = pos_max_d * 1.1; // A fairly large value in here makes moving smoother f32 d = 0.15*BS; // This should always apply, otherwise there are glitches sanity_check(d > pos_max_d); // Player object property step height is multiplied by BS in // /src/script/common/c_content.cpp and /src/content_sao.cpp float player_stepheight = (m_cao == nullptr) ? 0.0f : (touching_ground ? m_cao->getStepHeight() : (0.2f * BS)); // TODO this is a problematic hack. // Use a better implementation for autojump, or apply a custom stepheight // to all players, as this currently creates unintended special movement // abilities and advantages for Android players on a server. #ifdef __ANDROID__ if (touching_ground) player_stepheight += (0.6f * BS); #endif v3f accel_f = v3f(0,0,0); collisionMoveResult result = collisionMoveSimple(env, m_client, pos_max_d, m_collisionbox, player_stepheight, dtime, &position, &m_speed, accel_f); bool could_sneak = control.sneak && !free_move && !in_liquid && !is_climbing && physics_override_sneak; // Add new collisions to the vector if (collision_info && !free_move) { v3f diff = intToFloat(m_standing_node, BS) - position; f32 distance = diff.getLength(); // Force update each ClientEnvironment::step() bool is_first = collision_info->empty(); for (const auto &colinfo : result.collisions) { collision_info->push_back(colinfo); if (colinfo.type != COLLISION_NODE || colinfo.new_speed.Y != 0 || (could_sneak && m_sneak_node_exists)) continue; diff = intToFloat(colinfo.node_p, BS) - position; // Find nearest colliding node f32 len = diff.getLength(); if (is_first || len < distance) { m_standing_node = colinfo.node_p; distance = len; } } } /* If the player's feet touch the topside of any node, this is set to true. Player is allowed to jump when this is true. */ bool touching_ground_was = touching_ground; touching_ground = result.touching_ground; bool sneak_can_jump = false; // Max. distance (X, Z) over border for sneaking determined by collision box // * 0.49 to keep the center just barely on the node v3f sneak_max = m_collisionbox.getExtent() * 0.49; if (m_sneak_ladder_detected) { // restore legacy behaviour (this makes the m_speed.Y hack necessary) sneak_max = v3f(0.4 * BS, 0, 0.4 * BS); } /* If sneaking, keep on top of last walked node and don't fall off */ if (could_sneak && m_sneak_node_exists) { const v3f sn_f = intToFloat(m_sneak_node, BS); const v3f bmin = sn_f + m_sneak_node_bb_top.MinEdge; const v3f bmax = sn_f + m_sneak_node_bb_top.MaxEdge; const v3f old_pos = position; const v3f old_speed = m_speed; f32 y_diff = bmax.Y - position.Y; m_standing_node = m_sneak_node; // (BS * 0.6f) is the basic stepheight while standing on ground if (y_diff < BS * 0.6f) { // Only center player when they're on the node position.X = rangelim(position.X, bmin.X - sneak_max.X, bmax.X + sneak_max.X); position.Z = rangelim(position.Z, bmin.Z - sneak_max.Z, bmax.Z + sneak_max.Z); if (position.X != old_pos.X) m_speed.X = 0; if (position.Z != old_pos.Z) m_speed.Z = 0; } if (y_diff > 0 && m_speed.Y < 0 && (physics_override_sneak_glitch || y_diff < BS * 0.6f)) { // Move player to the maximal height when falling or when // the ledge is climbed on the next step. position.Y = bmax.Y; m_speed.Y = 0; } // Allow jumping on node edges while sneaking if (m_speed.Y == 0 || m_sneak_ladder_detected) sneak_can_jump = true; if (collision_info && m_speed.Y - old_speed.Y > BS) { // Collide with sneak node, report fall damage CollisionInfo sn_info; sn_info.node_p = m_sneak_node; sn_info.old_speed = old_speed; sn_info.new_speed = m_speed; collision_info->push_back(sn_info); } } /* Find the next sneak node if necessary */ bool new_sneak_node_exists = false; if (could_sneak) new_sneak_node_exists = updateSneakNode(map, position, sneak_max); /* Set new position but keep sneak node set */ setPosition(position); m_sneak_node_exists = new_sneak_node_exists; /* Report collisions */ if(!result.standing_on_object && !touching_ground_was && touching_ground) { MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround"); m_client->event()->put(e); // Set camera impact value to be used for view bobbing camera_impact = getSpeed().Y * -1; } { camera_barely_in_ceiling = false; v3s16 camera_np = floatToInt(getEyePosition(), BS); MapNode n = map->getNodeNoEx(camera_np); if(n.getContent() != CONTENT_IGNORE){ if(nodemgr->get(n).walkable && nodemgr->get(n).solidness == 2){ camera_barely_in_ceiling = true; } } } /* Check properties of the node on which the player is standing */ const ContentFeatures &f = nodemgr->get(map->getNodeNoEx(m_standing_node)); // Determine if jumping is possible m_can_jump = (touching_ground && !in_liquid && !is_climbing) || sneak_can_jump; if (itemgroup_get(f.groups, "disable_jump")) m_can_jump = false; // Jump key pressed while jumping off from a bouncy block if (m_can_jump && control.jump && itemgroup_get(f.groups, "bouncy") && m_speed.Y >= -0.5 * BS) { float jumpspeed = movement_speed_jump * physics_override_jump; if (m_speed.Y > 1) { // Reduce boost when speed already is high m_speed.Y += jumpspeed / (1 + (m_speed.Y / 16 )); } else { m_speed.Y += jumpspeed; } setSpeed(m_speed); m_can_jump = false; } }