Fix repeated updates of PathFollow3D Transform

Add optional parameter to specify whether applying rotation to the
PathFollow3D's Transform is necessary, preventing erroneous updates.
This commit is contained in:
Maganty Rushyendra 2020-07-08 10:06:02 +08:00
parent d0e0a19e4d
commit be3a1769fe
2 changed files with 38 additions and 36 deletions

View file

@ -84,7 +84,7 @@ void Path3D::_bind_methods() {
////////////// //////////////
void PathFollow3D::_update_transform() { void PathFollow3D::_update_transform(bool p_update_xyz_rot) {
if (!path) { if (!path) {
return; return;
} }
@ -156,45 +156,47 @@ void PathFollow3D::_update_transform() {
t.origin = pos; t.origin = pos;
Vector3 t_prev = (pos - c->interpolate_baked(offset - delta_offset, cubic)).normalized(); if (p_update_xyz_rot) { // Only update rotation if some parameter has changed - i.e. not on addition to scene tree
Vector3 t_cur = (c->interpolate_baked(offset + 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); Vector3 axis = t_prev.cross(t_cur);
float dot = t_prev.dot(t_cur); float dot = t_prev.dot(t_cur);
float angle = Math::acos(CLAMP(dot, -1, 1)); float angle = Math::acos(CLAMP(dot, -1, 1));
if (likely(!Math::is_zero_approx(angle))) { if (likely(!Math::is_zero_approx(angle))) {
if (rotation_mode == ROTATION_Y) { if (rotation_mode == ROTATION_Y) {
// assuming we're referring to global Y-axis. is this correct? // assuming we're referring to global Y-axis. is this correct?
axis.x = 0; axis.x = 0;
axis.z = 0; axis.z = 0;
} else if (rotation_mode == ROTATION_XY) { } else if (rotation_mode == ROTATION_XY) {
axis.z = 0; axis.z = 0;
} else if (rotation_mode == ROTATION_XYZ) { } else if (rotation_mode == ROTATION_XYZ) {
// all components are allowed // all components are allowed
}
if (likely(!Math::is_zero_approx(axis.length()))) {
t.rotate_basis(axis.normalized(), angle);
}
} }
if (likely(!Math::is_zero_approx(axis.length()))) { // do the additional tilting
t.rotate_basis(axis.normalized(), angle); float tilt_angle = c->interpolate_baked_tilt(offset);
} Vector3 tilt_axis = t_cur; // not sure what tilt is supposed to do, is this correct??
}
// do the additional tilting if (likely(!Math::is_zero_approx(Math::abs(tilt_angle)))) {
float tilt_angle = c->interpolate_baked_tilt(offset); if (rotation_mode == ROTATION_Y) {
Vector3 tilt_axis = t_cur; // not sure what tilt is supposed to do, is this correct?? tilt_axis.x = 0;
tilt_axis.z = 0;
} else if (rotation_mode == ROTATION_XY) {
tilt_axis.z = 0;
} else if (rotation_mode == ROTATION_XYZ) {
// all components are allowed
}
if (likely(!Math::is_zero_approx(Math::abs(tilt_angle)))) { if (likely(!Math::is_zero_approx(tilt_axis.length()))) {
if (rotation_mode == ROTATION_Y) { t.rotate_basis(tilt_axis.normalized(), tilt_angle);
tilt_axis.x = 0; }
tilt_axis.z = 0;
} else if (rotation_mode == ROTATION_XY) {
tilt_axis.z = 0;
} else if (rotation_mode == ROTATION_XYZ) {
// all components are allowed
}
if (likely(!Math::is_zero_approx(tilt_axis.length()))) {
t.rotate_basis(tilt_axis.normalized(), tilt_angle);
} }
} }
@ -213,7 +215,7 @@ void PathFollow3D::_notification(int p_what) {
if (parent) { if (parent) {
path = Object::cast_to<Path3D>(parent); path = Object::cast_to<Path3D>(parent);
if (path) { if (path) {
_update_transform(); _update_transform(false);
} }
} }

View file

@ -75,7 +75,7 @@ private:
bool loop; bool loop;
RotationMode rotation_mode; RotationMode rotation_mode;
void _update_transform(); void _update_transform(bool p_update_xyz_rot = true);
protected: protected:
virtual void _validate_property(PropertyInfo &property) const; virtual void _validate_property(PropertyInfo &property) const;