Unity_Rigidbody

Unity_Rigidbody

Rigidbody:刚体,可以使游戏对象在物理系统的控制下进行运动。多用于游戏对象在现实世界中的物理特性。

注意:通常在OnFixedUpdate()中执行Rigidbody类,因为物理相关计算一般需要在固定时间间隔内进行计算。

注意:当rigidbody接管了物体的运动时,不应该对物体进行Trasnsform属性的直接修改,包括Translate()。

注意:IsKinematic属性 -- 使物体不受物理引擎控制,可以手动通过脚本控制 (Can only be manipulated by its Transform)

常用方法:

AddForce() -- 给刚体施加一个顺时力

AddTorque() -- 给刚体施加一个扭矩

Sleep() -- 可使刚体进入休眠状态(至少会休眠一帧)
  当物体进入sleep模式后,no processor time is spent updating the Rigidbody until the next time it is "awoken" (set in motion again)

https://blog.csdn.net/m0_37283423/article/details/72639358

自动休眠:Rigidbody sleeping happens completely automatically. Whenever a rigidbody is slower than the sleepAugularVelocity and sleepVelocity, it will start falling asleep. After a few frames of resting, it will than be set to sleep.

这个自动休眠的机制也解释了为什么两个物体碰撞发生的前提是运动的那个物体必须带有刚体,而如果带有刚体的物体不运动而是另一个不带刚体的物体运动碰撞检测不到的原因 -- 刚体已处于休眠状态了,不进行碰撞检测模拟了。SleepAngularVelocity和sleepVelocity的值在(Edit -> Project Settings -> Physics)中可手动修改。

属性: 

mass:默认单位kg。官方建议同一个游戏场景中,物体质量的差别不要超过100倍

drag:移动时受到的空气阻力

angular drag:旋转时受到的空气阻力

IsKinematic:开启动力学。开启时不再受到物理引擎的影响(比如重力、碰撞等),只能通过Transform进行操作。

Interpolate:控制刚体运动时的抖动情况。抖动情况是由于运动模拟计算过程中物理运算和画面更新不同步造成的。
  None -- 不进行差值
  Interpolate -- 内插值:基于前一帧的Transform来平滑此次的Transform
  Extrapolate -- 外插值(推断、外推):基于下一帧的Transform来平滑此次的Transform
    用最近几帧的数值来推测未来的值,Used in animation, physics and multiplayer.

Collision Detection: 防止快速移动的物体未触发碰撞检测就穿过其他物体

Discrete -- 离散。用于普通碰撞检测(默认值)。
  对场景中所有其他碰撞体都使用离散碰撞检测
  其他碰撞体在对其进行碰撞测试时将使用离散碰撞检测

Continuous -- 连续。物理性能开销大,如果没有快速物体的碰撞问题,请设置为discrete
  对动态碰撞体(带刚体的即为动态)使用离散碰撞检测;
    对设置成Continuous Dynamic的其他刚体,与此刚体碰撞时使用连续碰撞检测;
    其他刚体将使用离散碰撞检测。
  对静态碰撞体使用扫描式的连续碰撞检测(sweep-based continuous collision detection);
  碰撞到一起的时候才会使用连续动态检测

Continuous Dynamic -- 连续动态
  对设置为连续和连续动态碰撞的刚体使用基于扫描的连续碰撞检测。
  对静态碰撞器使用连续碰撞检测
  对所有其他碰撞体使用离散碰撞检测

Continuous Speculative -- 对刚体和碰撞体使用speculative continuous collision detection
  唯一的可以设置为Kinematic的CCD模式
  比sweep-based continuous collision detection更少性能消耗

sweep-based和speculative continuous collision detection的详解:
  https://docs.unity3d.com/Manual/ContinuousCollisionDetection.html

sweep-based continuous collision detection

use Time of Impact algorithm to compute potential collisions for an object by sweeping its forward trajectory using its current velocity.

该方法基于线性扫描,将会忽略物体的旋转运动,如下图碰撞将检测不出来

speculative CCD:

works by increasing an object's broad-phase axis-aligned minimum bounding box (AABB), based on the object's linear and angular motion. 找到下一个physics step中潜在的接触 potential contacts,这些接触会被输入到solver中,这样可以确保所有的接触约束都得到满足,这样对象就不会穿越任何碰撞。

比较:

Speculative CCD效率比Sweep-based CCD高
  Speculative仅在碰撞检测阶段进行碰撞计算,sweep-based而不是在solving和intergrating阶段计算

Speculative CCD将会检测到被sweep-based丢失的碰撞
  Speculative的axis-aligned minimum bounding box基于线速度和角速度,不会miss上图情况

(非官方)总结:
  Continuous和Continuous Dynamic的区别是
    Continuous对Continuous使用discrete检测
    Continuous Dynamic对Continuous使用continuous检测
  Continuous适用于将会被高速移动物体撞上的物体
  Continuous Dynamic适用于高速移动的物体本身

注意:使用了非discrete的碰撞检测后还是出现了穿透怎么办呢?
    解决方法:使用射线检测的方式进行判断
  Continuous Collision Detection is only supported for Rigidbodies with Sphere, Capsule or Box Colliders
    若碰撞器不是球体、胶囊体、长方体,则肯定使用的是Discrete检测

碰撞体:

碰撞检测技术(引擎层面解析):http://www.cppblog.com/mybios/archive/2006/11/19/15408.html

The physics engine will not calculate a collision unless both objects have a collider attached.

原文地址:https://www.cnblogs.com/FudgeBear/p/13670958.html