namespace UnityEngine
{
//Control of an object's position through physics simulation.
[NativeHeader("Modules/Physics/Rigidbody.h")]
[RequireComponent(typeof(Transform))]
public class Rigidbody : Component
{
//“影响”如何精确地解析刚体关节和碰撞接触。解算器速度的迭代,默认:1
public int solverVelocityIterations { get; set; }
//刚体的最大角速度,以弧度每秒测量。 (默认7),范围:{0,无穷大}。
public float maxAngularVelocity { get; set; }
//休眠阈值:0.005。请注意,sleepThreshold是指能量,而不是速度
public float sleepThreshold { get; set; }
//“决定”如何精确地解析刚体关节和碰撞接触。默认:6
public int solverIterations { get; set; }
//插值允许你在固定帧率下平滑运行物理效果。默认:None
//插值:插值总是会滞后一点,但可以比外推更平滑。
//外推:外推将基于当前速度预测刚体的位置。
public RigidbodyInterpolation interpolation { get; set; }
public Vector3 velocity { get; set; }
//刚体的角速度矢量,单位是弧度每秒。
public Vector3 angularVelocity { get; set; }
//The drag of the object.
public float drag { get; set; }
//The angular drag of the object.
public float angularDrag { get; set; }
//The mass of the rigidbody.
public float mass { get; set; }
//刚体脱离穿透状态时的最大速度。
public float maxDepenetrationVelocity { get; set; }
public bool isKinematic { get; set; }
//控制物理是否会改变对象的旋转。
public bool freezeRotation { get; set; }
//Controls which degrees of freedom are allowed for the simulation of this Rigidbody.
public RigidbodyConstraints constraints { get; set; }
//The Rigidbody's collision detection mode.
public CollisionDetectionMode collisionDetectionMode { get; set; }
//The center of mass relative to the transform's origin.
public Vector3 centerOfMass { get; set; }
//The center of mass of the rigidbody in world space (Read Only).
public Vector3 worldCenterOfMass { get; }
//惯性张量的旋转。
public Quaternion inertiaTensorRotation { get; set; }
//相对于质心的质量的对角惯量张量。
public Vector3 inertiaTensor { get; set; }//惯量张量
//是否应该启用碰撞检测? (默认情况下总是启用)。
public bool detectCollisions { get; set; }
//Controls whether gravity affects this rigidbody.
public bool useGravity { get; set; }
//The position of the rigidbody.
public Vector3 position { get; set; }
//The rotation of the Rigidbody.
public Quaternion rotation { get; set; }
//Applies a force to a rigidbody that simulates explosion effects.
// explosionForce:
// The force of the explosion (which may be modified by distance).
// explosionPosition:
// The centre of the sphere within which the explosion has its effect.
// explosionRadius:
// The radius of the sphere within which the explosion has its effect.
//
// upwardsModifier:
// Adjustment to the apparent position of the explosion to make it seem to lift
// objects.
//
// mode:
// The method used to apply the force to its targets.
public void AddExplosionForce(float explosionForce, Vector3 explosionPosition, float explosionRadius, [DefaultValue("0.0f")] float upwardsModifier, [DefaultValue("ForceMode.Force)")] ForceMode mode);
// Adds a force to the Rigidbody.
//
// 参数:
// force:
// Force vector in world coordinates.
//
// mode:
// Type of force to apply.
public void AddForce(Vector3 force, [DefaultValue("ForceMode.Force")] ForceMode mode);
// Applies force at position. As a result this will apply a torque and force on the object.
public void AddForceAtPosition(Vector3 force, Vector3 position, [DefaultValue("ForceMode.Force")] ForceMode mode);
// Adds a force to the rigidbody relative to its coordinate system.
//
// 参数:
// force:
// Force vector in local coordinates.
//
// mode:
public void AddRelativeForce(Vector3 force, [DefaultValue("ForceMode.Force")] ForceMode mode);
// Adds a torque to the rigidbody relative to its coordinate system.
//
// 参数:
// torque:
// Torque vector in local coordinates.
//
// mode:
public void AddRelativeTorque(Vector3 torque, [DefaultValue("ForceMode.Force")] ForceMode mode);
// 摘要:
// Adds a torque to the rigidbody.
//
// 参数:
// torque:
// Torque vector in world coordinates.
//
// mode:
[ExcludeFromDocs]
public void AddTorque(Vector3 torque);
// 摘要:
// Adds a torque to the rigidbody.
//
// 参数:
// torque:
// Torque vector in world coordinates.
//
// mode:
public void AddTorque(Vector3 torque, [DefaultValue("ForceMode.Force")] ForceMode mode);
// The closest point to the bounding box of the attached colliders.
public Vector3 ClosestPointOnBounds(Vector3 position);
// The velocity of the rigidbody at the point worldPoint in global space.
public Vector3 GetPointVelocity(Vector3 worldPoint);
// The velocity relative to the rigidbody at the point relativePoint.
public Vector3 GetRelativePointVelocity(Vector3 relativePoint);
//Is the rigidbody sleeping?
public bool IsSleeping();
// 摘要:
// Moves the kinematic Rigidbody towards position.
//
// 参数:
// position:
// Provides the new position for the Rigidbody object.
public void MovePosition(Vector3 position);
// Rotates the rigidbody to rotation.
//
// 参数:
// rot:
// The new rotation for the Rigidbody.
public void MoveRotation(Quaternion rot);
//Reset the center of mass of the rigidbody.
public void ResetCenterOfMass();
//Reset the inertia tensor value and rotation.
public void ResetInertiaTensor();
//设置质量基于附加的碰撞器假设一个恒定的密度。
public void SetDensity(float density);
//Forces a rigidbody to sleep at least one frame.强制刚体至少休眠一帧。 若刚体所在的Transform位置、旋转、缩放数值没有变化,刚体就会自动休眠。什么是休眠:休眠=Transform不变,Transform不变=休眠
public void Sleep();
public bool SweepTest(Vector3 direction, out RaycastHit hitInfo, [DefaultValue("Mathf.Infinity")] float maxDistance, [DefaultValue("QueryTriggerInteraction.UseGlobal")] QueryTriggerInteraction queryTriggerInteraction);
// 摘要:
// Like Rigidbody.SweepTest, but returns all hits.
//
// 参数:
// direction:
// The direction into which to sweep the rigidbody.
//
// maxDistance:
// The length of the sweep.
//
// queryTriggerInteraction:
// Specifies whether this query should hit Triggers.
//
// 返回结果:
// An array of all colliders hit in the sweep.
public RaycastHit[] SweepTestAll(Vector3 direction, [DefaultValue("Mathf.Infinity")] float maxDistance, [DefaultValue("QueryTriggerInteraction.UseGlobal")] QueryTriggerInteraction queryTriggerInteraction);
//Forces a rigidbody to wake up.
public void WakeUp();
}
}
namespace UnityEngine
{
//Use ForceMode to specify how to apply a force using Rigidbody.AddForce.
public enum ForceMode
{
//Add a continuous force to the rigidbody, using its mass. 向刚体添加一个连续的力,与刚体的质量有关。
Force = 0,
//Add an instant force impulse to the rigidbody, using its mass.向刚体添加一个瞬时的力脉冲,与刚体的质量有关。
Impulse = 1,
//Add an instant velocity change to the rigidbody, ignoring its mass.给刚体添加一个瞬时速度变化,忽略它的质量。
VelocityChange = 2,
//Add a continuous acceleration to the rigidbody, ignoring its mass.给刚体添加一个连续的加速度,忽略它的质量。
Acceleration = 5
}
}
ForceMode为枚举类型,用来控制力的作用方式,有4个枚举成员,在以下举例中均设刚体质量为m=2.0f,力向量为f=(10.0f,0.0f,0.0f)。
(1)ForceMode.Force:默认方式,使用刚体的质量计算,以每帧间隔时间为单位计算动量。设FixedUpdate()的执行频率采用系统默认值(即0.02s),,则由动量定理
f•t=m•v
可得:10*0.02=2*v1,从而可得v1=0.1m/s,很多博客这里是m/帧,后面又转换成了m/s,那些结果是错的,是单位搞错了,请重视,以免产生困扰。
(2)ForceMode.Impulse:此种方式采用瞬间力作用方式,使用刚体的质量计算,把 t 的值默认为1,不再采用系统的帧频间隔,即
f•1.0=m•v
即可得v1=f/m=10.0/2.0=5.0m/s
(3)ForceMode.VelocityChange:此种作用方式下将忽略刚体的实际质量采用默认质量1.0,同时也忽略系统的实际帧频间隔,采用默认间隔1.0,即
f•1.0=1.0•v
即可得v1=f=10.0m/s
(4)ForceMode.Acceleration:在此种作用方式下会忽略刚体的实际质量而采用默认值1.0f,时间间隔以系统帧频间隔计算(默认值为0.02s),即
f•t=1.0•v
即可得v1= f•t=10*0.02=0.2m/s
这里就来解释一下对于官方注释中的continuous的理解方式,ForceMode.Force模式,是在一秒钟的时间内持续施加的力的和为f,所以在FixedUpdate默认设置中执行时,t为0.02。