Fix match targets to legs

This commit is contained in:
Pascal Serrarens 2026-04-14 10:16:50 +02:00
parent ae035d4672
commit 983b1e1c40
5 changed files with 36 additions and 21 deletions

View File

@ -13,7 +13,7 @@ namespace CreatureControl {
insectRig.legLength = float.PositiveInfinity;
foreach (TargetLeg leg in insectRig.legs) {
float legLength = leg.bones.length;
float legLength = leg.length;
if (legLength > 0 && legLength < insectRig.legLength)
insectRig.legLength = legLength;
}

View File

@ -25,7 +25,7 @@ namespace CreatureControl {
base.OnInspectorGUI();
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.FloatField("Femur Length", targetLeg.bones.femurLength);
EditorGUILayout.FloatField("Femur Length", targetLeg.femurLength);
EditorGUI.EndDisabledGroup();
}
@ -35,7 +35,7 @@ namespace CreatureControl {
return 0;
SerializedObject targetLegPrefabObj = new(prefabSource);
SerializedProperty targetBonesPrefabProp = targetLegPrefabObj.FindProperty(nameof(TargetLeg.bones));
//SerializedProperty targetBonesPrefabProp = targetLegPrefabObj.FindProperty(nameof(TargetLeg.bones));
return 0;
}

View File

@ -54,12 +54,12 @@ namespace CreatureControl {
public float legLength; // smalled leg length
public override void Pose() {
this.leftBackLeg.PoseLimb();
this.leftMiddleLeg.PoseLimb();
this.leftFrontLeg.PoseLimb();
this.rightBackLeg.PoseLimb();
this.rightMiddleLeg.PoseLimb();
this.rightFrontLeg.PoseLimb();
this.leftBackLeg.PoseTargetLimb();
this.leftMiddleLeg.PoseTargetLimb();
this.leftFrontLeg.PoseTargetLimb();
this.rightBackLeg.PoseTargetLimb();
this.rightMiddleLeg.PoseTargetLimb();
this.rightFrontLeg.PoseTargetLimb();
}
public override void MatchTo(Creature creature, ref bool somethingChanged) {

View File

@ -41,6 +41,10 @@ namespace CreatureControl {
}
public float length => femurLength + tibiaLength;
public void ResetLengths() {
_femurLength = 0;
_tibiaLength = 0;
}
/// <summary>
/// Check if all bones of the legs have been configured

View File

@ -9,6 +9,10 @@ namespace CreatureControl {
public class TargetLeg : MonoBehaviour {
public Leg bones;
public float length => bones.length;
public float femurLength => bones.femurLength;
public float tibiaLength => bones.tibiaLength;
public Transform target; // for the tarsus
public Quaternion targetToBoneFemur;
@ -21,36 +25,45 @@ namespace CreatureControl {
return;
this.bones.femur.position = leg.femur.position;
this.bones.femur.LookAt(leg.tibia);
this.bones.tibia.position = leg.tibia.position;
this.bones.tibia.LookAt(leg.tarsus);
this.bones.tarsus.position = leg.tarsus.position;
bones.ResetLengths();
targetToBoneFemur = TargetRig.TargetToBoneRotation(leg.femur, leg.tibia);
targetToBoneTibia = TargetRig.TargetToBoneRotation(leg.tibia, leg.tarsus);
// Put the end-effector target for IK in a sensible place
// Vector3 targetPosition = this.bones.femur.position + 0.7f * this.bones.length * legDirection.normalized;
Vector3 targetPosition = leg.tarsus.position;
Vector3 legDirection = (this.bones.tarsus.position - this.bones.femur.position).normalized;
Vector3 targetPosition = this.bones.femur.position + 0.7f * this.bones.length * legDirection.normalized;
Quaternion targetRotation = Quaternion.LookRotation(legDirection);
this.target.SetPositionAndRotation(targetPosition, targetRotation);
this.target.localPosition = new(this.target.localPosition.x, 0, this.target.localPosition.z);
PoseTargetLimb();
UpdateBones(leg);
}
/// <summary>
/// Pose the target limb
/// </summary>
public void PoseLimb() {
public void PoseTargetLimb() {
if (target == null)
return;
if (bones.femur == null || bones.tibia == null || bones.tarsus == null)
return;
Quaternion femurOrientation = FemurRotation(target.position);
this.bones.femur.rotation = femurOrientation;
Quaternion tibiaOrientation = TibiaRotation(target.position);
this.bones.tibia.rotation = tibiaOrientation;
Quaternion tarsusOrientation = TarsusRotation(target.rotation);
bones.femur.rotation = femurOrientation;
bones.tibia.rotation = tibiaOrientation;
bones.tarsus.rotation = tarsusOrientation;
this.bones.tarsus.rotation = tarsusOrientation;
}
public void UpdateBones(Leg leg) {
@ -63,10 +76,8 @@ namespace CreatureControl {
return Quaternion.identity;
Vector3 toTarget = targetPosition - this.bones.femur.position;
// Debug.DrawRay(femur.position, toTarget, Color.magenta);
// Debug.DrawRay(bones.femur.position, toTarget, Color.magenta);
float targetDistance = toTarget.magnitude;
float femurLength = Vector3.Distance(this.bones.femur.position, this.bones.tibia.position);
float tibiaLength = Vector3.Distance(this.bones.tibia.position, this.bones.tarsus.position);
float hipAngle = CosineRule(targetDistance, femurLength, tibiaLength);
// NaN happens when the distance to the footTarget is longer than the length of the leg
@ -76,8 +87,8 @@ namespace CreatureControl {
Quaternion femurOrientation = Quaternion.LookRotation(toTarget, Vector3.up);
femurOrientation = Quaternion.AngleAxis(hipAngle, femurOrientation * Vector3.left) * femurOrientation;
// Debug.DrawRay(femur.position, femurOrientation * Vector3.forward, Color.blue);
// Debug.DrawRay(femur.position, femurOrientation * Vector3.up, Color.green);
// Debug.DrawRay(bones.femur.position, femurOrientation * Vector3.forward, Color.blue);
// Debug.DrawRay(bones.femur.position, femurOrientation * Vector3.up, Color.green);
return femurOrientation;
}
@ -137,7 +148,7 @@ namespace CreatureControl {
if (bones.tibia != null && this.bones.tarsus != null)
Gizmos.DrawLine(this.bones.tibia.position, this.bones.tarsus.position);
PoseLimb();
PoseTargetLimb();
}
#endregion Scene