diff --git a/CreatureControl/Editor/Scripts/Insect/InsectRig_Editor.cs b/CreatureControl/Editor/Scripts/Insect/InsectRig_Editor.cs index 2c77f04..09b3b02 100644 --- a/CreatureControl/Editor/Scripts/Insect/InsectRig_Editor.cs +++ b/CreatureControl/Editor/Scripts/Insect/InsectRig_Editor.cs @@ -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; } diff --git a/CreatureControl/Editor/Scripts/TargetLeg_Editor.cs b/CreatureControl/Editor/Scripts/TargetLeg_Editor.cs index 9886259..7309e9c 100644 --- a/CreatureControl/Editor/Scripts/TargetLeg_Editor.cs +++ b/CreatureControl/Editor/Scripts/TargetLeg_Editor.cs @@ -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; } diff --git a/CreatureControl/Runtime/Scripts/Insect/InsectRig.cs b/CreatureControl/Runtime/Scripts/Insect/InsectRig.cs index 378ced4..f258421 100644 --- a/CreatureControl/Runtime/Scripts/Insect/InsectRig.cs +++ b/CreatureControl/Runtime/Scripts/Insect/InsectRig.cs @@ -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) { diff --git a/CreatureControl/Runtime/Scripts/Leg.cs b/CreatureControl/Runtime/Scripts/Leg.cs index 03043b2..484d3f0 100644 --- a/CreatureControl/Runtime/Scripts/Leg.cs +++ b/CreatureControl/Runtime/Scripts/Leg.cs @@ -41,6 +41,10 @@ namespace CreatureControl { } public float length => femurLength + tibiaLength; + public void ResetLengths() { + _femurLength = 0; + _tibiaLength = 0; + } /// /// Check if all bones of the legs have been configured diff --git a/CreatureControl/Runtime/Scripts/TargetLeg.cs b/CreatureControl/Runtime/Scripts/TargetLeg.cs index ba2ba25..fccfaf4 100644 --- a/CreatureControl/Runtime/Scripts/TargetLeg.cs +++ b/CreatureControl/Runtime/Scripts/TargetLeg.cs @@ -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); } /// /// Pose the target limb /// - 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