Namespace: bullet3d.rigid_body
Language: Lua
Type: Defold Lua
File: script_bullet3d_rigid_body.cpp
Source: engine/gamesys/src/gamesys/scripts/bullet3d/script_bullet3d_rigid_body.cpp
Rigid body functions accept the collision object userdata returned by
bullet3d.get_rigid_body(). Passing a trigger ghost object raises an error.
Defold retains ownership of the collision shape, motion state, world
membership, and native user pointer. The shape’s logical children can be
mutated through bullet3d.shape; shared resource shapes become per-instance
copies on first mutation. Mass and local inertia can be changed for dynamic
bodies without changing their Defold collision-object type.
Linear quantities use Defold units. Angular velocity, damping, and factors
are unscaled. Torque and angular impulse use squared physics scale because
inertia scales with length squared. Floating-point and vector inputs must be
finite. Damping must be in [0, 1], and sleeping thresholds must be
non-negative.
Type: FUNCTION Apply a force at the center of mass
Parameters
body (btRigidBody) - rigid bodyforce (vector3) - force in Defold unitsType: FUNCTION Apply an impulse at the center of mass
Parameters
body (btRigidBody) - rigid bodyimpulse (vector3) - impulse in Defold unitsType: FUNCTION This has the same point semantics as b2d.body.apply_force: world_position is the point where the force is applied. The binding converts it to the center-of-mass-relative offset expected by Bullet’s applyForce method.
Parameters
body (btRigidBody) - rigid bodyforce (vector3) - force in Defold unitsworld_position (vector3) - application point in world space and Defold unitsExamples
Apply an upward force at the game object’s current world position:
function init(self)
local body = bullet3d.get_rigid_body("#collisionobject")
local force = vmath.vector3(0, 100, 0)
bullet3d.rigid_body.apply_force(body, force, go.get_world_position())
end
Type: FUNCTION This exposes Bullet’s btRigidBody::applyForce point convention directly. relative_position is an offset from the body’s center of mass expressed in world axes, not a world position or body-local coordinate.
Parameters
body (btRigidBody) - rigid bodyforce (vector3) - force in Defold unitsrelative_position (vector3) - center-of-mass-relative offset in world axes and Defold unitsType: FUNCTION This exposes Bullet’s btRigidBody::applyImpulse point convention directly. relative_position is an offset from the body’s center of mass expressed in world axes, not a world position or body-local coordinate.
Parameters
body (btRigidBody) - rigid bodyimpulse (vector3) - impulse in Defold unitsrelative_position (vector3) - center-of-mass-relative offset in world axes and Defold unitsType: FUNCTION This has the same point semantics as b2d.body.apply_linear_impulse. world_position is converted to the center-of-mass-relative offset expected by Bullet’s applyImpulse method.
Parameters
body (btRigidBody) - rigid bodyimpulse (vector3) - impulse in Defold unitsworld_position (vector3) - application point in world space and Defold unitsType: FUNCTION Apply torque
Parameters
body (btRigidBody) - rigid bodytorque (vector3) - torque in Defold squared unitsType: FUNCTION Apply a torque impulse
Parameters
body (btRigidBody) - rigid bodyimpulse (vector3) - angular impulse in Defold squared unitsType: FUNCTION Clear accumulated force and torque
Parameters
body (btRigidBody) - rigid bodyType: FUNCTION Calls Bullet’s native btRigidBody::getAabb, which immediately calculates the bounds from the body’s current collision shape and world transform. This does not read the broadphase proxy’s cached AABB.
Parameters
body (btRigidBody) - rigid bodyReturns
aabb (bullet3d.world.aabb) - world-space bounds in Defold unitsType: ENUM Combine these constants into the complete flag mask accepted by bullet3d.rigid_body.set_flags().
Members
bullet3d.rigid_body.BT_DISABLE_WORLD_GRAVITY - Disable automatic world gravity. Set this bit before assigning custom body gravity that must survive later world-gravity changes or re-adding the body to a world.bullet3d.rigid_body.BT_ENABLE_GYROSCOPIC_FORCE_EXPLICIT - Enable explicit gyroscopic force integration.bullet3d.rigid_body.BT_ENABLE_GYROSCOPIC_FORCE_IMPLICIT_WORLD - Enable implicit world-space gyroscopic force integration.bullet3d.rigid_body.BT_ENABLE_GYROSCOPIC_FORCE_IMPLICIT_BODY - Enable implicit body-space gyroscopic force integration. This flag is enabled by default for newly constructed rigid bodies.Type: FUNCTION Get angular damping
Parameters
body (btRigidBody) - rigid bodyReturns
damping (number) - angular dampingType: FUNCTION Get the angular factor
Parameters
body (btRigidBody) - rigid bodyReturns
factor (vector3) - per-axis angular factorType: FUNCTION Get the angular sleeping threshold
Parameters
body (btRigidBody) - rigid bodyReturns
threshold (number) - threshold in radians per secondType: FUNCTION Get angular velocity
Parameters
body (btRigidBody) - rigid bodyReturns
velocity (vector3) - angular velocity in radians per secondType: FUNCTION Get the center-of-mass world position
Parameters
body (btRigidBody) - rigid bodyReturns
position (vector3) - center-of-mass position in Defold unitsType: FUNCTION Get linear and angular damping
Parameters
body (btRigidBody) - rigid bodyReturns
linear (number) - linear dampingangular (number) - angular dampingType: FUNCTION Get rigid body flags
Parameters
body (btRigidBody) - rigid bodyReturns
flags (integer) - rigid body flagsType: FUNCTION Get body gravity
Parameters
body (btRigidBody) - rigid bodyReturns
gravity (vector3) - gravity in Defold units per second squaredType: FUNCTION Get inverse mass
Parameters
body (btRigidBody) - rigid bodyReturns
inverse_mass (number) - inverse massType: FUNCTION Get linear damping
Parameters
body (btRigidBody) - rigid bodyReturns
damping (number) - linear dampingType: FUNCTION Get the linear factor
Parameters
body (btRigidBody) - rigid bodyReturns
factor (vector3) - per-axis linear factorType: FUNCTION Get the linear sleeping threshold
Parameters
body (btRigidBody) - rigid bodyReturns
threshold (number) - threshold in Defold units per secondType: FUNCTION Get linear velocity
Parameters
body (btRigidBody) - rigid bodyReturns
velocity (vector3) - velocity in Defold units per secondType: FUNCTION This has the same point semantics as b2d.body.get_linear_velocity_from_local_point. The local origin is the body’s center of mass.
Parameters
body (btRigidBody) - rigid bodylocal_point (vector3) - point in body-local space and Defold unitsReturns
velocity (vector3) - point velocity in Defold units per secondType: FUNCTION This has the same point semantics as b2d.body.get_linear_velocity_from_world_point.
Parameters
body (btRigidBody) - rigid bodyworld_point (vector3) - point in world space and Defold unitsReturns
velocity (vector3) - point velocity in Defold units per secondType: FUNCTION Returns the diagonal local inertia in Defold mass-times-distance-squared units. A zero component denotes an axis with zero inverse inertia.
Parameters
body (btRigidBody) - rigid bodyReturns
inertia (vector3) - diagonal local inertiaType: FUNCTION Get mass
Parameters
body (btRigidBody) - rigid bodyReturns
mass (number) - mass, or zero for an infinite-mass bodyType: FUNCTION Get total accumulated force
Parameters
body (btRigidBody) - rigid bodyReturns
force (vector3) - accumulated force in Defold unitsType: FUNCTION Get total accumulated torque
Parameters
body (btRigidBody) - rigid bodyReturns
torque (vector3) - accumulated torque in Defold squared unitsType: FUNCTION The relative position is expressed in world axes. Despite Bullet’s function name, it is not a body-local coordinate.
Parameters
body (btRigidBody) - rigid bodyrelative_position (vector3) - center-of-mass-relative offset in world axes and Defold unitsReturns
velocity (vector3) - point velocity in Defold units per secondType: FUNCTION Get the body’s world
Parameters
body (btRigidBody) - rigid bodyReturns
world (btDiscreteDynamicsWorld) - owning worldType: FUNCTION Test a rigid body flag
Parameters
body (btRigidBody) - rigid bodyflag (integer) - flag or maskReturns
set (boolean) - true when all requested flag bits are setType: FUNCTION Test whether a handle refers to a valid rigid body
Parameters
body (btRigidBody) - rigid bodyReturns
valid (boolean) - rigid body validityType: FUNCTION Set angular damping
Parameters
body (btRigidBody) - rigid bodydamping (number) - finite angular damping in [0, 1]Type: FUNCTION Set the angular factor
Parameters
body (btRigidBody) - rigid bodyfactor (vector3) - per-axis angular factorType: FUNCTION Set angular velocity
Parameters
body (btRigidBody) - rigid bodyvelocity (vector3) - finite angular velocity in radians per secondType: FUNCTION Set linear and angular damping
Parameters
body (btRigidBody) - rigid bodylinear (number) - finite linear damping in [0, 1]angular (number) - finite angular damping in [0, 1]Type: FUNCTION This replaces the complete flag mask. Every enabled gyroscopic mode is evaluated independently, so clear existing gyroscopic mode bits before selecting a different mode.
Parameters
body (btRigidBody) - rigid bodyflags (integer) - rigid body flagsType: FUNCTION A later bullet3d.world.set_gravity() call, or removing and re-adding the body to a world, can overwrite custom body gravity unless the body’s BT_DISABLE_WORLD_GRAVITY flag is set.
Parameters
body (btRigidBody) - rigid bodygravity (vector3) - gravity in Defold units per second squaredExamples
Give one body persistent custom gravity without discarding its other flags:
function init(self)
local body = bullet3d.get_rigid_body("#collisionobject")
local flags = bullet3d.rigid_body.get_flags(body)
flags = bit.bor(flags, bullet3d.rigid_body.BT_DISABLE_WORLD_GRAVITY)
bullet3d.rigid_body.set_flags(body, flags)
bullet3d.rigid_body.set_gravity(body, vmath.vector3(0, 4, 0))
end
Type: FUNCTION Set linear damping
Parameters
body (btRigidBody) - rigid bodydamping (number) - finite linear damping in [0, 1]Type: FUNCTION Set the linear factor
Parameters
body (btRigidBody) - rigid bodyfactor (vector3) - per-axis linear factorType: FUNCTION Set linear velocity
Parameters
body (btRigidBody) - rigid bodyvelocity (vector3) - finite velocity in Defold units per secondType: FUNCTION Recalculates local inertia from the body’s current collision shape. Only a dynamic body can be changed; zero mass cannot be used to convert it into a static body. Values too small to have a finite native inverse are rejected. The body is activated after the update.
Parameters
body (btRigidBody) - dynamic rigid bodymass (number) - finite mass greater than zeroExamples
Change the mass of a dynamic collision object and inspect its recalculated inertia:
function init(self)
local body = bullet3d.get_rigid_body("#collisionobject")
bullet3d.rigid_body.set_mass(body, 5)
print("local inertia", bullet3d.rigid_body.get_local_inertia(body))
end
Type: FUNCTION Sets mass and diagonal local inertia together, updates the world-space inertia tensor, and activates the body. Only dynamic bodies are accepted. A zero inertia component is allowed and disables angular response on that local axis; negative, non-finite, or nonzero values too small to have a finite native inverse are rejected.
Parameters
body (btRigidBody) - dynamic rigid bodymass (number) - finite mass greater than zerolocal_inertia (vector3) - finite non-negative diagonal local inertia in Defold mass-times-distance-squared unitsType: FUNCTION Set the sleeping thresholds
Parameters
body (btRigidBody) - rigid bodylinear (number) - finite non-negative linear threshold in Defold units per secondangular (number) - finite non-negative angular threshold in radians per second