<划重点的UNITY2017> 物理系统概述

来源:互联网 发布:罗马复兴兵种数据 编辑:程序博客网 时间:2024/04/29 16:36

Unity2017出来之后,很多有意思的新功能出现了。我想通过这个栏目,整理一下Unity的重点知识。参考Unity的最新文档。国外的知识都共享,而我看到很多国内教学都是二道贩子,用一些没啥价值的知识虎龙小白。本来就是二手知识(来源于最新官网文档)还收钱,这也是导致国内外知识传播时间和空间差距太大的原因之一。楼主水平有限,尽全力去整理总结,本栏目仅仅包含重点知识以及一些有意思的环节。一方面通过博客来提升自己,另一方面,希望能够帮到一些朋友。如果觉得我的博文有一点点用处,那么我就认为我下班后的时间有了意义。与大家共勉,谢谢。

1.物理系统概述
(1)刚体
A.you should apply forces to push the GameObject and let the physics engine calculate the results.
我们应该使用AddForce的方式来触发引擎的物理计算,来代替脚本当中生硬的transform计算。楼主曾经利用脚本写过一个ACT小项目,花了很大时间和经历,有兴趣的朋友可以给我投两个硬币:
DreamOfWitch项目
完全是脚本写的,非常费劲,而且不容易出效果。如果能够充分利用物理引擎会达到一个很好的效果。

B.you may want to control your character directly from script code but still allow it to be detected by triggers (see Triggers below). This kind of non-physical motion produced from a script is known as kinematic motion.
还记得刚体上的Kinematic吗?这个属性如果设置为True,那么可以让拥有刚体的物体摆脱物理计算。如果你想用代码控制物体,但是还想让物体有刚体,IsKinematic为True是一个好选择。

C.When a Rigidbody is moving slower than a defined minimum linear or rotational speed, the physics engine assumes it has come to a halt. When this happens, the GameObject does not move again until it receives a collision or force, and so it is set to “sleeping” mode. This optimisation means that no processor time is spent updating the Rigidbody until the next time it is “awoken” (that is, set in motion again).
需要掌握刚体睡眠的概念。当物体移动的很慢,旋转的很慢(在一个阈值之内的话),系统默认她为睡眠状态。Sleep状态下,直到再一次受到力或者受到碰撞她才会WakeUp。
在睡眠状态下,该物体的刚体不需要处理更新RigidBody的处理器时间。

D.However, a GameObject might fail to wake up if a Static Collider (that is, one without a Rigidbody) is moved into it or away from it by modifying the Transform position. This might result, say, in the Rigidbody GameObject hanging in the air when the floor has been moved out from beneath it. In cases like this, the GameObject can be woken explicitly using the WakeUp function.
如果是一个静态碰撞体(没有刚体,只有Collider组件)通过改变Transform的方式移入或者移出物体,不会触发刚体的物理系统。可以通过脚本WakeUp已经睡着的物体喔。

(2)碰撞体

A. compound colliders can often approximate the shape of an object quite well while keeping a low processor overhead. Further flexibility can be gained by having additional colliders on child objects (eg, boxes can be rotated relative to the local axes of the parent object). When creating a compound collider like this, there should only be one Rigidbody component, placed on the root object in the hierarchy.
复合碰撞体可以使用基础的碰撞体来模拟复杂的碰撞体。父物体挂在一个刚体,子物体们只需要挂在碰撞体就好了。这种方式还可以降低处理器开销。
注意这种情况放大、旋转,可能会破坏碰撞体的结构。

B.a mesh collider will normally be unable to collide with another mesh collider (ie, nothing will happen when they make contact). You can get around this in some cases by marking the mesh collider as Convex in the inspector. This will generate the collider shape as a “convex hull” which is like the original mesh but with any undercuts filled in. The benefit of this is that a convex mesh collider can collide with other mesh colliders so you may be able to use this feature when you have a moving character with a suitable shape.
网格碰撞一般是不会和其他碰撞体产生反应的。如果想让其产生反映,试试Convex属性吧,他会产生一个类似于基础碰撞的闭包。根据情况灵活使用这个特性。

C.Colliders can be added to an object without a Rigidbody component to create floors, walls and other motionless elements of a scene. These are referred to as static colliders. In general, you should not reposition static colliders by changing the Transform position since this will impact heavily on the performance of the physics engine. Colliders on an object that does have a Rigidbody are known as dynamic colliders. Static colliders can interact with dynamic colliders but since they don’t have a Rigidbody, they will not move in response to collisions.
这里介绍两个概念:
静态碰撞:物体只有Collider组件,而没有RigidBody组件。比如建筑,环境可以设置成静态碰撞。
动态碰撞:物体有Collider和RigidBody组件。
当静态碰撞和动态碰撞作用的时候,静态碰撞不会受到碰撞的影响。这也是将其作为环境碰撞体的原因。

(3)物理材质
A.When colliders interact, their surfaces need to simulate the properties of the material they are supposed to represent. For example, a sheet of ice will be slippery while a rubber ball will offer a lot of friction and be very bouncy. Although the shape of colliders is not deformed during collisions, their friction and bounce can be configured using Physics Materials. Getting the parameters just right can involve a bit of trial and error but an ice material, for example will have zero (or very low) friction and a rubber material with have high friction and near-perfect bounciness.
当物体进行摩擦的时候,可以通过物理材质来模拟冰、橡胶的效果。虽然Collider的形状不会受影响。

(4)碰撞详述
A.The physics engine assumes that static colliders never move or change and can make useful optimizations based on this assumption. Consequently, static colliders should not be disabled/enabled, moved or scaled during gameplay. If you do change a static collider then this will result in extra internal recomputation by the physics engine which causes a major drop in performance. Worse still, the changes can sometimes leave the collider in an undefined state that produces erroneous physics calculations. For these reasons, only colliders that are Rigidbodies should be altered. If you want a collider object that is not affected by incoming rigidbodies but can still be moved from a script then you should attach a Kinematic Rigidbody component to it rather than no Rigidbody at all.
关于静态碰撞体的一些概念:静态碰撞不应该移动、放缩、enable。这些会造成额外物理引擎资源消耗以及其对碰撞的错误判断。换句话说,只有有动态碰撞体才可以变化。如果你想让一个物体不受到其他刚体的影响,但是只受到脚本的影响,使用运动学碰撞体(Kinematic为True的那种)。

B.This is a GameObject with a Collider and a normal, non-kinematic Rigidbody attached. Rigidbody colliders are fully simulated by the physics engine and can react to collisions and forces applied from a script. They can collide with other objects (including static colliders) and are the most commonly used Collider configuration in games that use physics.
关于动态刚体,是引擎当中最常用的碰撞体了。可以受到力,受到其他刚体的影响。

C.This is a GameObject with a Collider and a kinematic Rigidbody attached (ie, the IsKinematic property of the Rigidbody is enabled). You can move a kinematic rigidbody object from a script by modifying its Transform Component but it will not respond to collisions and forces like a non-kinematic rigidbody. Kinematic rigidbodies should be used for colliders that can be moved or disabled/enabled occasionally but that should otherwise behave like static colliders.
关于运动学碰撞器,可以用脚本控制Transform来完成其移动。但是不受到其他非运动学刚体和力的影响。想想滑动门吧…
Even when immobile, kinematic rigidbody colliders have different behavior to static colliders.
运动学刚体可以用来制作固定在空中的碰撞体。类似于马里奥的天空砖块。

A Rigidbody component can be switched between normal and kinematic behavior at any time using the IsKinematic property.
同时刚体组件可以在正常的状态和运动学状态进行切换,来完成对应的效果。
这里我举个栗子。比如一个士兵在巡逻,游戏人物的身体每一个部分都可以绑定刚体。正常状态下是在动画控制下的。此时身体部位是Kinematic为true的。当这个士兵踩到地雷的时候,所有的刚体都设置为Kinematic为false,此时身体的每一部分都是正常物理计算的。在爆炸的效果中,可以模拟令人信服的效果。

(5)关节
You can attach one rigidbody object to another or to a fixed point in space using a Joint component. Generally, you want a joint to allow at least some freedom of motion and so Unity provides different Joint components that enforce different restrictions.

利用关节来允许部分的自由。举个例子:可以活动的门,铰链等等。

(6)角色控制器
In 3D physics, this type of behaviour can be created using a Character Controller. This component gives the character a simple, capsule-shaped collider that is always upright. The controller has its own special functions to set the object’s speed and direction but unlike true colliders, a rigidbody is not needed and the momentum effects are not realistic.

我并不建议使用Character Controller。虽然他方便,有这个组件就不用RigidBody,但是她不能够出色的模拟动量效应。如果你精益求精,不建议使用。