这篇文章将详细探讨Unity的2D物理性组件 ,与之等价的3D物理性组件请参考Physics 3D Reference 。 参阅Physics 2D以便更好的进行自定义设置 ;
#Rigibody 2D
编程接口 – SWITCH TO SCRIPTING
通过添加Rigibody-2D组件,可以将一个游戏对象置于physics engine[1]的作用下 ,并且 ,它的许多概念都和标准Rigidbody组件相似 ,是从后者移植过来的 。区别在于 ,Rigidbody-2D是用于2D场景的 ,游戏对象只能在XY平面进行移动变换 ,并且只能以垂直于2D平面的轴为轴心进行旋转 ;
取决于该组件的Body Type的不同 , 相关的设置选项也会不同 ;
Rigidbody 2D 的工作机制
Usually, the Unity Editor’s Transform component defines how a GameObject (and its child GameObjects) is positioned, rotated and scaled within the Scene . When it is changed, it updates other components, which may update things like where they render or where colliders are positioned. The 2D physics engine is able to move colliders and make them interact with each other, so a method is required for the physics engine to communicate this movement of colliders back to the Transform components. This movement and connection with colliders is what a Rigidbody 2D component is for.
通常来说 ,Transform组件定义了一个GameObject(和其child GameObject)在scene(场景)中的定位,旋转和缩放(称之为空间状态) ,当它的参数发生变化时(或者说游戏对象的空间状态发生变化时) ,它就会更新其它的组件,就比如游戏对象在何处被渲染、colliders(碰撞体)在何处定位 ;于此相对应的是 ,2D物理引擎有能力移动场景中带碰撞体的GameObject ,并且使它们之间进行碰撞交互 ,这就会导致GameObject的空间状态发生改变 ,所以我们需要通过一种途径 ,把新的空间状态反馈给Transform ,这就是Rigibody 2D针对碰撞体的功能实现 ;
The Rigidbody 2D component overrides the Transform and updates it to a position/rotation defined by the Rigidbody 2D. Note that while you can still override the Rigidbody 2D by modifying the Transform component yourself (because Unity exposes all properties on all components), doing so will cause problems such as GameObjects passing through or into each other, and unpredictable movement.
Rigibody 2D组件使GameObject的运动受物理引擎的支配 ,而不是靠人为修改Transform ,并且 ,由于GameObject的空间状态发生了变化 ,RigidBody还会实时的更新Transform ;需要谨记的是 ,运用Rigidbody控制的物体移动 ,是受物理引擎控制的 ,而使用Transform进行的物体运动 ,不受物理引擎的控制 ,当你的Gameobject添加了Rigidbody 2D后 ,即受到物理引擎的控制 , 就不要使用Transform来控制GameObject的运动 , 因为这样就会导致一些严重的运动失常问题 , 比如物体直接穿过和嵌入其它物体、不可预期的失常运动 ;
Any Collider 2D component added to the same GameObject or child GameObject is implicitly attached to that Rigidbody 2D. When a Collider 2D is attached to the Rigidbody 2D, it moves with it. A Collider 2D should never be moved directly using the Transform or any collider offset; the Rigidbody 2D should be moved instead. This offers the best performance and ensures correct collision
detection. Collider 2Ds attached to the same Rigidbody 2D won’t collide with each other. This means you can create a set of colliders that act effectively as a single compound collider, all moving and rotating in sync with the Rigidbody 2D.
任何一个被添加到GameObject上的Collider 2D都会被隐形的附加给Rigidbody 2D ,这也意味着 ,在用Rigidbody 2D来控制物体的运动时(即把物体置于物理引擎的作用下) ,Collider 2D会随之而运动 ;必须谨记的是 ,若想使一个Collider 2D正常运动 ,那么就不能使用Transform来进行控制 ,亦或者是collider-offset[2] , 正确的做法是使用Rigidbody 2D提供的API来进行控制 , 只有这样才能保证最好的执行效果和正确的碰撞检测 ;多个Collider 2D若同时被附加到同一个Rigidbody 2D上 ,彼此之间不会发生碰撞 , 与之相反的是 ,你可以叠加多个Collider 2D ,从而使碰撞的效果更明显 ,并且和Rigidbody 2D同步的进行空间状态的改变;
When designing a Scene, you are free to use a default Rigidbody 2D and start attaching colliders. These colliders allow any other colliders attached to different Rigidbody 2Ds to collide with each other.
当你在设计一个场景时 , 你可以不受限制的为默认的Rigidbody 2D添加不止一个的Collider 。并且与游戏场景中其它附加了Collider的Rigidbody 2D进行碰撞交互 ;
[1]physics engine : A system that simulates aspects of physical systems so that objects can accelerate correctly and be affected by collisions, gravity and other forces.More info
[2]collider-offset : 可以把Collider看作是一个用于碰撞检测的几何结构 ,这个几何结构和GameObject的Mesh网格重合 ,并且被附加到了Mesh网格上 。而Rigidbody不是一种独立于GameObject的实体 ,它被添加到GameObject上 ,使GameObject受物理引擎支配 ,在这种情况下 ,Rigidbody 2D就代表着Gameobject;