Skip to content
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

Fix gimbal lock on editor camera #119

Merged
merged 1 commit into from
Oct 23, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class FreeCamController : InputAdapter() {
private var zoomAmount = SPEED_01
private var degreesPerPixel = 0.5f
private val tmp = Vector3()
private val tmp2 = Vector3()
private val tmp3 = Vector3()
private var pan = true

fun setCamera(camera: Camera) {
Expand Down Expand Up @@ -95,7 +97,16 @@ class FreeCamController : InputAdapter() {

camera!!.direction.rotate(camera!!.up, deltaX)
tmp.set(camera!!.direction).crs(camera!!.up).nor()
camera!!.direction.rotate(tmp, deltaY)

// Resolves Gimbal Lock : https://github.com/libgdx/libgdx/issues/4023
val oldPitchAxis = tmp.set(camera!!.direction).crs(camera!!.up).nor()
JamesTKhan marked this conversation as resolved.
Show resolved Hide resolved
val newDirection: Vector3 = tmp2.set(camera!!.direction).rotate(tmp, deltaY)
val newPitchAxis: Vector3 = tmp3.set(tmp2).crs(camera!!.up)

if (!newPitchAxis.hasOppositeDirection(oldPitchAxis)) {
camera!!.direction.set(newDirection)
}

} else {
tmp.set(camera!!.direction).crs(camera!!.up).nor().scl(deltaX / velocity)
camera!!.position.add(tmp)
Expand Down