What Are the Three Main Types of Bodies Available in planck.js?
Planck.js, a popular 2D JavaScript physics engine ported from Box2D, provides three main body types to define how objects move and interact within a simulated physics world: static, kinematic, and dynamic. Choosing the correct body type for each object in your environment is essential for optimizing performance and achieving realistic simulation behavior.
Static Bodies
A static body does not move under the influence of simulation forces and behaves as if it has infinite mass. In planck.js, static bodies are treated with zero mass, zero inverse mass, and zero velocity.
- Behavior: They are completely stationary and do not react to gravity, collisions, impulses, or forces.
- Movement: They can only be moved manually by the user via direct position adjustments in code.
- Collisions: Static bodies do not collide with other static bodies or kinematic bodies; they only collide with dynamic bodies.
- Common Uses: Ground, floors, structural walls, and immovable obstacles.
Kinematic Bodies
A kinematic body is a hybrid type that moves according to its assigned velocity but remains unaffected by external forces or collisions. Like static bodies, they behave as though they have infinite mass.
- Behavior: They do not respond to gravity, forces, or impulses, meaning their velocity remains unchanged when colliding with other objects.
- Movement: They are moved by explicitly setting their linear or angular velocity, or by shifting their position manually.
- Collisions: Kinematic bodies do not collide with static or other kinematic bodies. When they collide with dynamic bodies, they push them out of the way without slowing down or bouncing back.
- Common Uses: Moving platforms, elevators, rotating obstacles, and conveyor belts.
Dynamic Bodies
A dynamic body is a fully simulated physical object that responds dynamically to the environment. It always possesses a finite, non-zero mass. If you attempt to assign a mass of zero to a dynamic body, planck.js automatically defaults its mass to one kilogram and prevents it from rotating.
- Behavior: They are fully subject to gravity, friction, restitution, and any forces or impulses applied during the simulation.
- Movement: Their position and rotation change automatically based on the physics solver calculations.
- Collisions: Dynamic bodies collide with all other body types, including static, kinematic, and other dynamic bodies.
- Common Uses: Main characters, projectiles, falling boxes, and any interactive props.