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

相关推荐
开开心心就好4 小时前
游戏安装报错缺环境?运行库整合包补上
java·mysql·游戏·pdf·ocr·音视频·lua
鹿野素材屋7 小时前
游戏开发(三):AI时代,如何快速还原一款竞品游戏
游戏
淡海水10 小时前
02-02-原理篇-Scriban模板引擎原理深度解析
unity·excel·游戏开发·luban
一只大鸽子11 小时前
游戏开发笔记19-Godot RPG游戏基础-文本样式
笔记·游戏·godot
禁默1 天前
AI搓童年游戏-用华为云码道 CodeArts(Kimi-K3.1-Pro)从零写一个零依赖俄罗斯方块
人工智能·游戏·华为云
2601_949695591 天前
显卡驱动版本过低玩不了游戏怎么办?如何检查最新驱动?
游戏·电脑·显卡·显卡驱动·驱动程序
ttod_qzstudio2 天前
系统架构设计师论文逐句拆解(三):ATAM 架构评估,怎样把“方案比较”写成质量属性权衡
unity·架构·系统架构·软考
一只大鸽子2 天前
游戏开发笔记17-Godot RPG游戏基础-菜单
笔记·游戏·godot
小贺儿开发2 天前
Unity 轻量级外部素材加载器(热更新)
科技·学习·unity·程序员·工具·热更新·ugui
赤生云电竞3 天前
云游戏边缘节点部署:机房选址与网络拓扑优化实践 —— 基于GPU虚拟化与多级QoS的端到端时延控制
游戏