sm.physics

Contains functions regarding the physics engine.

Constants:

Functions:

  • applyImpulse
  • applyImpulse
  • applyImpulse
  • applyTorque
  • distanceRaycast
  • explode
  • getGravity
  • getGroundMaterial
  • getSphereContacts
  • multicast
  • raycast
  • raycastTarget
  • setGravity
  • sphereContactCount
  • spherecast

  • sm.physics.filter table

    Collision filter types

    dynamicBody
    staticBody
    character
    areaTrigger
    joints
    terrainSurface
    terrainAsset
    harvestable
    areaTrigger
    static
    default
    all

    sm.physics.types table

    Physics types are used to define an object's characteristics is in the physics world. Upon a raycast or collision detection, these types are used to find out what object was intersected.

    "invalid" No object.
    "terrainSurface" The ground.
    "terrainAsset" Trees and boulders.
    "lift" A Lift.
    "body" A Body.
    "character" A Character.
    "joint" A Joint.
    "harvestable" A Harvestable.
    "vision" A collision area used by sensors.

    sm.physics.applyImpulse(target, impulse, worldSpace=false, offset=nil)

    Applies an impulse to a Shape, changing its velocity immediately. The impulse is applied to the shape's centerpoint with an optional offset.

    Parameters:

    TypeNameDescription
    ShapetargetThe object on which the impulse is exerted on.
    Vec3impulseThe direction and strength of the impulse.
    booleanworldSpace=falseWhether the impulse is applied in world space coordinates. (Defaults to local rotation)
    Vec3offset=nilThe offset from the center point. (Defaults to no offset)

    sm.physics.applyImpulse(target, impulse, worldSpace=false, offset=nil)

    Applies an impulse to a Body, changing its velocity immediately. The impulse is applied to the body's center of mass with an optional offset.

    Parameters:

    TypeNameDescription
    BodytargetThe object on which the impulse is exerted on.
    Vec3impulseThe direction and strength of the impulse.
    booleanworldSpace=falseWhether the impulse is applied in world space coordinates. (Defaults to local rotation)
    Vec3offset=nilThe offset from the center point. (Defaults to no offset)

    sm.physics.applyImpulse(target, impulse)

    Applies an impulse to a Character, changing its velocity immediately. The impulse is applied to the character's centerpoint.

    Parameters:

    TypeNameDescription
    CharactertargetThe character on which the impulse is exerted on.
    Vec3impulseThe direction and strength of the impulse.

    sm.physics.applyTorque(target, torque, worldSpace=false)

    Applies a torque impulse to a Body, changing its angular velocity immediately. The torque is applied along the body's center of mass, making it rotate.

    Parameters:

    TypeNameDescription
    BodytargetThe object on which the torque is exerted on.
    Vec3torqueThe direction and strength of the torque.
    booleanworldSpace=falseWhether the torque is applied in world space coordinates. (Defaults to local rotation)

    sm.physics.distanceRaycast(start, direction)

    Performs a distance ray cast from a position with a given direction.

    Note:

    sm.physics.distanceRaycast is generally cheaper to use than sm.physics.raycast as it performs collision checks in a simplified world. If the raycast is only used for checking collision, it is advised to use this method instead.

    Parameters:

    TypeNameDescription
    Vec3startThe start position.
    Vec3directionThe ray's direction and length.

    Returns:

    TypeDescription
    boolean, number2 values: whether raycast was successful; the fraction (0–1) of the distance reached until collision divided by the ray's length.

    sm.physics.explode(position, level, destructionRadius, impulseRadius, magnitude, effectName=nil, ignoreShape=nil, parameters=nil)

    Server only

    Creates an explosion at given position. The explosion creates a shockwave that is capable of destroying blocks and pushing characters and creations away.

    Shapes that are within the explosion's destruction radius may receive the event server_onExplosionHit.

    Note:

    The destruction level is the damage effect on blocks and parts, determining how likely it is that they are destroyed. This is related to the `qualityLevel` found in parts json-files.

    Any quality level equal to or less than the destruction level may be destroyed. The effect fades one level every half travelled of the remaining destruction radius.

    A quality level of 0 means a block or part is indestructible.

    Parameters:

    TypeNameDescription
    Vec3positionThe center point of the explosion.
    integerlevelThe destruction level affecting nearby objects.
    numberdestructionRadiusThe destruction radius. Objects inside this sphere may be destroyed.
    numberimpulseRadiusThe impulse radius. Objects inside this sphere are affected by an impulse.
    numbermagnitudeThe impulse strength of the explosion. The strength diminishes with distance.
    stringeffectName=nilThe name of the effect to be played upon explosion. (Optional)
    ShapeignoreShape=nilThe shape to be ignored. (Optional)
    tableparameters=nilThe table containing the parameters for the effect. (Optional)

    sm.physics.getGravity()

    Server only

    Returns the gravitational acceleration affecting shapes and bodies.

    Returns:

    TypeDescription
    numberThe gravitational value.

    sm.physics.getGroundMaterial(worldPosition)

    Returns the material at the given position in the terrain.

    Parameters:

    TypeNameDescription
    Vec3worldPositionThe world position to check the material at.

    Returns:

    TypeDescription
    stringThe material name.

    sm.physics.getSphereContacts(pos, radius)

    Server only

    Returns a table of the game objects that are found inside the given sphere

    Parameters:

    TypeNameDescription
    Vec3posThe world position of the sphere.
    numberradiusThe radius of the sphere.

    Returns:

    TypeDescription
    tableThe table with tables of objects found inside the sphere. { bodies={Body, ..}, characters={Character, ..}, harvestables={Harvestable, ..}, lifts={Lift, ..} }

    sm.physics.multicast(casts)

    Performs multiple sphere and/or raycasts given a table of parameters.

    Type can be "sphere" or "ray". Radius is ignored for rays.

    Parameters:

    TypeNameDescription
    tablecastsTable of casts. { type=string, startPoint=Vec3, endPoint=Vec3, radius=number, mask=sm.physics.filter }

    Returns:

    TypeDescription
    tableArray of pairs of boolean and RaycastResult. {{boolean, RaycastResult}, ..}

    sm.physics.raycast(start, end, body=nil, mask=nil)

    Performs a ray cast between two positions.

    The returned RaycastResult contains information about any object intersected by the ray.

    If the ray cast is called from within a shape (e.g. a Sensor), a Body may be provided which the ray will not intersect.

    Parameters:

    TypeNameDescription
    Vec3startThe start position.
    Vec3endThe end position.
    Bodybody=nilThe body to be ignored. (Optional)
    integermask=nilThe collision mask. Defaults to sm.physics.filter.default (Optional)

    Returns:

    TypeDescription
    boolean, RaycastResultTrue if raycast was successful; The raycast result data.

    sm.physics.raycastTarget(start, end, body)

    Performs a ray cast between two positions to find a specific target.

    a Body must be provided as a target.

    The returned RaycastResult contains information about any object intersected by the ray.

    Parameters:

    TypeNameDescription
    Vec3startThe start position.
    Vec3endThe end position.
    BodybodyThe body to be exclusively checked.

    Returns:

    TypeDescription
    boolean, RaycastResultTrue if raycast was successful; The raycast result data.

    sm.physics.setGravity(gravity)

    Server only

    Sets the gravitational acceleration affecting shapes and bodies.

    Parameters:

    TypeNameDescription
    numbergravityThe gravitational value.

    sm.physics.sphereContactCount(worldPosition, radius, includeTerrain=false, countWater=false)

    Returns the number of collision objects that are found inside the given sphere

    Parameters:

    TypeNameDescription
    Vec3worldPositionThe world position of the sphere.
    numberradiusThe radius of the sphere.
    booleanincludeTerrain=falseTrue if terrain should be included in the test
    booleancountWater=falseTrue if water should be included

    Returns:

    TypeDescription
    integerThe number of objects.

    sm.physics.spherecast(start, end, radius, body=nil, mask=nil)

    Performs a spherical ray cast between two positions.

    The returned RaycastResult contains information about any object intersected by the ray.

    If the ray cast is called from within a shape (e.g. a Sensor), a Body may be provided which the ray will not intersect.

    Parameters:

    TypeNameDescription
    Vec3startThe start position.
    Vec3endThe end position.
    numberradiusThe radius of the sphere.
    Bodybody=nilThe body to be ignored. (Optional)
    integermask=nilThe collision mask. Defaults to sm.physics.filter.default (Optional)

    Returns:

    TypeDescription
    boolean, RaycastResultTrue if raycast was successful; The raycast result data.