Fix PathFollow rotations.

Replaced the lookahead (which is problematic) with exact delta in offset. Also made some other minor fixes and improvements.

Fixes #12258.
This commit is contained in:
Ferenc Arn 2017-10-21 11:26:55 -04:00
parent 632459a46b
commit 0b89b90b36
2 changed files with 25 additions and 32 deletions

View file

@ -85,9 +85,15 @@ void PathFollow::_update_transform() {
if (!c.is_valid()) if (!c.is_valid())
return; return;
if (delta_offset == 0) {
return;
}
float o = offset; float o = offset;
if (loop)
if (loop) {
o = Math::fposmod(o, c->get_baked_length()); o = Math::fposmod(o, c->get_baked_length());
}
Vector3 pos = c->interpolate_baked(o, cubic); Vector3 pos = c->interpolate_baked(o, cubic);
Transform t = get_transform(); Transform t = get_transform();
@ -101,14 +107,14 @@ void PathFollow::_update_transform() {
// see C. Dougan, The Parallel Transport Frame, Game Programming Gems 2 for example // see C. Dougan, The Parallel Transport Frame, Game Programming Gems 2 for example
// for a discussion about why not Frenet frame. // for a discussion about why not Frenet frame.
Vector3 t_prev = pos - c->interpolate_baked(o - lookahead, cubic); Vector3 t_prev = (pos - c->interpolate_baked(o - delta_offset, cubic)).normalized();
Vector3 t_cur = c->interpolate_baked(o + lookahead, cubic) - pos; Vector3 t_cur = (c->interpolate_baked(o + delta_offset, cubic) - pos).normalized();
Vector3 axis = t_prev.cross(t_cur); Vector3 axis = t_prev.cross(t_cur);
float dot = t_prev.normalized().dot(t_cur.normalized()); float dot = t_prev.dot(t_cur);
float angle = Math::acos(CLAMP(dot, -1, 1)); float angle = Math::acos(CLAMP(dot, -1, 1));
if (axis.length() > CMP_EPSILON && angle > CMP_EPSILON) { if (likely(Math::abs(angle) > CMP_EPSILON)) {
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;
@ -116,27 +122,31 @@ void PathFollow::_update_transform() {
} 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 OK // all components are allowed
} }
t.rotate_basis(axis.normalized(), angle); if (likely(axis.length() > CMP_EPSILON)) {
t.rotate_basis(axis.normalized(), angle);
}
} }
// do the additional tilting // do the additional tilting
float tilt_angle = c->interpolate_baked_tilt(o); float tilt_angle = c->interpolate_baked_tilt(o);
Vector3 tilt_axis = t_cur; // is this correct?? Vector3 tilt_axis = t_cur; // not sure what tilt is supposed to do, is this correct??
if (tilt_axis.length() > CMP_EPSILON && tilt_angle > CMP_EPSILON) { if (likely(Math::abs(tilt_angle) > CMP_EPSILON)) {
if (rotation_mode == ROTATION_Y) { if (rotation_mode == ROTATION_Y) {
tilt_axis.x = 0; tilt_axis.x = 0;
tilt_axis.z = 0; tilt_axis.z = 0;
} else if (rotation_mode == ROTATION_XY) { } else if (rotation_mode == ROTATION_XY) {
tilt_axis.z = 0; tilt_axis.z = 0;
} else if (rotation_mode == ROTATION_XYZ) { } else if (rotation_mode == ROTATION_XYZ) {
// all components are OK // all components are allowed
} }
t.rotate_basis(tilt_axis.normalized(), tilt_angle); if (likely(tilt_axis.length() > CMP_EPSILON)) {
t.rotate_basis(tilt_axis.normalized(), tilt_angle);
}
} }
t.translate(pos_offset); t.translate(pos_offset);
@ -195,8 +205,6 @@ bool PathFollow::_set(const StringName &p_name, const Variant &p_value) {
set_cubic_interpolation(p_value); set_cubic_interpolation(p_value);
} else if (String(p_name) == "loop") { } else if (String(p_name) == "loop") {
set_loop(p_value); set_loop(p_value);
} else if (String(p_name) == "lookahead") {
set_lookahead(p_value);
} else } else
return false; return false;
@ -219,8 +227,6 @@ bool PathFollow::_get(const StringName &p_name, Variant &r_ret) const {
r_ret = cubic; r_ret = cubic;
} else if (String(p_name) == "loop") { } else if (String(p_name) == "loop") {
r_ret = loop; r_ret = loop;
} else if (String(p_name) == "lookahead") {
r_ret = lookahead;
} else } else
return false; return false;
@ -238,7 +244,6 @@ void PathFollow::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back(PropertyInfo(Variant::INT, "rotation_mode", PROPERTY_HINT_ENUM, "None,Y,XY,XYZ")); p_list->push_back(PropertyInfo(Variant::INT, "rotation_mode", PROPERTY_HINT_ENUM, "None,Y,XY,XYZ"));
p_list->push_back(PropertyInfo(Variant::BOOL, "cubic_interp")); p_list->push_back(PropertyInfo(Variant::BOOL, "cubic_interp"));
p_list->push_back(PropertyInfo(Variant::BOOL, "loop")); p_list->push_back(PropertyInfo(Variant::BOOL, "loop"));
p_list->push_back(PropertyInfo(Variant::REAL, "lookahead", PROPERTY_HINT_RANGE, "0.001,1024.0,0.001"));
} }
void PathFollow::_bind_methods() { void PathFollow::_bind_methods() {
@ -271,8 +276,9 @@ void PathFollow::_bind_methods() {
} }
void PathFollow::set_offset(float p_offset) { void PathFollow::set_offset(float p_offset) {
delta_offset = p_offset - offset;
offset = p_offset; offset = p_offset;
if (path) if (path)
_update_transform(); _update_transform();
_change_notify("offset"); _change_notify("offset");
@ -322,16 +328,6 @@ float PathFollow::get_unit_offset() const {
return 0; return 0;
} }
void PathFollow::set_lookahead(float p_lookahead) {
lookahead = p_lookahead;
}
float PathFollow::get_lookahead() const {
return lookahead;
}
void PathFollow::set_rotation_mode(RotationMode p_rotation_mode) { void PathFollow::set_rotation_mode(RotationMode p_rotation_mode) {
rotation_mode = p_rotation_mode; rotation_mode = p_rotation_mode;
@ -356,11 +352,11 @@ bool PathFollow::has_loop() const {
PathFollow::PathFollow() { PathFollow::PathFollow() {
offset = 0; offset = 0;
delta_offset = 0;
h_offset = 0; h_offset = 0;
v_offset = 0; v_offset = 0;
path = NULL; path = NULL;
rotation_mode = ROTATION_XYZ; rotation_mode = ROTATION_XYZ;
cubic = true; cubic = true;
loop = true; loop = true;
lookahead = 0.1;
} }

View file

@ -67,10 +67,10 @@ public:
private: private:
Path *path; Path *path;
real_t delta_offset; // change in offset since last _update_transform
real_t offset; real_t offset;
real_t h_offset; real_t h_offset;
real_t v_offset; real_t v_offset;
real_t lookahead;
bool cubic; bool cubic;
bool loop; bool loop;
RotationMode rotation_mode; RotationMode rotation_mode;
@ -98,9 +98,6 @@ public:
void set_unit_offset(float p_unit_offset); void set_unit_offset(float p_unit_offset);
float get_unit_offset() const; float get_unit_offset() const;
void set_lookahead(float p_lookahead);
float get_lookahead() const;
void set_loop(bool p_loop); void set_loop(bool p_loop);
bool has_loop() const; bool has_loop() const;