Skip to content

Commit

Permalink
Fix over 180 degrees rotations in HumanSkeleton (#1277)
Browse files Browse the repository at this point in the history
  • Loading branch information
Erimelowo authored Jan 2, 2025
1 parent 4e698b6 commit a606c5a
Showing 1 changed file with 1 addition and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -785,11 +785,6 @@ class HumanSkeleton(
var hipRot = it.getRotation()
var chestRot = chest.getRotation()

// Get the rotation relative to where we expect the hip to be
if (chestRot.times(FORWARD_QUATERNION).dot(hipRot) < 0.0f) {
hipRot = hipRot.unaryMinus()
}

// Interpolate between the chest and the hip
chestRot = chestRot.interpQ(hipRot, waistFromChestHipAveraging)

Expand All @@ -802,15 +797,6 @@ class HumanSkeleton(
var rightLegRot = rightUpperLegTracker?.getRotation() ?: IDENTITY
var chestRot = chest.getRotation()

// Get the rotation relative to where we expect the upper legs to be
val expectedUpperLegsRot = chestRot.times(FORWARD_QUATERNION)
if (expectedUpperLegsRot.dot(leftLegRot) < 0.0f) {
leftLegRot = leftLegRot.unaryMinus()
}
if (expectedUpperLegsRot.dot(rightLegRot) < 0.0f) {
rightLegRot = rightLegRot.unaryMinus()
}

// Interpolate between the pelvis, averaged from the legs, and the chest
chestRot = chestRot.interpQ(leftLegRot.lerpQ(rightLegRot, 0.5f), waistFromChestLegsAveraging).unit()

Expand All @@ -827,15 +813,6 @@ class HumanSkeleton(
var rightLegRot = rightUpperLegTracker?.getRotation() ?: IDENTITY
var waistRot = it.getRotation()

// Get the rotation relative to where we expect the upper legs to be
val expectedUpperLegsRot = waistRot.times(FORWARD_QUATERNION)
if (expectedUpperLegsRot.dot(leftLegRot) < 0.0f) {
leftLegRot = leftLegRot.unaryMinus()
}
if (expectedUpperLegsRot.dot(rightLegRot) < 0.0f) {
rightLegRot = rightLegRot.unaryMinus()
}

// Interpolate between the pelvis, averaged from the legs, and the chest
waistRot = waistRot.interpQ(leftLegRot.lerpQ(rightLegRot, 0.5f), hipFromWaistLegsAveraging).unit()

Expand All @@ -849,15 +826,6 @@ class HumanSkeleton(
var rightLegRot = rightUpperLegTracker?.getRotation() ?: IDENTITY
var chestRot = it.getRotation()

// Get the rotation relative to where we expect the upper legs to be
val expectedUpperLegsRot = chestRot.times(FORWARD_QUATERNION)
if (expectedUpperLegsRot.dot(leftLegRot) < 0.0f) {
leftLegRot = leftLegRot.unaryMinus()
}
if (expectedUpperLegsRot.dot(rightLegRot) < 0.0f) {
rightLegRot = rightLegRot.unaryMinus()
}

// Interpolate between the pelvis, averaged from the legs, and the chest
chestRot = chestRot.interpQ(leftLegRot.lerpQ(rightLegRot, 0.5f), hipFromChestLegsAveraging).unit()

Expand Down Expand Up @@ -1110,24 +1078,11 @@ class HumanSkeleton(
rightKnee: Quaternion,
hip: Quaternion,
): Quaternion {
// Get the knees' rotation relative to where we expect them to be.
// The angle between your knees and hip can be over 180 degrees...
var leftKneeRot = leftKnee
var rightKneeRot = rightKnee

val kneeRot = hip.times(FORWARD_QUATERNION)
if (kneeRot.dot(leftKneeRot) < 0.0f) {
leftKneeRot = leftKneeRot.unaryMinus()
}
if (kneeRot.dot(rightKneeRot) < 0.0f) {
rightKneeRot = rightKneeRot.unaryMinus()
}

// R = InverseHip * (LeftLeft + RightLeg)
// C = Quaternion(R.w, -R.x, 0, 0)
// Pelvis = Hip * R * C
// normalize(Pelvis)
val r = hip.inv() * (leftKneeRot + rightKneeRot)
val r = hip.inv() * (leftKnee + rightKnee)
val c = Quaternion(r.w, -r.x, 0f, 0f)
return (hip * r * c).unit()
}
Expand Down

0 comments on commit a606c5a

Please sign in to comment.