Skip to content

Fix transform inversion to handle scaling correctly #1823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/variant/transform2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@
namespace godot {

void Transform2D::invert() {
// FIXME: this function assumes the basis is a rotation matrix, with no scaling.
// Transform2D::affine_inverse can handle matrices with scaling, so GDScript should eventually use that.
SWAP(columns[0][1], columns[1][0]);
// Use proper affine inversion that handles scaling
real_t det = determinant();
#ifdef MATH_CHECKS
ERR_FAIL_COND(det == 0);
#endif
real_t idet = 1.0f / det;

SWAP(columns[0][0], columns[1][1]);
columns[0] *= Vector2(idet, -idet);
columns[1] *= Vector2(-idet, idet);

columns[2] = basis_xform(-columns[2]);
}

Expand Down
4 changes: 1 addition & 3 deletions src/variant/transform3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ Transform3D Transform3D::affine_inverse() const {
}

void Transform3D::invert() {
basis.transpose();
basis.invert();
origin = basis.xform(-origin);
}

Transform3D Transform3D::inverse() const {
// FIXME: this function assumes the basis is a rotation matrix, with no scaling.
// Transform3D::affine_inverse can handle matrices with scaling, so GDScript should eventually use that.
Transform3D ret = *this;
ret.invert();
return ret;
Expand Down
Loading