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

相关推荐
芙莉莲教你写代码3 小时前
Flutter 框架跨平台鸿蒙开发 - 水果消消乐游戏
flutter·游戏·华为·harmonyos
恶猫3 小时前
游戏脚本助手,电脑点击器,脚本自动点击识图找图_无限试用版
游戏
huwuhang3 小时前
孤岛惊魂系列全集 离线游戏 1~6部网盘整合下载 全DLC+修改器 中文版 、孤岛惊魂中文版 全部游戏、
游戏
芙莉莲教你写代码3 小时前
Flutter 框架跨平台鸿蒙开发 - 坦克大战游戏
flutter·游戏·华为·harmonyos
mxwin3 小时前
Unity Shader UV 坐标与纹理平铺Tiling & Offset 深度解析
unity·游戏引擎·shader·uv
张二娃同学3 小时前
基于 Python 与 Tkinter 的猜数字游戏设计与实现:支持玩家猜数与 AI 反向推理
开发语言·git·python·游戏·开源
前端不太难3 小时前
鸿蒙游戏:从单设备到全场景
游戏·harmonyos
Swift社区4 小时前
未来游戏形态:鸿蒙 + AI + 多端协同
人工智能·游戏·harmonyos
chao1898444 小时前
基于STM32F1的声源定位系统设计与实现
stm32·嵌入式硬件·unity
智算菩萨4 小时前
【Pygame】第5章 键盘与鼠标事件处理(附有2D射击游戏)
游戏·计算机外设·pygame