protected override void DoSolverUpdate() { if (!rootBone) { return; } for (int i = 0; i < solverPoses.Count; ++i) { SolverPose solverPose = solverPoses[i]; if (solverPose != null && solverPose.bone) { solverPose.solverRotation = solverPose.bone.transform.localRotation; solverPose.solverPosition = rootBone.transform.InverseTransformPoint(solverPose.bone.transform.position); } } Vector3 localEndPosition = rootBone.transform.InverseTransformPoint(solverPoses[solverPoses.Count - 1].bone.endPosition); Vector3 localTargetPosition = rootBone.transform.InverseTransformPoint(targetPosition); damping = Mathf.Clamp01(damping); float l_damping = 1f - Mathf.Lerp(0f, 0.99f, damping); for (int i = 0; i < iterations; ++i) { for (int j = solverPoses.Count - 1; j >= 0; --j) { SolverPose solverPose = solverPoses[j]; Vector3 toTarget = localTargetPosition - solverPose.solverPosition; Vector3 toEnd = localEndPosition - solverPose.solverPosition; toTarget.z = 0f; toEnd.z = 0f; float localAngleDelta = MathUtils.SignedAngle(toEnd, toTarget, Vector3.forward); localAngleDelta *= l_damping; Quaternion localRotation = Quaternion.AngleAxis(localAngleDelta, Vector3.forward); solverPose.solverRotation = solverPose.solverRotation * localRotation; Vector3 pivotPosition = solverPose.solverPosition; localEndPosition = RotatePositionFrom(localEndPosition, pivotPosition, localRotation); for (int k = solverPoses.Count - 1; k > j; --k) { SolverPose sp = solverPoses[k]; sp.solverPosition = RotatePositionFrom(sp.solverPosition, pivotPosition, localRotation); } } } }
public void RestoreDefaultPoses() { for (int i = 0; i < solverPoses.Count; i++) { SolverPose pose = solverPoses [i]; if (pose != null) { pose.RestoreDefaultPose(); } } }
protected override void DoSolverUpdate() { if (!rootBone || solverPoses.Count != 2) { return; } SolverPose pose0 = solverPoses[0]; SolverPose pose1 = solverPoses[1]; Vector3 localTargetPosition = targetPosition - rootBone.transform.position; localTargetPosition.z = 0f; float distanceMagnitude = localTargetPosition.magnitude; float angle0 = 0f; float angle1 = 0f; float sqrDistance = localTargetPosition.sqrMagnitude; float sqrParentLength = (pose0.bone.length * pose0.bone.length); float sqrTargetLength = (pose1.bone.length * pose1.bone.length); float angle0Cos = (sqrDistance + sqrParentLength - sqrTargetLength) / (2f * pose0.bone.length * distanceMagnitude); float angle1Cos = (sqrDistance - sqrParentLength - sqrTargetLength) / (2f * pose0.bone.length * pose1.bone.length); if ((angle0Cos >= -1f && angle0Cos <= 1f) && (angle1Cos >= -1f && angle1Cos <= 1f)) { angle0 = Mathf.Acos(angle0Cos) * Mathf.Rad2Deg; angle1 = Mathf.Acos(angle1Cos) * Mathf.Rad2Deg; } float flipSign = flip ? -1f : 1f; Vector3 rootBoneToTarget = Vector3.ProjectOnPlane(targetPosition - rootBone.transform.position, rootBone.transform.forward); if (rootBone.transform.parent) { rootBoneToTarget = rootBone.transform.parent.InverseTransformDirection(rootBoneToTarget); } float baseAngle = Mathf.Atan2(rootBoneToTarget.y, rootBoneToTarget.x) * Mathf.Rad2Deg; pose0.solverRotation = Quaternion.Euler(0f, 0f, baseAngle - flipSign * angle0); pose1.solverRotation = Quaternion.Euler(0f, 0f, flipSign * angle1); }
void UpdateBones() { for (int i = 0; i < solverPoses.Count; ++i) { SolverPose solverPose = solverPoses[i]; if (solverPose != null && solverPose.bone) { if (weight == 1f) { solverPose.bone.transform.localRotation = solverPose.solverRotation; } else { solverPose.bone.transform.localRotation = Quaternion.Slerp(solverPose.bone.transform.localRotation, solverPose.solverRotation, weight); } } } }
public void Initialize(Bone2D _rootBone, int numChilds) { rootBone = _rootBone; Bone2D bone = rootBone; solverPoses.Clear(); for (int i = 0; i < numChilds; ++i) { if (bone) { SolverPose solverPose = new SolverPose(); solverPose.bone = bone; solverPoses.Add(solverPose); bone = bone.child; } } StoreDefaultPoses(); }
public void Initialize(Bone2D _rootBone, int numChilds) { rootBone = _rootBone; Bone2D bone = rootBone; solverPoses.Clear(); for(int i = 0; i < numChilds; ++i) { if(bone) { SolverPose solverPose = new SolverPose(); solverPose.bone = bone; solverPoses.Add(solverPose); bone = bone.child; } } StoreDefaultPoses(); }