<Rigidbody/>
<Rigidbody/> コンポーネントは <Entity/> に質量、速度、摩擦などの物理プロパティを付与し、グローバル物理シミュレーションとの対話を可能にします。<Rigidbody/> を持つ <Entity/> は重力、力、トルク、その他の物理挙動に反応します。
PlayCanvas の物理システムの仕組みについては 物理ドキュメント を、React 固有の詳細については React の物理 ガイドを参照してください。
はじめに
ヒント
PlayCanvas で物理を使用するには ammo.js と <Application usePhysics/> の有効化が必要です。アプリ内で npm i sync-ammo を実行してください。
<Rigidbody/> コンポーネントを使用するには、sync-ammo 依存関係をインストールし、ルートの <Application/> コンポーネントに <Application usePhysics/> Prop を設定する必要があります。これにより物理システムが有効になり、Entity が物理シミュレーションと対話できるようになります。
詳細は React の物理 ガイドを参照してください。
使い方
<Entity/> に <Rigidbody/> コンポーネントを追加します。剛体の形状を定義するために <Collision/> コンポーネントも Entity に追加する必要があります。
<Application usePhysics>
<Entity>
<Rigidbody type="box" />
</Entity>
</Application>
- Demo
- Code
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>
</>)
}
PlayCanvas ドキュメントの RigidBody Component で詳細を確認できます。
プロパティ
| Name | Type | Default |
|---|---|---|
type? | "dynamic" | "kinematic" | "static"Sets the rigid body type determines how the body is simulated. | "static" |
angularDamping? | numberSets the rate at which a body loses angular velocity over time.
Gets the rate at which a body loses angular velocity over time. | - |
angularFactor? | [number, number, number]Sets the scaling factor for angular movement of the body in each axis. Only valid for rigid
bodies of type BODYTYPE_DYNAMIC. Defaults to 1 in all axes (body can freely rotate).
Gets the scaling factor for angular movement of the body in each axis. | - |
angularVelocity? | [number, number, number]Sets the rotational speed of the body around each world axis.
Gets the rotational speed of the body around each world axis. | - |
body? | any | - |
friction? | numberSets the friction value used when contacts occur between two bodies. A higher value indicates
more friction. Should be set in the range 0 to 1. Defaults to 0.5.
Gets the friction value used when contacts occur between two bodies. | - |
group? | numberSets the collision group this body belongs to. Combine the group and the mask to prevent bodies
colliding with each other. Defaults to 1.
Gets the collision group this body belongs to. | - |
linearDamping? | numberSets the rate at which a body loses linear velocity over time. Defaults to 0.
Gets the rate at which a body loses linear velocity over time. | - |
linearFactor? | [number, number, number]Sets the scaling factor for linear movement of the body in each axis. Only valid for rigid
bodies of type BODYTYPE_DYNAMIC. Defaults to 1 in all axes (body can freely move).
Gets the scaling factor for linear movement of the body in each axis. | - |
linearVelocity? | [number, number, number]Sets the speed of the body in a given direction.
Gets the speed of the body in a given direction. | - |
mask? | numberSets the collision mask sets which groups this body collides with. It is a bit field of 16
bits, the first 8 bits are reserved for engine use. Defaults to 65535.
Gets the collision mask sets which groups this body collides with. | - |
mass? | numberSets the mass of the body. This is only relevant for BODYTYPE_DYNAMIC bodies, other
types have infinite mass. Defaults to 1.
Gets the mass of the body. | - |
restitution? | numberSets the value that controls the amount of energy lost when two rigid bodies collide. The
calculation multiplies the restitution values for both colliding bodies. A multiplied value
of 0 means that all energy is lost in the collision while a value of 1 means that no energy
is lost. Should be set in the range 0 to 1. Defaults to 0.
Gets the value that controls the amount of energy lost when two rigid bodies collide. | - |
rollingFriction? | numberSets the torsional friction orthogonal to the contact point. Defaults to 0.
Gets the torsional friction orthogonal to the contact point. | - |
system? | ComponentSystemThe ComponentSystem used to create this Component. | - |
entity? | EntityThe Entity that this Component is attached to. | - |
enabled? | booleanSets the enabled state of the component.
Gets the enabled state of the component. | - |