diff options
Diffstat (limited to 'scene/3d')
-rw-r--r-- | scene/3d/path.cpp | 39 | ||||
-rw-r--r-- | scene/3d/physics_body.cpp | 47 | ||||
-rw-r--r-- | scene/3d/spatial.cpp | 3 |
3 files changed, 55 insertions, 34 deletions
diff --git a/scene/3d/path.cpp b/scene/3d/path.cpp index d55c795d38..62684bd1e1 100644 --- a/scene/3d/path.cpp +++ b/scene/3d/path.cpp @@ -111,18 +111,15 @@ void PathFollow::_update_transform() { return; } float bi = c->get_bake_interval(); - float o = offset; float o_next = offset + bi; if (loop) { - o = Math::fposmod(o, bl); o_next = Math::fposmod(o_next, bl); } else if (rotation_mode == ROTATION_ORIENTED && o_next >= bl) { - o = bl - bi; o_next = bl; } - Vector3 pos = c->interpolate_baked(o, cubic); + Vector3 pos = c->interpolate_baked(offset, cubic); Transform t = get_transform(); // Vector3 pos_offset = Vector3(h_offset, v_offset, 0); not used in all cases // will be replaced by "Vector3(h_offset, v_offset, 0)" where it was formerly used @@ -136,9 +133,9 @@ void PathFollow::_update_transform() { else forward.normalize(); - Vector3 up = c->interpolate_baked_up_vector(o, true); + Vector3 up = c->interpolate_baked_up_vector(offset, true); - if (o_next < o) { + if (o_next < offset) { Vector3 up1 = c->interpolate_baked_up_vector(o_next, true); Vector3 axis = up.cross(up1); @@ -166,8 +163,8 @@ void PathFollow::_update_transform() { t.origin = pos; - Vector3 t_prev = (pos - c->interpolate_baked(o - delta_offset, cubic)).normalized(); - Vector3 t_cur = (c->interpolate_baked(o + delta_offset, cubic) - pos).normalized(); + Vector3 t_prev = (pos - c->interpolate_baked(offset - delta_offset, cubic)).normalized(); + Vector3 t_cur = (c->interpolate_baked(offset + delta_offset, cubic) - pos).normalized(); Vector3 axis = t_prev.cross(t_cur); float dot = t_prev.dot(t_cur); @@ -190,7 +187,7 @@ void PathFollow::_update_transform() { } // do the additional tilting - float tilt_angle = c->interpolate_baked_tilt(o); + float tilt_angle = c->interpolate_baked_tilt(offset); Vector3 tilt_axis = t_cur; // not sure what tilt is supposed to do, is this correct?? if (likely(!Math::is_zero_approx(Math::abs(tilt_angle)))) { @@ -256,7 +253,7 @@ void PathFollow::_validate_property(PropertyInfo &property) const { if (path && path->get_curve().is_valid()) max = path->get_curve()->get_baked_length(); - property.hint_string = "0," + rtos(max) + ",0.01,or_greater"; + property.hint_string = "0," + rtos(max) + ",0.01,or_lesser"; } } @@ -300,8 +297,8 @@ void PathFollow::_bind_methods() { ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow::set_loop); ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow::has_loop); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "offset", PROPERTY_HINT_RANGE, "0,10000,0.01,or_greater"), "set_offset", "get_offset"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "unit_offset", PROPERTY_HINT_RANGE, "0,1,0.0001,or_greater", PROPERTY_USAGE_EDITOR), "set_unit_offset", "get_unit_offset"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "offset", PROPERTY_HINT_RANGE, "0,10000,0.01,or_lesser"), "set_offset", "get_offset"); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "unit_offset", PROPERTY_HINT_RANGE, "0,1,0.0001,or_lesser", PROPERTY_USAGE_EDITOR), "set_unit_offset", "get_unit_offset"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "h_offset"), "set_h_offset", "get_h_offset"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "v_offset"), "set_v_offset", "get_v_offset"); ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_mode", PROPERTY_HINT_ENUM, "None,Y,XY,XYZ,Oriented"), "set_rotation_mode", "get_rotation_mode"); @@ -319,8 +316,24 @@ void PathFollow::set_offset(float p_offset) { delta_offset = p_offset - offset; offset = p_offset; - if (path) + if (path) { + if (path->get_curve().is_valid() && path->get_curve()->get_baked_length()) { + float path_length = path->get_curve()->get_baked_length(); + + if (loop) { + while (offset > path_length) + offset -= path_length; + + while (offset < 0) + offset += path_length; + + } else { + offset = CLAMP(offset, 0, path_length); + } + } + _update_transform(); + } _change_notify("offset"); _change_notify("unit_offset"); } diff --git a/scene/3d/physics_body.cpp b/scene/3d/physics_body.cpp index a107c3bf7a..6049b6cdb4 100644 --- a/scene/3d/physics_body.cpp +++ b/scene/3d/physics_body.cpp @@ -1142,25 +1142,34 @@ bool KinematicBody::move_and_collide(const Vector3 &p_motion, bool p_infinite_in Vector3 KinematicBody::move_and_slide(const Vector3 &p_linear_velocity, const Vector3 &p_floor_direction, bool p_stop_on_slope, int p_max_slides, float p_floor_max_angle, bool p_infinite_inertia) { - Vector3 lv = p_linear_velocity; + Vector3 body_velocity = p_linear_velocity; + Vector3 body_velocity_normal = body_velocity.normalized(); for (int i = 0; i < 3; i++) { if (locked_axis & (1 << i)) { - lv[i] = 0; + body_velocity[i] = 0; + } + } + + Vector3 current_floor_velocity = floor_velocity; + if (on_floor && on_floor_body.is_valid()) { + //this approach makes sure there is less delay between the actual body velocity and the one we saved + PhysicsDirectBodyState *bs = PhysicsServer::get_singleton()->body_get_direct_state(on_floor_body); + if (bs) { + current_floor_velocity = bs->get_linear_velocity(); } } // Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky - Vector3 motion = (floor_velocity + lv) * (Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time()); + Vector3 motion = (current_floor_velocity + body_velocity) * (Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time()); on_floor = false; + on_floor_body = RID(); on_ceiling = false; on_wall = false; colliders.clear(); floor_velocity = Vector3(); - Vector3 lv_n = p_linear_velocity.normalized(); - while (p_max_slides) { Collision collision; @@ -1187,7 +1196,6 @@ Vector3 KinematicBody::move_and_slide(const Vector3 &p_linear_velocity, const Ve colliders.push_back(collision); motion = collision.remainder; - bool is_on_slope = false; if (p_floor_direction == Vector3()) { //all is a wall on_wall = true; @@ -1199,16 +1207,13 @@ Vector3 KinematicBody::move_and_slide(const Vector3 &p_linear_velocity, const Ve floor_velocity = collision.collider_vel; if (p_stop_on_slope) { - if ((lv_n + p_floor_direction).length() < 0.01 && collision.travel.length() < 1) { + if ((body_velocity_normal + p_floor_direction).length() < 0.01 && collision.travel.length() < 1) { Transform gt = get_global_transform(); gt.origin -= collision.travel.slide(p_floor_direction); set_global_transform(gt); return Vector3(); } } - - is_on_slope = true; - } else if (Math::acos(collision.normal.dot(-p_floor_direction)) <= p_floor_max_angle + FLOOR_ANGLE_THRESHOLD) { //ceiling on_ceiling = true; } else { @@ -1216,18 +1221,12 @@ Vector3 KinematicBody::move_and_slide(const Vector3 &p_linear_velocity, const Ve } } - if (p_stop_on_slope && is_on_slope) { - motion = motion.slide(p_floor_direction); - lv = lv.slide(p_floor_direction); - } else { - Vector3 n = collision.normal; - motion = motion.slide(n); - lv = lv.slide(n); - } + motion = motion.slide(collision.normal); + body_velocity = body_velocity.slide(collision.normal); for (int j = 0; j < 3; j++) { if (locked_axis & (1 << j)) { - lv[j] = 0; + body_velocity[j] = 0; } } } @@ -1239,7 +1238,7 @@ Vector3 KinematicBody::move_and_slide(const Vector3 &p_linear_velocity, const Ve --p_max_slides; } - return lv; + return body_velocity; } Vector3 KinematicBody::move_and_slide_with_snap(const Vector3 &p_linear_velocity, const Vector3 &p_snap, const Vector3 &p_floor_direction, bool p_stop_on_slope, int p_max_slides, float p_floor_max_angle, bool p_infinite_inertia) { @@ -2161,6 +2160,9 @@ void PhysicalBone::_notification(int p_what) { update_bone_id(); reset_to_rest_position(); _reset_physics_simulation_state(); + if (!joint.is_valid() && joint_data) { + _reload_joint(); + } break; case NOTIFICATION_EXIT_TREE: if (parent_skeleton) { @@ -2169,7 +2171,10 @@ void PhysicalBone::_notification(int p_what) { } } parent_skeleton = NULL; - update_bone_id(); + if (joint.is_valid()) { + PhysicsServer::get_singleton()->free(joint); + joint = RID(); + } break; case NOTIFICATION_TRANSFORM_CHANGED: if (Engine::get_singleton()->is_editor_hint()) { diff --git a/scene/3d/spatial.cpp b/scene/3d/spatial.cpp index 9a659ef4af..043d5d5548 100644 --- a/scene/3d/spatial.cpp +++ b/scene/3d/spatial.cpp @@ -322,6 +322,7 @@ Transform Spatial::get_relative_transform(const Node *p_parent) const { void Spatial::set_translation(const Vector3 &p_translation) { data.local_transform.origin = p_translation; + _change_notify("transform"); _propagate_transform_changed(this); if (data.notify_local_transform) { notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); @@ -337,6 +338,7 @@ void Spatial::set_rotation(const Vector3 &p_euler_rad) { data.rotation = p_euler_rad; data.dirty |= DIRTY_LOCAL; + _change_notify("transform"); _propagate_transform_changed(this); if (data.notify_local_transform) { notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); @@ -357,6 +359,7 @@ void Spatial::set_scale(const Vector3 &p_scale) { data.scale = p_scale; data.dirty |= DIRTY_LOCAL; + _change_notify("transform"); _propagate_transform_changed(this); if (data.notify_local_transform) { notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED); |