w0rm/elm-physics/2.0.1

NOTE: the latest version is 6.2.0

Physics

Highly experimental toy physics engine in Elm.

The API is currently shaping up and will be most likely changed.

World

type World

Let be the new world!

setGravity : Vec3 -> World -> World

Call it to set the world’s gravity, cause by default it’s an open space:

setGravity (vec3 0 0 -10) world
--> worldWithGravity
addBody : Body -> World -> ( World, BodyId )

You can also add bodies to the world. The function returns updated world and an id of the body.

addBody body world
--> (newWorld, bodyId)
type alias BodyId =
BodyId

A unique identifier of a body in the world. It can be used to link additional information, like meshes.

Body

type Body

Body is a solid matter without any moving parts.

This gives you an empty body, add shapes to turn it into something meaningful.

setMass : Float -> Body -> Body

After creating a body, you have to give it a mass. Bodies without mass don’t move!

setMass 5 boxBody
--> heavyBoxBody
addShape : Shape -> Body -> ( Body, ShapeId )

Adds a shape at the center of the body and returns a tuple of updated body and a shape id. For example, call this to add a box:

addShape (box (vec3 1 1 1)) body
--> (newBody, shapeId)
type alias ShapeId =
ShapeId

Same as BodyId, but for shapes within a body.

rotateBy : Vec3 -> Float -> Body -> Body

Rotate the body around the axis by a specific angle from its current orientation.

rotateBy (vec3 1 0 0) pi body
--> upsideDownBody
offsetBy : Vec3 -> Body -> Body

Move the body from its current position.

offsetBy (vec3 0 0 12) body
--> elevatedBody

Shape

type Shape

Shape is an integral part of a body. It is positioned in the body’s coordinate system.

We support the following shapes:

box : Vec3 -> Shape

A box shape is defined by its half extends. To create a 2x2 box, call this:

box (vec3 1 1 1)
sphere : Float -> Shape

A sphere shape is defined by its radius.

A plane shape, with a normal pointing in the direction of the z axis.

Physics

step : Float -> World -> World

Simulate the world, given the time delta since the last step.

Call this function on a message from the onAnimationFrame subscription!

( { transform : Mat4
, bodyId : BodyId
, shapeId : ShapeId
}
-> a
-> a
)
-> a
-> World
-> a

Fold over each shape in the world. Use this to convert shapes into visual representation, e.g. WebGL entities.

entities =
    fold
    ({ transform, bodyId, shapeId } currentEntities ->
        makeEntity transform bodyId shapeId :: currentEntities
    )
    []
    world
  • transform is a transformation matrix that offsets and rotates a shape into the world coorditantes

  • bodyId is an id of the body the shape belongs to

  • shapeId is an id of the shape within the body

Use bodyId and shapeId to link additional information from the outside of the engine, e.g. which mesh to render or what color should this shape be.

Debug

The following functions are exposed for debugging. Check the demos for usage details.

foldContacts : (Vec3 -> a -> a) -> a -> World -> a

Fold over the contact points in the world.

foldFaceNormals : (Mat4 -> Vec3 -> Vec3 -> a -> a) -> a -> World -> a

Fold over all the face normals in the world, where each face normal consists of a normal vector for a face and a reference point within the face. These are both expressed within the local shape coordinate system. The transform to the current world coordinates from the shape coordinates is also provided.

foldUniqueEdges : (Mat4 -> Vec3 -> Vec3 -> a -> a) -> a -> World -> a

Fold over all the unique edges in the world, where each unique edge consists of a unit direction vector that runs parallel to an edge of a face in the shape. A vertex point of the shape and a transform to the current world coordinates from the shape coordinates is also provided for context. Both the vector and the point are expressed within the local shape coordinate system.