w0rm/elm-physics/4.0.0

NOTE: the latest version is 6.2.0

Physics.Body

type alias Body data =
Protected data

Represents a physical body containing user defined data, like a WebGL mesh.

By default bodies don’t move. To change this, use setBehavior.

All bodies start out centered on the origin, use moveTo to set the position.

The supported bodies are:

For complex bodies check compound.

block : Block3d Meters BodyCoordinates -> data -> Body data

A block is created from elm-geometry Block3d. To create a 1x1x1 cube, centered at the origin of the body, call this:

cubeBody =
    block
        (Block3d.centeredOn
            Frame3d.origin
            ( meters 1, meters 1, meters 1 )
        )
        data
plane : data -> Body data

A plane with the normal that points in the direction of the z axis.

A plane is collidable in the direction of the normal. Planes don’t collide with other planes and are always static.

sphere : Sphere3d Meters BodyCoordinates -> data -> Body data

A sphere is created from elm-geometry Sphere3d.

To create a 1 meter radius sphere, that is centered at the origin of the body, call this:

sphereBody =
    sphere
        (Sphere3d.atOrigin (meters 1))
        data
particle : data -> Body data

A particle is an abstract point that doesn’t have dimensions. Particles don’t collide with each other.

Behavior

Bodies may have static or dynamic behavior.

dynamic : Mass -> Behavior

Dynamic bodies move and react to forces and collide with other dynamic and static bodies.

Static bodies don’t move and only collide with dynamic bodies.

setBehavior : Behavior -> Body data -> Body data

Change the behavior, e.g. to make a body dynamic:

dynamicBody =
    staticBody
        |> setBehavior (dynamic (Mass.kilograms 5))

Position and Orientation

Body data
-> Frame3d Meters WorldCoordinates { defines : BodyCoordinates }

Get the position and orientation of the body in the world as Frame3d.

This is useful to transform points and directions between world and body coordinates.

moveTo : Point3d Meters WorldCoordinates -> Body data -> Body data

Set the position of the body in the world, e.g. to raise a body 5 meters above the origin:

movedBody =
    body
        |> moveTo (Point3d.meters 0 0 5)
translateBy : Vector3d Meters WorldCoordinates -> Body data -> Body data

Move the body in the world relative to its current position, e.g. to translate a body down by 5 meters:

translatedBody =
    body
        |> translateBy (Vector3d.meters 0 0 -5)
rotateAround : Axis3d Meters WorldCoordinates -> Angle -> Body data -> Body data

Rotate the body in the world around axis, e.g. to rotate a body 45 degrees around Z axis:

movedBody =
    body
        |> rotateAround Axis3d.z (Angle.degrees 45)
originPoint : Body data -> Point3d Meters WorldCoordinates

Get the origin point of a body in the world

User-Defined Data

setData : data -> Body data -> Body data

Set user-defined data.

getData : Body data -> data

Get user-defined data.

Advanced

setMaterial : Material -> Body data -> Body data

Set the material to controll friction and bounciness.

compound : List Shape -> data -> Body data

Make a compound body from a list of shapes.

For example, the sphere from above can be defined like this:

sphere radius data =
    compound
        [ Shape.sphere
            (Sphere3d.atOrigin (meters 1))
        ]
        data

We only support rigid bodies.