Merge pull request #29125 from rodolforg/fix_spatial_look_at_affecting_scale

fix un-scaling in Spatial::look_at_from_position
This commit is contained in:
Rémi Verschelde 2019-05-23 17:01:40 +02:00 committed by GitHub
commit 7c73a741f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 15 deletions

View File

@ -676,28 +676,29 @@ void Spatial::set_identity() {
void Spatial::look_at(const Vector3 &p_target, const Vector3 &p_up) { void Spatial::look_at(const Vector3 &p_target, const Vector3 &p_up) {
Transform lookat(get_global_transform()); Vector3 origin(get_global_transform().origin);
if (lookat.origin == p_target) { look_at_from_position(origin, p_target, p_up);
ERR_EXPLAIN("Node origin and target are in the same position, look_at() failed");
ERR_FAIL();
}
if (p_up.cross(p_target - lookat.origin) == Vector3()) {
ERR_EXPLAIN("Up vector and direction between node origin and target are aligned, look_at() failed");
ERR_FAIL();
}
Vector3 original_scale(lookat.basis.get_scale());
lookat = lookat.looking_at(p_target, p_up);
// as basis was normalized, we just need to apply original scale back
lookat.basis.scale(original_scale);
set_global_transform(lookat);
} }
void Spatial::look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up) { void Spatial::look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up) {
if (p_pos == p_target) {
ERR_EXPLAIN("Node origin and target are in the same position, look_at() failed");
ERR_FAIL();
}
if (p_up.cross(p_target - p_pos) == Vector3()) {
ERR_EXPLAIN("Up vector and direction between node origin and target are aligned, look_at() failed");
ERR_FAIL();
}
Transform lookat; Transform lookat;
lookat.origin = p_pos; lookat.origin = p_pos;
Vector3 original_scale(get_global_transform().basis.get_scale());
lookat = lookat.looking_at(p_target, p_up); lookat = lookat.looking_at(p_target, p_up);
// as basis was normalized, we just need to apply original scale back
lookat.basis.scale(original_scale);
set_global_transform(lookat); set_global_transform(lookat);
} }