PhysicsBodyDefinition
A PhysicsBody definition used to specify important initial properties.
Read time 5 minutesLast updated 10 days ago
Definition
- Type: Struct
- Namespace: Unity.U2D.Physics
- Assembly: UnityEngine.PhysicsCore2DModule
- Implements: IEquatable<PhysicsBodyDefinition>
public struct PhysicsBodyDefinition : IEquatable<PhysicsBodyDefinition>
Remarks
To configure the properties of a physics world and its objects, such as gravity, friction, position, and shape, use definitions. Definitions are objects that store physics properties and values. You create a definition, then pass it into the world, body, or shape you create. Definitions optimize physics performance by letting you avoid setting properties after you create a world, body, or shape, which can make the CPU do extra work. Definitions also allow you to pass around and reuse sets of values. If a script that holds a definition is in a class attached to a GameObject and the definition is a field, Unity displays its properties in the Inspector window so you can configure them there instead of in code. Because definitions are large types, pass them into other methods using the or keyword to avoid the extra memory cost of copying the structure.
MonoBehaviourpublicstructrefinAdditional Resources: PhysicsWorldDefinition, PhysicsShapeDefinition
Examples
// Declare a body definition, configure it, then create a body from it.using UnityEngine;using Unity.U2D.Physics;public class CreateObjectsWithDefinitions : MonoBehaviour{ // Declare a definition with default properties for the body. public PhysicsBodyDefinition bodyDefinition = new PhysicsBodyDefinition(); void Start() { // Get the default world. PhysicsWorld world = PhysicsWorld.defaultWorld; // Set the position of the body using the body definition. bodyDefinition.position = new Vector2(0f, 5f); // Create the physics body with the body definition. PhysicsBody myObject = world.CreateBody(bodyDefinition); }}
Constructors
Constructor | Description |
|---|---|
| PhysicsBodyDefinition() | Create a default PhysicsBody definition. |
| PhysicsBodyDefinition(System.Boolean) | Create a default PhysicsBody definition. |
Properties
Property | Description |
|---|---|
| angularDamping | Angular damping is used to reduce the angular velocity over time i.e. slow down rotating bodies. The damping parameter can be larger than 1.0f but the damping effect becomes sensitive to the time step when the damping parameter is large. |
| angularVelocity | The initial angular velocity of the body, in degrees per second. |
| awake | Is this body initially awake or sleeping? |
| collisionThreshold | A threshold used to control when continuous collision detection is used when a body moves. The value is used to compare the body linear velocity movement against the extents of all the shapes added to the body scaled by this threshold. If the movement exceeds the extents scaled by the threshold then continuous collision detection is used to stop tunneling. Lower values reduce the distance the body must move before continuous collision detection is used and can have a considerable impact on performance! Higher values increase the distance the body must move before continuous collision detection is used. Too low a threshold will result in continuous collision detection being used more often therefore affecting performance so this should be limited to specific bodies only. The default threshold is 0.5 which equates to half the total shape extents. The threshold is clamped to a range of 0.0 to 1.0 with 0.0 meaning continuous collision detection will always be used. |
| constraints | The degrees of freedom constraints (locks) for the body of Linear X, Linear Y and Rotation Z. |
| contactRecyclingAllowed | Controls contact recycling for this body. Enabled by default. Contact recycling reuses contact manifolds when bodies move only slightly, improving performance. Disabling it can avoid ghost collisions, at the cost of higher simulation time. Both bodies in a contact must have recycling enabled for that contact to be recycled. |
| enabled | Used to disable a body. A disabled body does not move or collide. |
| fastCollisionsAllowed | Treat this body as high speed object that performs continuous collision detection against dynamic and kinematic bodies, but not other high speed bodies. Fast collision bodies should be used sparingly, not because they are slow but because everything using fast collisions does not work well. They are not a solution for general dynamic-versus-dynamic continuous collision. They also may interfere with joint constraints. |
| fastRotationAllowed | This allows this body to bypass rotational speed limits. This should only be used for circular objects, such as wheels, balls etc. |
| gravityScale | Scale the gravity applied to this body, non-dimensional. |
| linearDamping | Linear damping is use to reduce the linear velocity i.e. slow down translating bodies. The damping parameter can be larger than 1 but the damping effect becomes sensitive to the time step when the damping parameter is large. Generally linear damping is undesirable because it makes objects move slowly as if they are floating. |
| linearVelocity | The initial linear velocity of the body's origin, in meters/sec. |
| massConfiguration | The authored mass configuration applied for the overridden values. Anything without a matching override bit is ignored. |
| massOverride | Selects which of the mass, rotational inertia and center of mass are overridden with the authored values instead of being computed from the attached shapes. Anything without a matching bit is computed from the attached shapes as normal. Only applies to a Dynamic body. |
| position | The initial position of the body, in world-space. Bodies should be created with the desired position as creating bodies at the origin and then moving them nearly doubles the cost of body creation, especially if the body is moved after shapes have been added. |
| rotation | The initial rotation of the body, in world-space. Bodies should be created with the desired rotation as creating bodies at the origin and then rotating them nearly doubles the cost of body creation, especially if the body is moved after shapes have been added. |
| sleepingAllowed | Set this flag to false if this body should never fall asleep. |
| sleepThreshold | A speed threshold below which the body is allowed to sleep, in meters/sec. |
| transformWriteMode | The method used to Write the body pose to the Transform. |
| type | A body is one of these three body types, Dynamic, Kinematic or Static, each of which determines how the body behaves in the simulation. |
| worldDrawing | Controls whether this body is automatically drawn when the world is drawn. |
Static Properties
Property | Description |
|---|---|
| defaultDefinition | Get a default PhysicsBody definition. |