<Rigidbody/>
Rigidbody 提供质量、速度和摩擦,使实体响应重力、力、力矩及其他全局物理行为。
入门
提示
安装 ammo.js 依赖:npm i sync-ammo,并启用 Application usePhysics。
根 Application 的 usePhysics 会启用物理系统,使实体参与模拟。
详情见 React 物理。
用法
为同一实体添加 Rigidbody 和 Collision,后者定义形状。
<Application usePhysics>
<Entity>
<Rigidbody type="box" />
</Entity>
</Application>
- 演示
- 代码
rigidbody-example.jsx
// ↑ imports hidden
const Box = ({ color, ...entityProps }) => {
const material = useMaterial({ diffuse: color })
return (
<Entity {...entityProps}>
<RigidBody type="dynamic" mass={1} restitution={0.2} friction={0.5} />
<Collision type="box" />
<Render type="box" material={material} />
</Entity>
)
}
export const RigidbodyExample = () => {
const [boxes, setBoxes] = useState([
{ id: 1, position: [0, 20, 0], color: '#e74c3c' },
{ id: 2, position: [0.5, 8, 0], color: '#3498db' },
{ id: 3, position: [-0.5, 11, 0], color: '#2ecc71' }
])
const addBox = () => {
const colors = ['#e74c3c', '#3498db', '#2ecc71', '#f39c12', '#9b59b6', '#1abc9c']
const color = colors[Math.floor(math.random(0, colors.length))]
const box = {
id: Date.now(),
position: [math.random(-2, 2), 5, math.random(-2, 2)],
rotation: [math.random(0, 360), math.random(0, 360), math.random(0, 360)],
color
}
setBoxes([...boxes, box])
}
return (<>
{/* Ground plane */}
<Entity key="ground" position={[0, -0.6, 0]} scale={[10, 0.1, 10]}>
<RigidBody type="static" friction={0.5} />
<Collision type="box" halfExtents={[5, 0.1, 5]} />
</Entity>
{/* Falling boxes */}
{ boxes.map((props) => <Box key={props.id} {...props} />) }
{/* UI */}
<div className="overlay">
<button onClick={addBox}>Add Box</button>
</div>
</>)
}
属性
| 名称 | 类型与说明 | 默认值 |
|---|---|---|
type? | "dynamic" | "kinematic" | "static"设置决定模拟方式的刚体类型。 | "static" |
angularDamping? | number设置或获取刚体角速度随时间衰减的速率。 | - |
angularFactor? | [number, number, number]设置或获取各轴角运动缩放因子,仅用于 BODYTYPE_DYNAMIC。默认各轴为 1,可自由旋转。更新物理刚体时使用 setter。 | - |
angularVelocity? | [number, number, number]设置或获取刚体绕各世界轴的旋转速度,仅用于 BODYTYPE_DYNAMIC。更新物理刚体时使用 setter。 | - |
friction? | number设置或获取两个刚体接触时的摩擦系数,越高摩擦越大,范围 0 到 1,默认 0.5。 | - |
gravityScale? | number设置或获取此刚体对世界重力(RigidBodyComponentSystem#gravity)的倍率,仅用于 BODYTYPE_DYNAMIC,默认 1。0 忽略重力,负数使其上升。若要独立重力方向,设为 0 并每帧用 applyForce 自行施力。 | - |
group? | number设置或获取刚体所属碰撞组,与 mask 配合控制碰撞。默认动态为 1、静态为 2、运动学为 4。设置 RigidBodyComponent#type 会重置组为新类型默认值,因此应先设置类型,再设置组。 | - |
linearDamping? | number设置或获取刚体线速度随时间衰减的速率,默认 0。 | - |
linearFactor? | [number, number, number]设置或获取各轴线性运动缩放因子,仅用于 BODYTYPE_DYNAMIC。默认各轴为 1,可自由移动。更新物理刚体时使用 setter。 | - |
linearVelocity? | [number, number, number]设置或获取刚体在指定方向的速度,仅用于 BODYTYPE_DYNAMIC。更新物理刚体时使用 setter。 | - |
mask? | number设置或获取刚体可碰撞组的 16 位掩码,低 8 位保留给引擎。静态默认 65533,与其他静态刚体之外的对象碰撞;动态和运动学默认 65535,与全部对象碰撞。设置 type 会重置掩码,因此应在类型之后设置 mask。 | - |
mass? | number设置或获取质量,仅对 BODYTYPE_DYNAMIC 有效,其他类型具有无限质量。默认 1。 | - |
restitution? | number设置或获取恢复系数,控制碰撞损失的能量。碰撞双方的恢复系数相乘,乘积 0 表示能量全部损失,1 表示无损失。范围 0 到 1,默认 0。 | - |
rollingFriction? | number设置或获取垂直于接触点的扭转摩擦,默认 0。 | - |
system? | ComponentSystem创建此组件的 ComponentSystem。 | - |
entity? | Entity此组件挂载的 Entity。 | - |
enabled? | boolean设置或获取组件的启用状态。 | - |