Api Sandbox PhysicsBody
class

PhysicsBody

public sealed class PhysicsBody

Represents a physics object. An entity can have multiple physics objects. See PhysicsGroup. A physics objects consists of one or more PhysicsShapes.

Constructors

PhysicsBody(PhysicsWorld world)

Properties

Name Type Description
GameObject GameObject The GameObject that created this body
Component Component The component that created this body
Position Vector3 Position of this body in world coordinates.
World PhysicsWorld The physics world this body belongs to.
Rotation Rotation Rotation of the physics body in world space.
Scale Single
Velocity Vector3 Linear velocity of this body in world space.
AngularVelocity Vector3 Angular velocity of this body in world space.
MassCenter Vector3 Center of mass for this physics body in world space coordinates.
LocalMassCenter Vector3 Center of mass for this physics body relative to its origin.
OverrideMassCenter Boolean Is this physics body mass calculated or set directly.
Mass Single Mass of this physics body.
GravityEnabled Boolean Whether gravity is enabled for this body or not.
EnableCollisionSounds Boolean Whether to play collision sounds
GravityScale Single Scale the gravity relative to Gravity. 2 is double the gravity, etc.
UseController Boolean If true we'll create a controller for this physics body. This is useful for keyframed physics objects that need to push things. The controller will sweep as the entity moves, rather than teleporting the object.. which works better when pushing dynamic objects etc.
EnableTouch Boolean Enables Touch callbacks on all PhysicsShapes of this body. Returns true if ANY of the physics shapes have touch events enabled.
EnableTouchPersists Boolean Sets EnableTouchPersists on all shapes of this body. Returns true if ANY of the physics shapes have persistent touch events enabled.
EnableSolidCollisions Boolean Sets EnableSolidCollisions on all shapes of this body. Returns true if ANY of the physics shapes have solid collisions enabled.
BodyType PhysicsBodyType Movement type of physics body, either Static, Keyframed, Dynamic Note: If this body is networked and dynamic, it will return Keyframed on the client
AutoSleep Boolean Whether this body is allowed to automatically go into "sleep" after a certain amount of time of inactivity. Sleeping for more info on the sleep mechanic.
Transform Transform Transform of this physics body.
ShapeCount Int32 How many shapes belong to this body.
Shapes IEnumerable<PhysicsShape> All shapes that belong to this body.
Enabled Boolean Whether this body is enabled or not. Disables collisions, physics simulation, touch events, trace queries, etc.
MotionEnabled Boolean Controls physics simulation on this body.
Sleeping Boolean Physics bodies automatically go to sleep after a certain amount of time of inactivity to save on performance. You can use this to wake the body up, or prematurely send it to sleep.
SpeculativeContactEnabled Boolean If enabled, this physics body will move slightly ahead each frame based on its velocities.
Parent PhysicsBody The physics body we are attached to, if any
SelfOrParent PhysicsBody A convenience property, returns Parent, or if there is no parent, returns itself.
PhysicsGroup PhysicsGroup The physics group we belong to.
LinearDamping Single Generic linear damping, i.e. how much the physics body will slow down on its own.
AngularDamping Single Generic angular damping, i.e. how much the physics body will slow down on its own.
LinearDrag Single
AngularDrag Single
DragEnabled Boolean
Inertia Vector3 The diagonal elements of the local inertia tensor matrix.
InertiaRotation Rotation The orientation of the principal axes of local inertia tensor matrix.
Density Single Returns average of densities for all physics shapes of this body. This is based on SurfaceMaterial of each shape.
LastWaterEffect RealTimeSince Time since last water splash effect. Used internally.
SurfaceMaterial String Sets SurfaceMaterial on all child PhysicsShapes.
Surface Surface
GroupName String What is this body called in the group?
GroupIndex Int32 Return the index of this body in its PhysicsGroup
OnIntersectionStart Action<PhysicsIntersection>
OnIntersectionUpdate Action<PhysicsIntersection>
OnIntersectionEnd Action<PhysicsIntersectionEnd>
EnhancedCcd Boolean Enable enhanced continuous collision detection (CCD) for this body. When enabled, the body performs CCD against dynamic bodies (but not against other bodies with enhanced CCD enabled). This is useful for fast-moving objects like bullets or rockets that need reliable collision detection.
Locking PhysicsLock

Methods

Void SetComponentSource(Component c)
GameObject GetGameObject()
Void Move(Transform tx, Single delta)

Move to a new position. Unlike Transform, if you have `UseController` enabled, this will sweep the shadow to the new position, rather than teleporting there.

PhysicsShape AddSphereShape(Vector3 center, Single radius, Boolean rebuildMass = True)

Add a sphere shape to this body.

center — Center of the sphere, relative to Position of this body.
radius — Radius of the sphere.
rebuildMass — Whether the mass should be recalculated after adding the shape.
returns — The newly created shape, if any.
PhysicsShape AddSphereShape(Sphere sphere, Boolean rebuildMass = True)

Add a sphere shape to this body.

PhysicsShape AddCapsuleShape(Vector3 center, Vector3 center2, Single radius, Boolean rebuildMass = True)

Add a capsule shape to this body.

center — Point A of the capsule, relative to Position of this body.
center2 — Point B of the capsule, relative to Position of this body.
radius — Radius of the capsule end caps.
rebuildMass — Whether the mass should be recalculated after adding the shape.
returns — The newly created shape, or null on failure.
PhysicsShape AddBoxShape(Vector3 position, Rotation rotation, Vector3 extent, Boolean rebuildMass = True)

Add a box shape to this body.

position — Center of the box, relative to Position of this body.
rotation — Rotation of the box, relative to Rotation of this body.
extent — The extents of the box. The box will extend from its center by this much in both negative and positive directions of each axis.
rebuildMass — Whether the mass should be recalculated after adding the shape.
returns — The newly created shape, or null on failure.
PhysicsShape AddBoxShape(BBox box, Rotation rotation, Boolean rebuildMass = True)

Add a box shape to this body.

PhysicsShape AddHullShape(Vector3 position, Rotation rotation, List<Vector3> points, Boolean rebuildMass = True)
PhysicsShape AddHullShape(Vector3 position, Rotation rotation, Span<Vector3> points, Boolean rebuildMass = True)
PhysicsShape AddCylinderShape(Vector3 position, Rotation rotation, Single height, Single radius, Int32 slices = 16)

Add a cylinder shape to this body.

PhysicsShape AddConeShape(Vector3 position, Rotation rotation, Single height, Single radius1, Single radius2 = 0, Int32 slices = 16)

Add a cone shape to this body.

PhysicsShape AddConeShape(Vector3 a, Vector3 b, Single radiusA, Single radiusB, Int32 slices = 16)

Add a cone shape to this body.

PhysicsShape AddMeshShape(List<Vector3> vertices, List<Int32> indices)
PhysicsShape AddMeshShape(Span<Vector3> vertices, Span<Int32> indices)
PhysicsShape AddHeightFieldShape(UInt16[] heights, Byte[] materials, Int32 sizeX, Int32 sizeY, Single sizeScale, Single heightScale)
PhysicsShape AddCloneShape(PhysicsShape shape)
Void ClearShapes()

Remove all physics shapes, but not the physics body itself.

Void RebuildMass()

Meant to be only used on dynamic bodies, rebuilds mass from all shapes of this body based on their volume and physics properties, for cases where they may have changed.

Void Remove()

Completely removes this physics body.

Void ApplyImpulse(Vector3 impulse)

Applies instant linear impulse (i.e. a bullet impact) to this body at its center of mass. For continuous force (i.e. a moving car), use ApplyForce(Vector3)

Void ApplyImpulseAt(Vector3 position, Vector3 velocity)

Applies instant linear impulse (i.e. a bullet impact) to this body at given position. For continuous force (i.e. a moving car), use ApplyForceAt(Vector3,Vector3)

Void ApplyAngularImpulse(Vector3 impulse)

Applies instant angular impulse (i.e. a bullet impact) to this body. For continuous force (i.e. a moving car), use ApplyTorque(Vector3)

Void ApplyForce(Vector3 force)

Applies force to this body at the center of mass. This force will only be applied on the next physics frame and is scaled with physics timestep.

Void ApplyForceAt(Vector3 position, Vector3 force)

Applies force to this body at given position. This force will only be applied on the next physics frame and is scaled with physics timestep.

Void ApplyTorque(Vector3 force)

Applies angular velocity to this body. This force will only be applied on the next physics frame and is scaled with physics timestep.

force
Void ClearForces()

Clear accumulated linear forces (ApplyForce(Vector3) and ApplyForceAt(Vector3,Vector3)) during this physics frame that were not yet applied to the physics body.

Void ClearTorque()

Clear accumulated torque (angular force, ApplyTorque(Vector3)) during this physics frame that were not yet applied to the physics body.

Vector3 GetVelocityAtPoint(Vector3 point)

Returns the world space velocity of a point of the object. This is useful for objects rotating around their own axis/origin.

point — The point to test, in world coordinates.
returns — Velocity at the given point.
Vector3 FindClosestPoint(Vector3 vec)

Returns the closest point to the given one between all shapes of this body.

vec — Input position.
returns — The closest possible position on the surface of the physics body to the given position.
Void SetInertiaTensor(Vector3 inertia, Rotation rotation)

Sets the inertia tensor using the given moments and rotation.

inertia — Principal moments (Ixx, Iyy, Izz).
rotation — Rotation of the principal axes.
Void ResetInertiaTensor()

Resets the inertia tensor to its calculated values.

BBox GetBounds()

Returns Axis-Aligned Bounding Box (AABB) of this physics body.

PhysicsPoint LocalPoint(Vector3 p)

Convenience function that returns a PhysicsPoint from a position relative to this body.

PhysicsPoint WorldPoint(Vector3 p)

Convenience function that returns a PhysicsPoint for this body from a world space position.

PhysicsPoint MassCenterPoint()

Returns a PhysicsPoint at the center of mass of this body.

Boolean CheckOverlap(PhysicsBody body)

Checks if another body overlaps us, ignoring all collision rules

Boolean CheckOverlap(PhysicsBody body, Transform transform)

Checks if another body overlaps us at a given transform, ignoring all collision rules

PhysicsShape AddShape(HullPart part, Transform transform, Boolean rebuildMass = True)

Add a shape from a physics hull

PhysicsShape AddShape(MeshPart part, Transform transform, Boolean convertToHull, Boolean rebuildMass = True)

Add a shape from a mesh hull

Transform GetLerpedTransform(Double time)

When the physics world is run at a fixed timestep, getting the positions of bodies will not be smooth. You can use this function to get the lerped position between steps, to make things super awesome.

Void SmoothMove(Vector3 position, Single timeToArrive, Single timeDelta)

Move body to this position in a way that cooperates with the physics system. This is quite good for things like grabbing and moving objects.

Void SmoothMove(Transform transform, Single smoothTime, Single timeDelta)

Move body to this position in a way that cooperates with the physics system. This is quite good for things like grabbing and moving objects.

Void SmoothRotate(Rotation rotation, Single smoothTime, Single timeDelta)

Rotate the body to this position in a way that cooperates with the physics system.

Assembly: Sandbox.Engine Namespace: Sandbox Full Name: Sandbox.PhysicsBody