游戏邦在:
杂志专栏:
gamerboom.com订阅到鲜果订阅到抓虾google reader订阅到有道订阅到QQ邮箱订阅到帮看

分享设计参数动画系统的经验

发布时间:2014-08-13 12:52:43 Tags:,,

作者:Peyman Massoudi

由于游戏开发过程要由拥有不同技能的人参与,游戏开发者就要尽量根据从事游戏不同环节工作的人的角度来增加抽象程度。参数动画就是为此而设计的,它提供了一些诸如AI,或者需要使用动画的玩法等其他模块界面的参数。参数动画可以确保动画总能够适当回应参数变化 ,以便AI或玩法等其他模块能够根据自己的计算来更改这些参数,而无需处理动画技巧方面的细节。本文旨在分享如何创造一个参数动画系统,并令其用于其他模块。本文使用了Unity Mecanim动画系统来创造参数动画系统。

引言

如今,制作电子游戏需要广泛的知识。游戏开发过程中要有程序员、游戏设计师、美术人员和总监等人的参与,每个人都要扮演拥有不同技能的角色。例如,美术人员可以是动画人员,建模师或概念美术师,而程序员可以是AI程序员,工具程序员,玩法程序员,网络程序员等。为了开发一款富有趣味的游戏,大家必须恰当地将这些广泛的技能运用到一起。每个人都要从自己的角度出发来推动开发进程。为实现这一点,就必须在游戏开发过程中拥有不同技能的不同人之间创造一定程度的抽象性。这种抽象性也可能以动画形式来体现。实现这种抽象性的一个方法就是使用参数动画。参数动画通过呈现一些参数,将动画系统从其他模块完全分离出来。这些参数是一些让其他模块与动画系统将连接的数据性或布尔界面。它们可以是瞄准方向、速度、力、表面梯度、命值或者像是跳跃或者其他可能的布尔参数。通过改变这些参数,动画系统会返回一些合适的运动。我们有许多方法可创造参数动画,例如使用动画混合树并以类似动画分层和动画状态机等技巧将其结合起来。其他动画技巧可以结合起来使用,例如运行时间系畜产或其他程序生成动画技巧等参数动画。参数动画中最重要的事情就是,它应该恰当地响应参数变化,以便用上任何可能有助于该系统实现这个目标的技巧。在找到从其他游戏模块中分离动画细节的方法之后,其他模块就可以更轻松地开始执行自己的运算。多数情况下,动画参数有别于玩法、控制或瞄准方向、运行速度等AI模块。我们将在下一环节使用动画混合树和动画状态机创造一个参数动画系统。

参数化动画

我们将在此环节设计和执行一个参数运动动画系统。为执行这个参数运动系统,就要用到混合树及动画状态机。Unity的Mecanim动画系统要用于执行这个参数运动系统。我们选择Mecanim有两个原因,首先,Unity是极受欢迎的游戏开发者工具,其次,Mecanim提供了将动画从玩法或AI等其他模块中分离出来的优质功能。本文仅使用一个参数来描述玩家的速度。我们通过其他模块来计算速度参数,它将传送到动画系统中,而后者则会通过选择其最匹配的动画进行响应。图1显示了为此原因而创造的动画状态机。根据速度值的情况,动画系统可以在闲置和运动状态之间切换。

图1(from gamedev)

图1(from gamedev)

在参数化动画之前,动画团队及其他要用到动画的团队要彼此沟通,并确定自己所需的参数,详细说明每个参数的阀值。在本文中只定义了速度参数,其最小值为2.5m/s,最大值为10m/s。速度参数可以因其他许多模块而更改。例如,玩法程序员可能通过检查玩家控制一个虚拟摇杆的频率来调整速度,或者AI程序员可能根据自己所用的寻径、障碍躲避或群体仿真来更改速度基础。

在找到特定参数及其阈值之后,动画人员可以创造可能处理参数变化来实现最佳视觉效果的特定动画。

在下面三个环节中,我们会分别考虑图1所显示的三个动画状态。

运动状态

本文使用了7种运动的动画,并为之创造了一个混合树,它可以根据速度参数混合行走、奔跑和疾跑动画。图2显示了在运动状态下创造的混合树。需要注意的是Mecanim混合树可以用行走和奔跑等不同的时机混合两个甚至更多个动画。

正如图2所示,这里有2个行走动画,3个奔跑动画和2个疾跑动画。在解释为何使用这7个动画之前,让我们先考虑一下动画系统“Accel”所存在的一个参数。Accel参数由动画系统定义,想控制速度的参数动画用户却对它一无所知。Accel是用于显示玩家是否开始或停止奔跑或行走。通过改变这一参数,混合树将可混合以下几种动画。

1.walk,walk_accel:它们是两个行走动画,计时相同,但它们有一个区别在于,在walk_accel动画中,角色的头部和脊柱会向前倾斜,以便显示角色准备开始行走。run和run_accel以及sprint、sprint_accel这两个有关奔跑、疾跑的动画亦是如此。

2.run_deaccel:这个动画与run动画相同,区别就在于角色的头部和脊柱会向后倾以便显示角色想停止奔跑状态。

图2(from gamedev)

图2(from gamedev)

accel参数可以在-1和1之间变换,1显示的是角色打算开始向前移动,而-1则显示角色打算停止。

闲置状态

闲置状态有一个混合树,混合两个闲置动画。idle_mecanim动画是一个让玩家站立的简单动画,而idle_accel_mecanim则与之类似,但角色的头部和脊柱会向前倾斜,以便显示角色打算开始移动。图3显示了闲置状态的混合树。Accel参数是用于混合idle和idle_accel。

图3(from gamedev)

图3(from gamedev)

Idle_mirrored状态

最后一个状态就是Idle_mirrored。这个状态是用于避免脚部漂浮的现象。在idle_mecanim动画中,角色的左脚位于身体之前,右脚则在身后,这显示了一种更为进取性的站立姿势。这个姿势会导致角色在即将从行走切换到闲置状态时出现一些脚部漂浮的情况。当我们从行走切换到闲置状态时,如果角色在行走时右脚居前,左脚居后,这种切换也会出现脚部漂浮的情况,因为在闲置动画中角色左脚要居前,右脚要靠后。为避免这种情况,我们创造了Idle_mirrored状态。Idle_mecanim和idle_accel_mecanim动画就是为这个原因而创造。

mecanim运动重定向系统Avatar用于反射这两个动画。图4显示了在dle_mirrored状态下创造的混合树。

图4(from gamedev)

图4(from gamedev)

动画状态机参数

动画状态机定义了3个参数:

1.速度:速度用于混合行走、奔跑和疾跑动画或者切换到闲置状态。它是玩法/AI模块传送到动画系统的值。

2.Accel:Accel用于混合加速姿势动画和一般姿势,以便显示角色打算开始或停止移动。Accel由动画状态机定义,其他模块对此毫不知情,它是在参数动画模块进行运算。

3.时间:动画系统中所用的时间主要是为了选择执行哪种切换,切换到闲置状态或Idle_mirrored状态。时间由动画状态机定义,其他模块对此并不知情,它由参数动画模块进行运算。

执行切换的规则

在Mecanim的动画状态机中,你可以通过满足定义条件来执行切换。这个环节包含运用于我们参数动画系统的规则。

第一个规则就是,如果速度参数低于2.5,那么玩家就必须停止,否则他就会向前移动。这意味着如果玩家处于闲置状态,而速度却高于2.5,那么他就应该切换到运动状态。

第二个规则与运动状态的时间有关。为避免脚部漂浮的情况,从运动切换到闲置状态应该在特定时间的行走动画时间中执行。人类行走循环有一个阶段叫双重支持。在双重支持阶段,左右脚都会落在地面上。在此我呈现了自己所创造的双重支持阶段的行走动画。行走动画长度为1.2秒。

(时间>0.53以及时间<0.75):双重支持阶段,左脚在前,右脚在后,双脚都落在地面。如果这个条件为真,并且速度就小于2.5,那么系统就要切换到闲置状态。

(时间<0.19或(时间>0.91并且时间<1):双重支持阶段,右脚在前,左脚在后,双脚都落在地面。如果这个条件为真,并且速度小于2.5,那么系统就要切换到Idle_mirrored状态。图5显示了从运动切换到闲置状态的定义规则。

图5(from gamedev)

图5(from gamedev)

图6和图7显示了从运动切换到Idle_mirrored状态的定义规则。在mecanim定义一个状态转到另一个状态至少两次切换就好像是“OR”布尔操作,而每次切换就好像它的算子。

图6(from gamedev)

图6(from gamedev)

图7(from gamedev)

图7(from gamedev)

图8显示了从闲置和idle_mirrored到运动状态的切换定义规则。

图8(from gamedev)

图8(from gamedev)

传送参数

之前的环节显示了我们可如何使用动画混合树以及动画状态机来制做参数动画系统,但仅使用这两者并不够。我们还需要一些编码来控制参数动画系统内的定义参数。这里定义了两个只有动画系统需要处理的参数,即时间和Accel。我在之前环节中提到,速度参数是由其他模块控制。我编写了一个名为ParametricAnimation的类,它含有两个方法setSpeedParameter和updateSpeed。这个类可以访问动画状态机,换句话说就是Unity中玩家预制件的定义动画师组件。ParametricAnimation类本身是一个组件,它的updateSpeed函数在其更新函数每帧会调用一次:

public void setSpeed(float speed)
{
//mAnim is the Animator component which contains API to access Mecanim features
mPreviousSpeed = mSpeed;
mSpeed = speed;
mAnim.SetFloat(“Speed”,speed);
}

void updateSpeed()
{
//Setting Time parameter
float t = mAnim.GetCurrentAnimatorStateInfo(0).normalizedTime;

//Normalized time increases by 1 for looped animations after each loop ends
t -=  Mathf.FloorToInt(t);
mAnim.SetFloat(“Time”, t);

//Setting the Aceel Parameter
mAccel = mAnim.GetFloat(“Accel”);

if(Mathf.Abs(mSpeed – mPreviousSpeed) <= 0.001f)
{
if(mAccel < 0)
{
mAccel += Time.deltaTime;
}
else
{
mAccel -= Time.deltaTime;
}

if(Mathf.Abs(mAccel) <= 0.04f)
{
mAccel = 0;
}
}

else
{
mAccel += 0.7f * (mSpeed -mPreviousSpeed);
}

mAnim.SetFloat(“Accel”, mAccel);

if(mAccel <= -1)
{
mAnim.SetFloat(“Accel”, -1);
}

if(mAccel >= 1)
{
mAnim.SetFloat(“Accel”, 1);
}

mPreviousSpeed = mSpeed;

}

为了实现最佳视觉效果,速度参数要通过加速来运算,之后再传送到参数动画系统。这意味着如果你想开始运动,最好通过时间而非将一帧内的速度设置为特定值来增加速度。例如,你想从闲置切换到奔跑状态,最好是将数帧内的速度设为0至5,而不是将一帧内的速度设为5。但这要取决于每款游戏的玩法设计规则,也许有些游戏需要敏捷控制,而其他游戏则没有这种需求。

总结

美术人员和动画师更希望在整个游戏开发过程中能够掌握对自己作品的控制权,所以必须用上能够赋予他们这种能力的技术。参数动画是将动画系统从其他系统中分离出来的一个绝佳方法。使用这一系统,动画师和动画程序员就可以在运行时间更改自己的动画而无需与AI/玩法程序员打交道了。这可以让动画管道更为灵活,有助于团队取得更好的结果。

本文所创造的是一个可通过速度参数运行的简单参数动画系统。为此,我们可以使用Unity的Mecanim动画系统来创造动画状态机和动画混合树。其他诸如运行时间装置等高级动画技巧、物理动画或AI动画技巧也可以结合本文所用技巧来实现更高级的参数动画。任何有助于提升动画系统恰当响应参数变化的动画技巧都可以在参数动画系统中使用。

在此还建议各位增加一个偏离参数来提升这里所设计的运动系统,以便玩家转向或更改方向。(本文为游戏邦/gamerboom.com编译,拒绝任何不保留版权的转载,如需转载请联系:游戏邦

Designing Parametric Animation Systems

By Peyman Massoudi

Due to the presence of different people with different skills in a game development process, game developers try hard to increase the level of abstraction base on the view of the people working on different parts of a game. Parametric animation is designed for this reason. It provides some parameters as interfaces for other modules like AI or gameplay which want to use animations. Parametric animation ensures that the animation always responds well to parameter changes so other modules like AI or gameplay can change these parameters base on their own computations without any need to deal with the details of animation techniques. This article tends to show how a parametric animation system can be created and be used by other modules. In this article, Unity Mecanim animation system is used to create a parametric animation system.

Introduction

Today, making video games needs a wide area of knowledge. There exists programmers, game designers, artists and directors in a game development process and each of which is divided into different roles which need different skills. For example an artist can be an animator, modeler or concept artist or a programmer can be AI programmer, tools programmer, gameplay programmer, Network programmer or many other. To develop an entertaining game, this wide area of skills should be used appropriately together. Each person should help the development process by his/her view. To achieve this, some level of abstraction should be created between different people with different skills during game development process. This abstraction could be occurred in animation too. One way to reach this abstraction is to use parametric animations. Parametric animations separate animation system totaly from other modules which want to use it by presenting some parameters. These parameters are some numeric or boolean interfaces that let other modules to connect with the animation system. They can be anything like aim-direction, speed, force, surface gradient, health or maybe a boolean parameter like jump or many other possible parameters. By changing these parameters the animation system returns a suitable motion. There are many ways to create parametric animations like using animation blend trees and combining them with some techniques like animation layering and animation state machines. Other animation techniques can be combined to be used as parametric animation like runtime rigs or many other procedural animation techniques. The most important thing in parametric animation is that, it should respond well to parameter changes so any technique which can help the system to reach this goal can be used. After finding a way to separate details of animation from other game modules, the other modules can start making their own computations more easily. Most of the times, the animation parameters are specified from gameplay, control or AI modules like aim direction or run-speed. In next section a parametric animation system will be created by using animation blend trees and animation state machine.

Parameterizing animation

In this section a parametric locomotion animation system will be designed and implemented. To implement this parametric locomotion system, blend trees and animation state machines are used. Unity’s Mecanim animation system is used to implement this parametric locomotion system. Mecanim is selected for two reasons, first, Unity is very popular among game developers and second, Mecanim provides good features to separate animation from other modules like gameplay or AI. This article just uses one parameter to depict speed of the player. The speed parameter is calculated from other modules and it will be passed to the animation system and the animation system responds to it by selecting its best-suited animations. Figure 1 shows the animation state machine which is created for this reason. Based on the value of speed, animation system can change between idle and locomotion states.

Figure1: Animation state machine created to handle parametric animation

Before parameterizing animation, the animation team and the other teams which want to use animations should talk to each other and specify which parameters they need and specify each parameter thresholds. In this article only speed parameter is defined which has a minimum value of 2.5 m/s and maximum value of 10 m/s. The speed parameter can be modified by many other modules. For example gameplay programmers can change speed by checking how much player holds an analog stick or AI programmers can change speed base on their used algorithm for path finding, obstacle avoidance or crowd simulation.

After finding a specific parameter and its thresholds, animators can create specific animations which can handle the parameter changes to achieve best visual results.

In next three sections each of the three animation states showed in figure1 will be considered separately.

Locomotion state

In this article seven animations are used for locomotion and a blend tree is created for them which can blend between walk, run and sprint animations base on the speed parameter. Figure 2 shows the blend tree created in the locomotion state. Just note that Mecanim blend trees can blend between two or more animations with different timings like walk and run. If you want to use another animation blend tree system instead of Unity Mecanim and you don’t know how to blend between animations with different timings like walk and run, then check out this post on my blog.

As Figure 2 shows there are two walk animations, three run animations and two sprints. Before explaining why these seven animations are used, let’s consider the existence of a parameter in the animation system named “Accel”. The Accel parameter is defined within the animation system and the parametric animation users which want to control the speed know nothing about it. Accel is used to show the player is start or stop running or walking. By changing it, blend trees will blend between animations which are listed below:

1. walk, walk_accel: They are two walk animations, with same timings, but they have just one difference and that is in walk_accel animation, the head and the spine of the character is leaned forward to show that the character wants to start walking. This is also true for run, run_accel and sprint, sprint_accel.

2. run_deaccel: run_deaccel animation is like run animation with one difference and that is the character’s head and spine leans backward to show that the character wants to stop running.

Figure2: Blend trees inside the Locomotion state

Accel parameter can be altered between -1 and 1 where 1 shows that the character tends to start moving forward and -1 shows that the character tends to stop.

Idle state

There is a blend tree in idle state which can blend between two idle animations. The idle_mecanim animation is a simple animation in which player is standing and the idle_accel_mecanim animation is similar to idle_mecanim animation while the head and spine of the character leaned forward in it showing that the character wants to start moving. Figure 3 shows blend tree inside the Idle state. The Accel parameter is used to blend between idle and idle_accel.

Figure3: Blend tree inside the Idle state

Idle_mirrored state

The final state is named Idle_mirrored. It is like the idle state. This state is created to avoid foot skating. In idle_mecanim animation, the character’s left foot is in front of the character and the right foot is back showing a more aggressive style of standing. This stance pose will lead the character to some foot skates while a transition is going to be made from walk to idle. When we make a transition from walk to idle, if the character’s right foot is in front and his left foot is in back during walking, then making a transition to idle causes some foot skating because in idle animation left foot is in front and right foot is in back. To avoid foot skating the Idle_mirrored state is created. Idle_mecanim and idle_accel_mecanim animations are mirrored for this reason.

Mecanim motion retargeting system named Avatar is used to mirror these two animations. Figure 4 shows the blend tree created in Idle_mirrored state.

Figure4: Blend tree inside the Idle_mirrored state

Animation state machine parameters

Three parameters are defined within animation state machine:

1. Speed: Speed is used to blend between walk, run and sprint animations or make transition to idle states. It is the exact value that the gameplay/AI modules pass to animation system.

2. Accel: Accel is used to blend between accelerated posed animations and normal posed to show that the character wants to start or stop moving. Accel is defined within the animation state machine and other modules know nothing about it and it’s calculated in parametric animation modules.

3. Time: Time is used within the animation system to select which transition should be made, transition to Idle state or Idle_mirrored state. Time is defined within the animation state machine and other modules know nothing about it and it’s calculated in parametric animation modules.

Rules for making transitions

In Mecanim’s animation state machine, transitions can be made by satisfying defined conditions. This section covers the rules used in our parametric animation system.

The first rule is that if the speed parameter is below 2.5 then player should be stopped else he should move forward. This means that if player is in idle state and speed goes higher than 2.5 then he should make a transition to locomotion state.

The second rule is about the time of the locomotion state. To avoid foot skating, transitions from locomotion to idle should be made at some specific time of walk animation. Human walk cycle has a phase named double support. At double support phase both left and right feet are planted on the ground. Double support phase of walk animation I created is presented here. The walk animation length is 1.2 seconds:

(Time > 0.53 and Time < 0.75): Double support phase, left foot in front, right foot In back and both feet planted on the ground. If this condition is true and speed is less than 2.5 then system should make a transition to idle state

(Time < 0.19 or (Time > 0.91 and Time < 1)): Double Support phase, right foot in front, left foot in back and both feet planted on the ground. If this condition is true and speed is less than 2.5 then system should make a transition to Idle_mirrored state. Figure 5 shows the rules defined for transition from locomotion to Idle state.

Figure5: Conditions defined for transition from Locomotion to Idle

Figure 6 and 7 shows the rules defined for transition from locomotion to Idle_mirrored state. In mecanim defining two or more transitions from one state to another acts like ‘OR’ Boolean operator while each transition acts like its operands.

Figure6: Conditions defined for transition1 from Locomotion to Idle_mirrored

Figure7: Conditions defined for transition2 from Locomotion to Idle_mirrored

Figure 8 shows the rule defined for transition from idle and idle_mirrored to locomotion.

Figure8: Conditions defined for transition from Idle and Idle_mirrored to Locomotion

Passing parameters

Previous sections showed how we can use animation blend trees and animation state machines to make a parametric animation system but just using these two is not enough for our purposes. There need to be some coding to control the parameters defined within the scope of parametric animation system. Two parameters are defined which only the animation system have to deal them, Time and Accel. As I mentioned in previous sections the speed parameter is controlled by other modules. I wrote a class named ParametricAnimation with two methods, setSpeedParameter and updateSpeed. This class has access to the animation state machine or in other words the defined Animator component of the player’s prefab in Unity. The ParametricAnimation class is a component itself and its updateSpeed function is called once per frame in its update function:

public void setSpeed(float speed)
{
//mAnim is the Animator component which contains API to access Mecanim features
mPreviousSpeed = mSpeed;
mSpeed = speed;
mAnim.SetFloat(“Speed”,speed);
}

void updateSpeed()
{
//Setting Time parameter
float t = mAnim.GetCurrentAnimatorStateInfo(0).normalizedTime;

//Normalized time increases by 1 for looped animations after each loop ends
t -=  Mathf.FloorToInt(t);
mAnim.SetFloat(“Time”, t);

//Setting the Aceel Parameter
mAccel = mAnim.GetFloat(“Accel”);

if(Mathf.Abs(mSpeed – mPreviousSpeed) <= 0.001f)
{
if(mAccel < 0)
{
mAccel += Time.deltaTime;
}
else
{
mAccel -= Time.deltaTime;
}

if(Mathf.Abs(mAccel) <= 0.04f)
{
mAccel = 0;
}
}

else
{
mAccel += 0.7f * (mSpeed -mPreviousSpeed);
}

mAnim.SetFloat(“Accel”, mAccel);

if(mAccel <= -1)
{
mAnim.SetFloat(“Accel”, -1);
}

if(mAccel >= 1)
{
mAnim.SetFloat(“Accel”, 1);
}

mPreviousSpeed = mSpeed;

}

To achieve best visual results, the speed parameter should be calculated by acceleration and then be passed to parametric animation system. This means if you want to start running, it is better to increase speed through time rather than setting the speed to a specific value at just one frame. For example if you want to go from idle to run it’s better to go from speed 0 to 5 through several frames not just setting the speed to 5 in just one frame. However this depends on the gameplay designed rules for each game, maybe some games need agile controls and others do not.

Conclusion

Artists and animators prefer to have control over their artworks through the whole process of game development so a technique is needed to give this power to them. Parametric animation is a great way to separate the animation system from other systems. By using it animators and animation programmers can always change the animations at runtime without any need to deal with other programmers like AI/GamePlay programmers. This makes the animation pipeline very flexible and helps the team to achieve better results.

In this article a simple parametric animation system was created which can work by a speed parameter. For this, an animation state machine and animation blend trees were created by using unity’s Mecanim animation system. Other advanced animation techniques like runtime rigs, Physics-Based Animation or AI-Based animation techniques can be combined with the techniques used in this article to achieve more advanced parametric animation. Any animation technique which can help the animation system to respond well to the parameter changes can be used in a parametric animation system.

A suggestion to improve the locomotion system designed here is to add a deviation parameter which can make the player turn or change direction.(source:gamedev


上一篇:

下一篇: