Unity中简单实现IK(Generic Inverse Kinematics)算法

1.原理解释:

待补充。。。

2.简单实现Demo:

1.搭建简单场景:

2.实现代码:

算法实现分析:

待补充。。。

主要实现代码如下:

cs 复制代码
 void SolveTwoBoneIK()
 {
     Vector3 targetPosition = target.position;
     Vector3 rootPosition = rootBone.position;

     float targetDistance = Vector3.Distance(rootPosition, targetPosition);
     float chainLength = upperLength + lowerLength;

     if (targetDistance >= chainLength)
     {
         Vector3 direction = (targetPosition - rootPosition).normalized;
         middleBone.position = rootPosition + direction * upperLength;
         endBone.position = rootPosition + direction * chainLength;
         return;
     }

     // 使用余弦定理计算关节角度
     // cos(angle) = (a² + b² - c²) / (2ab)

     float upperSqr = upperLength * upperLength;
     float lowerSqr = lowerLength * lowerLength;
     float targetSqr = targetDistance * targetDistance;

     float upperAngle = Mathf.Acos(
         Mathf.Clamp(
             (upperSqr + targetSqr - lowerSqr) / (2 * upperLength * targetDistance),
             -1f, 1f
         )
     ) * Mathf.Rad2Deg;

     float middleAngle = Mathf.Acos(
         Mathf.Clamp(
             (upperSqr + lowerSqr - targetSqr) / (2 * upperLength * lowerLength),
             -1f, 1f
         )
     ) * Mathf.Rad2Deg;

     Vector3 toBendDirection = Vector3.zero;
     if (poleTarget != null)
     {
         Vector3 toTargetPos = targetPosition - rootPosition;
         Vector3 toPole = poleTarget.position - rootPosition;
         toBendDirection = Vector3.ProjectOnPlane(toPole, toTargetPos).normalized;
     }
     else
     {
         toBendDirection = Vector3.Cross(
             targetPosition - rootPosition,
             Vector3.up
         ).normalized;
     }

     // 5. 应用旋转
     Vector3 toTarget = (targetPosition - rootPosition).normalized;

     Quaternion upperRotation = Quaternion.LookRotation(toTarget, toBendDirection);
     upperRotation *= Quaternion.Euler(-upperAngle, 0, 0);
     rootBone.rotation = Quaternion.Lerp(rootBone.rotation, upperRotation, weight);

     middleBone.position = rootBone.position + rootBone.forward * upperLength;

     Vector3 toEnd = (targetPosition - middleBone.position).normalized;
     middleBone.rotation = Quaternion.Lerp(
         middleBone.rotation,
         Quaternion.LookRotation(toEnd, toBendDirection),
         weight
     );

     endBone.position = Vector3.Lerp(
         endBone.position,
         targetPosition,
         weight
     );
 }

实现结果:

参考链接:

Unity 3D - Generic Inverse Kinematics (IK) - Open Source - Github (youtube.com)

C# Inverse Kinematics in Unity 🎓 (youtube.com)

https://www.youtube.com/watch?v=qqOAzn05fvk

相关推荐
于先生吖2 小时前
从源码到上线:Java 游戏陪玩系统的性能优化与安全加固
安全·游戏
驱动开发0072 小时前
[原创]硬件级别虚拟HID鼠标键盘,过游戏检测
游戏·计算机外设
桌面运维家2 小时前
Windows游戏鼠标DPI调校指南:精准定位与优化
windows·游戏·计算机外设
呆呆敲代码的小Y3 小时前
【Unity-AI开发篇】| Unity-MCP最新指南:让AI接管游戏开发
人工智能·游戏·unity·ai·游戏引擎·mcp·unitymcp
音视频开发_AIZ3 小时前
语聊房实时语音SDK选型:即构 vs 声网 vs 腾讯云深度对比
flutter·unity·uni-app·实时音视频·ai降噪·实时语音·语音社交
HahaGiver6663 小时前
Unity Shader Graph 2D - 一个液体瓶子效果
unity·游戏引擎
前端不太难3 小时前
OpenClaw:经典 2D 游戏引擎解析
游戏引擎·状态模式
敲代码的小王!3 小时前
prompt开发游戏-哄哄模拟器
java·游戏·ai·prompt
luckycoding4 小时前
3676. 碗子数组的数目
算法·游戏·深度优先
R-sz4 小时前
虚幻 UE5 像素流多用户部署,像素流多实例部署
ue5·游戏引擎·虚幻