Physics Component
Cocos Creator currently provides users with a variety of collider components, common rigid body components, as well as tool-type constant force components.
Collider Component
The collider component is used to represent the shape of the collision body of a rigid body. Different geometric shapes have different properties.
Note: the following property names are all lowercase in the actual code. Note: currently only box, sphere and capsule are supported in
builtin
.
BoxCollider
Properties | Description |
---|---|
material | The physics material referenced by the collider (the default physics material was referenced when it is empty) |
isTrigger | Whether it is a trigger, the trigger will not generate physical feedback |
center | The origin of the shape in the local coordinate system |
size | The size of the box, ie length, width, height |
To use BoxCollider, please refer to the BoxCollider API.
SphereCollider
Properties | Description (Other reference box colliders) |
---|---|
radius | Radius of the sphere |
To use SphereCollider, please refer to the SphereCollider API.
CylinderCollider
Properties | Description (Other reference box colliders) |
---|---|
direction | The reference axis of the cylinder’s extended direction |
height | The total height of the cylinder |
radius | The radius of the circular surfaces at both ends of the cylinder |
To use CylinderCollider, please refer to the CylinderCollider API.
CapsuleCollider
Properties | Description (Other reference cylinders and box colliders) |
---|---|
cylinderHeight | The height of the cylinder in the capsule |
radius | The radius of the sphere in the capsule |
To use CapsuleCollider, please refer to the CapsuleCollider API.
Note: cannon.js does not support capsule components. It is recommended to use two spheres and cylinders and piece them together.
ConeCollider
Properties | Description (Other reference cylinders and box colliders) |
---|---|
radius | The radius of the circle of the underside of the cone |
height | The height of the cone |
To use ConeCollider, please refer to the ConeCollider API.
PlaneCollider
Properties | Description (Other reference box colliders) |
---|---|
normal | The normal vector to the plane |
constant | The distance that the plane moves along the normal vector |
To use PlaneCollider, please refer to the PlaneCollider API.
MeshCollider
Properties | Description (Other reference box colliders) |
---|---|
mesh | The mesh asset referenced by the mesh collider, used to initialize the mesh collision volume |
convex | Whether used the convex hull of the mesh is approximated, the number of vertices of the mesh should be less than 255 (through which any convex collider and dynamic rigid body can be supported). |
To use MeshCollider, please refer to the MeshCollider API.
Note: cannon.js has poor support for the mesh collider component, and only allows detection with the sphere\plane collider. Note: currently only ammo.js backend supports convex functionality.
SimplexCollider
Properties | Description (Other reference box colliders) |
---|---|
shapeType | There are four types of simplex: point, line, triangular face and tetrahedron |
vertex0 | Vertex 0 of the simplex, point (consisting of 0) |
vertex1 | Vertex 1 of the simplex, line (consisting of 0,1) |
vertex2 | Vertex 2 of the simplex, triangular face (consisting of 0,1,2) |
vertex3 | Vertex 3 of the simplex, tetrahedron (consisting of 0,1,2,3) |
Note:
cannon.js
has imperfect support for line and triangular face.
To use SimplexCollider, please refer to the SimplexCollider API.
RigidBody
In order to more easily simulate physical behavior, Cocos Creator provides users with rigid body components.
Properties | Description (The property values in the above picture are all default values) |
---|---|
group | The group of the physics element |
mass | The mass of the object |
type | The type of the rigid body |
linearDamping | Linear damping, used to reduce the linear velocity of an object |
angularDamping | Angular damping, used to reduce the rotation rate of objects |
useGravity | Whether the rigid body is affected by gravity |
linerFactor | Linear factor, which can affect the change of linear velocity in each axis |
angularFactor | Rotation factor, which can affect the change of the rotation speed of each axis |
To use RigidBody, please refer to the RigidBody API.
ConstantForce
This is a tool component that depends on a RigidBody and will apply a given force and torque to a rigid body every frame.
Properties | Description |
---|---|
force | The force applied to the rigid body in the world coordinate system |
localForce | The force applied to the rigid body in the local coordinate system |
torque | Torque applied to the rigid body in the world coordinate system |
localTorque | Torque applied to the rigid body in the local coordinate system |
To use ConstantForce, please refer to the ConstantForce API.