二维逆运动学入门

If you’ve read my previous blog post about the new 2D Animation package in Unity, you might be curious about what the new Inverse Kinematics package can add to your projects. Read on to find out about the basic ideas behind IK and how to set it up in Unity.

如果您阅读过我以前有关Unity中新2D动画包的博客文章 ,您可能会对新的反向运动包可以添加到您的项目中感到好奇。 继续阅读以了解IK背后的基本思想以及如何在Unity中进行设置。

获取包装 (Getting the Package)

As mentioned before, 2D IK ships as a separate editor package, and is available from the Package Manager – just like 2D Animation.

如前所述,2D IK作为单独的编辑器软件包提供,并且可以从“软件包管理器”中获得,就像2D动画一样。

逆运动学基础 (Basics of Inverse Kinematics)

In order to understand Inverse Kinematics, we first need to be aware that they’re the opposite of Forward Kinematics – which refers to the typical process of manually moving and rotating each bone to arrive at the desired point in world space. The idea behind Inverse Kinematics is to take a chain of bone transforms and use an algorithm to move each transform so that the last bone is as close to a given target position as possible.

为了理解逆向运动学,我们首先需要意识到它们与正向运动学是相反的-正向运动学是指手动移动和旋转每个骨骼以到达世界空间中所需点的典型过程。 反向运动学的思想是采用一系列骨骼变换,并使用一种算法来移动每个变换,以使最后一个骨骼尽可能接近给定的目标位置。

Inverse Kinematics are a handy tool in multiple animation scenarios: for example, they can be used to ensure that your character’s feet stay on top of the ground on an uneven surface; or to simulate a character grabbing onto an object. You could also use them when designing or prototyping your animation cycles to make bone positioning easier.

逆运动学是多种动画场景中的便捷工具:例如,它们可用于确保角色的脚在不平坦的表面上停留在地面上; 或模拟抓到对象上的角色。 您还可以在设计或制作动画循环原型时使用它们,以使骨骼定位更加容易。

In this blog post, we’ll pick up where we left off with 2D Animation introduction, using the same rigged Viking sprite. To start using IK on our bone rig, we begin by adding an IK Manager 2D component to the highest object in the hierarchy – in our case, the rigged sprite Game Object which also contains the Sprite Skin component. Alternatively, you could attach it to the root bone of the rig. Once we have an IK manager, we can use the plus and minus buttons underneath to start adding and removing the different kinds of IK solvers applied to our object.

在此博客文章中,我们将使用相同的Viking精灵精灵从2D动画介绍中继续学习。 要开始在骨骼装备上使用IK,我们首先将IK Manager 2D组件添加到层次结构中的最高对象–在本例中,是已绑定的Sprite游戏对象,其中也包含Sprite Skin组件。 或者,您可以将其连接到装备的根骨骼。 有了IK管理器后,我们可以使用下面的加号和减号按钮开始添加和删除应用于对象的各种IK解算器。

添加IK解算器 (Adding IK Solvers)

Before we move on, here’s a brief overview of the terminology that will be used in this blog post when talking about Inverse Kinematics, to make sure we are on the same page:

在继续进行之前,这里是在讨论逆运动学时将在本博客文章中使用的术语的简要概述,以确保我们在同一页面上:

An IK chain refers to the group of bones parented to each other that will be affected by the IK algorithm. An example of a chain of bones could be an arm or leg of a character, however, it’s not limited to just limbs – the chain can span any number of bones, from the furthest bone to the root. For example, you could include the bones of the arm in a chain, as well as part of the character’s spine.

IK链是指将受IK算法影响的彼此父代的骨骼组。 骨骼链的一个例子可以是角色的手臂或腿,但是,它不仅限于肢体-链可以跨越从最远的骨骼到根部的任何数量的骨骼。 例如,您可以将手臂的骨骼以及角色脊柱的一部分包括在链中。

An effector is the end-bone or last child in an IK chain. The IK algorithm’s purpose is to bring the effector as close to the target as possible. An example of an effector could be the hand bone or a finger bone in a more detailed skeleton.

效应器是IK链中的末端骨骼或最后一个子代。 IK算法的目的是使效应器尽可能靠近目标。 效应器的示例可以是更详细的骨架中的手骨或手指骨。

A target or goal refers to the target position in world space that we wish the effector to arrive at as a result of performing the algorithm. For example, if you wanted the character to grab hold of a weapon object, you would use its position as the goal.

目标或目标是指我们希望效果器执行算法后在世界空间中所要达到的目标位置。 例如,如果您希望角色抓住武器对象,则可以将其位置用作目标。

We now need to make a decision about the most appropriate IK solver for our use case. An IK solver, in this case, simply refers to the algorithm that we use to rotate the bones in the IK chain, either until the effector is at the target position, or until we run out of iterations. There are many different IK algorithms, and you can, of course,, write custom ones yourself. By default, Unity’s IK package ships with three common types of solvers, which makes IK really easy to set up.

现在,我们需要确定最适合我们用例的IK解算器。 在这种情况下,IK解算器仅指代我们用来旋转IK链中骨骼的算法,直到效果器位于目标位置或直到迭代用尽为止。 IK算法有很多,您当然可以自己编写自定义算法。 默认情况下,Unity的IK软件包附带三种常见类型的求解器,这使IK的设置非常容易。

First, we have the CCD, or cyclic coordinate descent solver. This is one of the most popular IK algorithms for games. It works by iterating over all the bones in the chain one-by-one starting at the effector end of the chain, and performing a rotation on each one to be as close to the target as possible, until the effector arrives at the target or we’ve reached the maximum number of iterations allowed.

首先,我们有CCD或循环坐标下降求解器。 这是最流行的IK游戏算法之一。 它的工作原理是:从链的效应器末端开始,在链中的所有骨骼上进行一个接一个的迭代,然后对每个骨骼进行旋转,使其尽可能接近目标,直到效应器到达目标或目标。我们已经达到了允许的最大迭代次数。

To set up a CCD solver, we need to first add it to the IK Manager component. After that, In the hierarchy, you will see that a “New CCDSolver2D” has been parented to our sprite object. We can now configure how it behaves and which bones it will affect. Let’s demonstrate how we can add IK to one of our viking’s arms. To do so, select the last bone in the arm chain, and in the hierarchy, create a new empty transform object (Right Click > Create Empty). The new object should automatically be parented to the bone. You can now grab this transform and move it to the tip of the hand bone. It will serve as both our target and effector.

要设置CCD解算器,我们首先需要将其添加到IK Manager组件中。 之后,在层次结构中,您将看到“ New CCDSolver2D”已成为我们的sprite对象的父对象。 现在,我们可以配置它的行为方式以及它将影响哪些骨骼。 让我们演示如何将IK添加到维京人的手臂中。 为此,选择手臂链中的最后一个骨骼,然后在层次结构中创建一个新的空变换对象( 右键单击 > 创建空 )。 新对象应自动成为骨骼的父对象。 现在,您可以抓住此变换并将其移到手骨的尖端。 它既可以作为我们的目标,也可以作为效应器。

Now, if we select our newly added CCD solver, there are several things we need to set up. First up, we have the option to assign our Target and Effector Game Objects. You can grab the empty Game Object you created earlier, and put it into the Target field. This will essentially serve as the last transform in our IK chain, and will rotate the tip of our character’s hand bone towards our goal.

现在,如果我们选择新添加的CCD求解器,则需要设置几项。 首先,我们可以选择分配目标和效应游戏对象。 您可以获取先前创建的空游戏对象,并将其放入“目标”字段。 从本质上讲,这将成为IK链中的最后一个变换,并使角色的手骨尖端朝我们的目标旋转。

The next step is to assign the desired chain length for our IK solver. Once you start dragging the slider, you will see yellow gizmos appear along the parts of the skeleton which will make up the chain. In this case, our chain length will be 4 – we have our hand tip transform, the hand bone, and the lower and upper arm bones. Once you’re happy with your chain, you can use the Create Effector button to automatically generate a transform for which our IK chain will be reaching.

下一步是为我们的IK解算器分配所需的链长。 一旦开始拖动滑块,您将看到黄色小物件出现在构成链的骨架部分上。 在这种情况下,我们的链长将为4 –我们有手尖变换,手骨以及上下手臂骨。 对链满意后,可以使用“创建效应器”按钮自动生成我们的IK链将要到达的变换。

And that’s it – you can now use the new IK gizmo that appeared at the viking’s hand to change the target, and the IK solution will react accordingly.

就是这样–现在,您可以使用维京人手中出现的新IK gizmo更改目标,并且IK解决方案将做出相应的React。

You will notice some other settings available in the CCD solver. They include:

您会注意到CCD求解器中的一些其他设置。 它们包括:

Iterations – this setting determines the maximum number of iterations the algorithm will step through unless it is able to arrive at the goal earlier. We need to keep in mind that most solvers are iterative algorithms, which is why we need to set a limitation here for the case that the goal can never be reached. If you use IK at runtime, it might be worth keeping iterations quite low; however, if you are only using it for positioning in animations, you can pretty much increase the iteration count to be as high as you need to while recording your animations.

迭代次数 -此设置确定算法将执行的最大迭代次数,除非它能够更早地达到目标。 我们需要记住,大多数求解器是迭代算法,这就是为什么我们需要为永远无法达到目标的情况设置一个限制的原因。 如果在运行时使用IK,则可能值得将迭代次数保持在较低水平。 但是,如果仅将其用于动画中的定位,则可以在记录动画时将迭代次数增加到所需的数量。

Tolerance – this determines what is the maximum tolerable distance that the effector can be from the goal for the IK algorithm to be considered complete.

公差 –这决定了效应器离IK算法的目标可以达到的最大最大公差距离。

Velocity determines the strength of the rotation performed on each iteration of the algorithm. Generally, higher Velocity values will mean that the goal is reached faster.

速度决定了算法每次迭代执行的旋转强度。 通常,较高的“速度”值将意味着更快地达到目标。

Finally, the Weight setting determines the overall strength of the applied IK solution, and is further influenced by the global Weight setting in the IK manager component.

最后,权设置决定了所应用的IK解决方案的整体强度,并且进一步受到IK管理器组件中全局权重设置的影响。

If you don’t want the tip of the character’s hand to rotate with IK, and only want the wrist to be positioned at a specific point, you can instead use the hand bone in the Target field, and create the effector from that. Then either disable the Constrain Rotation checkbox to be able to position the bone manually, or rotate the effector instead.

如果您不希望角色的手尖随IK一起旋转,而只希望将手腕放在特定点上,则可以改用“目标”字段中的手骨,然后从中创建效果器。 然后禁用“ 约束旋转”复选框以能够手动定位骨骼,或者旋转效应器。

The 2D IK package includes another type of IK solver which is particularly useful when animating humanoid characters, called the Limb solver. This is a modified version of the CCD algorithm which is limited to a chain of three transforms. It is perfect for positioning limbs, and also includes the option to ‘flip’ the IK result in case the joints in the limbs end up facing the wrong way; however, it does not include as many customization options as the included CCD algorithm.

2D IK软件包包括另一种类型的IK解算器,这在动画人形角色时特别有用,称为 Limb 解算器。 这是CCD算法的修改版本,仅限于三个变换的链。 它是四肢定位的理想选择,并且还包括“翻转” IK结果的选项,以防四肢的关节最终朝向错误的方向。 但是,它不包括所包含的CCD算法那么多的自定义选项。

We also have the FABRIK solver, which stands for forwards and backward reaching inverse kinematics. This algorithm first does a backward pass along the chain, starting with the effector at the goal; and then a forward pass from the highest bone in the hierarchy, applying the correct lengths to the points acquired during the backward pass. FABRIK generally takes fewer iterations to reach the target than CCD, but is slower per iteration if rotation constraints are applied to the chain.

我们还有 FABRIK 求解器,它代表 向前和向后到达逆运动学 。 该算法首先从目标的效应器开始沿链向后进行; 然后从层次结构中最高的骨骼进行前向遍历,将正确的长度应用于在后向遍历期间获取的点。 与CCD相比,FABRIK通常需要较少的迭代次数才能达到目标,但是如果将旋转约束应用于链,则每次迭代都较慢。

If you wish to find out more about inverse kinematics and how the CCD algorithm works, you can check out a video on it in 3D context presented by James Bouckley at Unite Berlin 2018. James also explains his idea of modifying the original CCD algorithm.

如果您想了解有关逆运动学以及CCD算法如何工作的更多信息,可以 在James Bouckley在Unite Berlin 2018上的3D上下文中 查看有关该 视频的视频 。James还解释了他修改原始CCD算法的想法。

As mentioned earlier, you are able to write custom 2D solvers and build on top of the existing ones in the package. For more details on doing so, please refer to the preview documentation available on GitHub. It will give you a good idea on how to get started!

如前所述,您可以编写自定义2D解算器并在程序包中现有解算器的基础上构建。 有关这样做的更多详细信息,请参阅 GitHub上 的 预览文档 。 它将为您提供一个入门的好主意!

It’s also worth noting that in the case that you have two different solvers affecting one or more of the same bones, the IK priority is determined by the order you have the solvers arranged in within the IK Manager, with items nearer to the top of the list having the highest priority.

还要注意的是,如果您有两个影响一个或多个相同骨骼的不同求解器,则IK优先级由您在IK管理器中排列求解器的顺序确定,其中项更靠近具有最高优先级的列表。

Additionally, it’s important to keep in mind that even if you remove the IK solvers, the default position is not returned to the object unless you’ve had them keyframed in an animation, so it is a form of destructive editing. In cases where you wish to undo the effects of the IK solution, we include the option to Restore Default Pose to restore the bones to the point before IK was applied. Once that’s done, you can remove the IK solver safely.

此外,请务必记住,即使删除了IK解算器,除非您已在动画中为其设置关键帧,否则默认位置也不会返回给对象,因此这是一种破坏性编辑形式。 如果希望撤消IK解决方案的效果,则可以使用 “还原默认姿势 ”选项 将骨骼还原到应用IK之前的位置。 完成后,可以安全地删除IK解算器。

在动画中使用IK (Using IK in Animations)

If you wish to use IK within animation clips, the positions of the IK effectors will also get added to the animation as keyframes when you change them – provided the effectors are children of the main GameObject being animated. You could add keyframes for all of your IK effectors at the start, but if you wanted to later remove them – you would need to keyframe the bones changed by the effector, and only then remove the IK. This way you will still keep the transforms of your bones in the right place; but keep in mind that the bones might arrive at the transforms differently since there is no longer an IK solution applied at every animation frame (unless you add keys every step of the way).

如果希望在动画剪辑中使用IK,则在更改IK效果器的位置时,它们也会作为关键帧添加到动画中,前提是这些效果器是要制作动画的主要GameObject的子级。 您可以在开始时为所有IK效果器添加关键帧,但是如果以后要删除它们,则需要对由效果器更改的骨骼进行关键帧设置,然后再删除IK。 这样,您仍然可以将骨骼的变形保持在正确的位置。 但是请记住,由于不再在每个动画帧上都应用IK解决方案,骨骼可能会以不同的方式到达变换(除非您在每个步骤中都添加了关键点)。

And that’s that! You’re now fully ready to use Inverse Kinematics with your animations. It’s worth noting that the tool is usable beyond the scope of 2D Animation, though the features definitely tie in well together. As always, you are free to experiment and find your own creative ways to utilize IK. Here’s an example of what you could end up with if you try attaching colliders onto the IK effectors:

就是这样! 现在,您已经准备好在动画中使用逆运动学。 值得注意的是,该工具在2D动画的范围之外仍然可用,尽管这些功能确实可以很好地结合在一起。 与往常一样,您可以自由尝试并找到自己的创造性方式来利用IK。 这是一个示例示例,说明如果尝试将对撞机附加到IK效果器上,可能会得到的结果:

更多资源和反馈 (Further Resources and Feedback)

There is additional information on working with IK on the 2D IK repository, which contains the most updated documentation.

2D IK 存储库中, 还有有关使用IK的其他信息 ,其中包含最新的文档。

The documentation is particularly worth a look if you wish to find out more about the API and how to write your own IK Solvers.

如果您想了解有关API以及如何编写自己的IK解算器的更多信息,该文档特别值得一看。

On the same repository, you will find several sample projects that you can play around with which demonstrate the basics of the workflow described in this blog post.

同一存储库上 ,您将找到几个示例项目,这些示例项目可用于演示此博客文章中描述的工作流的基础。

#Unity2DC挑战 (#Unity2DChallenge)

We would love to see what you are creating with our new 2D packages, and to hear about your experience using them!

我们很乐意看到您使用我们的新2D软件包创建的内容,并了解您使用它们的经验!

That’s one of the reasons we launched the Unity 2D Challenge. Create a small piece of content using some of our new 2D tools and you can win cash prizes and tickets to Unite! It can be anything from some pixel-perfect art to a thin vertical slice of a 2D game. The challenge is open for submissions until December 17, so don’t miss the deadline and get started .

这就是我们发起Unity 2D Challenge的原因之一。 使用我们的一些新2D工具创建一小部分内容,您将获得现金奖励和Unite门票! 它可以是任何像素完美的艺术品,也可以是2D游戏的垂直薄片。 挑战赛截止到12月17日截止,因此请不要错过截止日期并开始。

You can also share your thoughts and projects with us on the 2D Animation Forum!

您也可以在2D动画论坛上与我们分享您的想法和项目!

翻译自: https://blogs.unity3d.com/2018/12/04/getting-started-with-2d-inverse-kinematics/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值