Skip to content

Commit

Permalink
Fix Basis(), Basis#isOrthogonal, Basis#isEqualApprox (#330)
Browse files Browse the repository at this point in the history
* fix Basis::isEqualApprox inverted

* fix Basis() not identity

* fix Basis::isOrthogonal()

* test jvm Basis(), Basis::getRotationQuat(), Basis::isEqualApprox(...)

* fix test_jvm_basis_get_rotation_quat

Co-authored-by: Richard Treier <[email protected]>
  • Loading branch information
richardtreier and Richard Treier authored Jul 5, 2022
1 parent 5e2fdc3 commit 7573ef1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
18 changes: 17 additions & 1 deletion harness/tests/src/main/kotlin/godot/tests/coretypes/BasisTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import godot.Node
import godot.annotation.RegisterClass
import godot.annotation.RegisterFunction
import godot.core.Basis
import godot.core.Quat
import godot.core.Vector3

@RegisterClass
Expand All @@ -17,4 +18,19 @@ class BasisTest : Node() {
basis[index] = vector3
return basis
}
}

@RegisterFunction
fun getRotationQuat(basis: Basis): Quat {
return basis.getRotationQuat()
}

@RegisterFunction
fun newJvmBasis(): Basis {
return Basis()
}

@RegisterFunction
fun isEqualApproxJvm(a: Basis, b: Basis): Boolean {
return a.isEqualApprox(b)
}
}
42 changes: 42 additions & 0 deletions harness/tests/test/unit/test_coretypes_basis.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,45 @@ func test_should_set_right_vector3_using_operator_set():
jvm_basis = basis_test.set(jvm_basis, 2, Vector3(50, 90, 70))
assert_eq(engine_basis, jvm_basis, "Basis set operator on JVM side should be same as gdscript one.")
basis_test.free()

func test_jvm_basis_get_rotation_quat():
# Tests previous issues with Basis#getQuat()
# see https://github.com/utopia-rise/godot-kotlin-jvm/pull/330
var basis_test = godot_tests_coretypes_BasisTest.new()
var basis = Basis.IDENTITY \
.rotated(Vector3(1, 0, 0), 5.0 * PI / 180.0) \
.rotated(Vector3(0, 1, 0), 5.0 * PI / 180.0) \
.rotated(Vector3(0, 0, 1), 5.0 * PI / 180.0) \
.scaled(Vector3(1, 2, 3))

var jvm_quat = basis.get_rotation_quat()
var engine_quat = basis_test.get_rotation_quat(basis)
assert_true(engine_quat.is_equal_approx(jvm_quat), "Quat from Basis in JVM should equal expected Quat")
basis_test.free()

func test_new_jvm_basis_should_be_unity():
# Tests previous issues with Basis#getQuat()
# see https://github.com/utopia-rise/godot-kotlin-jvm/pull/330
var basis_test = godot_tests_coretypes_BasisTest.new()
var jvm_basis = basis_test.new_jvm_basis()
var engine_basis = Basis(Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1))
assert_eq(engine_basis, jvm_basis, "Basis() on JVM side should be same as unity gdscript one.")
basis_test.free()

func test_jvm_basis_is_equal_approx():
# Tests previous issues with Basis#getQuat()
# see https://github.com/utopia-rise/godot-kotlin-jvm/pull/330
var basis_test = godot_tests_coretypes_BasisTest.new()
var ref = Basis(Vector3(1, 2, 3), Vector3(6, 8, 10), Vector3(5, 9, 7))
assert_true(basis_test.is_equal_approx_jvm(ref, Basis(Vector3(1, 2, 3), Vector3(6, 8, 10), Vector3(5, 9, 7))))
assert_false(basis_test.is_equal_approx_jvm(ref, Basis(Vector3(0, 2, 3), Vector3(6, 8, 10), Vector3(5, 9, 7))))
assert_false(basis_test.is_equal_approx_jvm(ref, Basis(Vector3(1, 0, 3), Vector3(6, 8, 10), Vector3(5, 9, 7))))
assert_false(basis_test.is_equal_approx_jvm(ref, Basis(Vector3(1, 2, 0), Vector3(6, 8, 10), Vector3(5, 9, 7))))
assert_false(basis_test.is_equal_approx_jvm(ref, Basis(Vector3(1, 2, 3), Vector3(0, 8, 10), Vector3(5, 9, 7))))
assert_false(basis_test.is_equal_approx_jvm(ref, Basis(Vector3(1, 2, 3), Vector3(6, 0, 10), Vector3(5, 9, 7))))
assert_false(basis_test.is_equal_approx_jvm(ref, Basis(Vector3(1, 2, 3), Vector3(6, 8, 0), Vector3(5, 9, 7))))
assert_false(basis_test.is_equal_approx_jvm(ref, Basis(Vector3(1, 2, 3), Vector3(6, 8, 10), Vector3(0, 9, 7))))
assert_false(basis_test.is_equal_approx_jvm(ref, Basis(Vector3(1, 2, 3), Vector3(6, 8, 10), Vector3(5, 0, 7))))
assert_false(basis_test.is_equal_approx_jvm(ref, Basis(Vector3(1, 2, 3), Vector3(6, 8, 10), Vector3(5, 9, 0))))
basis_test.free()

22 changes: 11 additions & 11 deletions kt/godot-library/src/main/kotlin/godot/core/Basis.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Basis() : CoreType {
_y.y = 1.0
_y.z = 0.0
_z.x = 0.0
_z.y = 0.1
_z.y = 0.0
_z.z = 1.0
}

Expand Down Expand Up @@ -277,7 +277,7 @@ class Basis() : CoreType {

private fun isOrthogonal(): Boolean {
val id = Basis()
val m = this.transposed()
val m = this.transposed().times(this)
return m.isEqualApprox(id)
}

Expand Down Expand Up @@ -418,17 +418,17 @@ class Basis() : CoreType {
*
*/
fun isEqualApprox(a: Basis, epsilon: RealT = CMP_EPSILON): Boolean {
if (isEqualApprox(this._x.x, a._x.x, epsilon)) return false
if (isEqualApprox(this._x.y, a._x.y, epsilon)) return false
if (isEqualApprox(this._x.z, a._x.z, epsilon)) return false
if (!isEqualApprox(this._x.x, a._x.x, epsilon)) return false
if (!isEqualApprox(this._x.y, a._x.y, epsilon)) return false
if (!isEqualApprox(this._x.z, a._x.z, epsilon)) return false

if (isEqualApprox(this._y.x, a._y.x, epsilon)) return false
if (isEqualApprox(this._y.y, a._y.y, epsilon)) return false
if (isEqualApprox(this._y.x, a._y.x, epsilon)) return false
if (!isEqualApprox(this._y.x, a._y.x, epsilon)) return false
if (!isEqualApprox(this._y.y, a._y.y, epsilon)) return false
if (!isEqualApprox(this._y.z, a._y.z, epsilon)) return false

if (isEqualApprox(this._z.x, a._z.x, epsilon)) return false
if (isEqualApprox(this._z.y, a._z.y, epsilon)) return false
if (isEqualApprox(this._z.z, a._z.z, epsilon)) return false
if (!isEqualApprox(this._z.x, a._z.x, epsilon)) return false
if (!isEqualApprox(this._z.y, a._z.y, epsilon)) return false
if (!isEqualApprox(this._z.z, a._z.z, epsilon)) return false

return true
}
Expand Down

0 comments on commit 7573ef1

Please sign in to comment.