w0rm/elm-physics/1.0.2

NOTE: this package is not compatible with Elm 0.19.1

Physics

Highly experimental toy physics engine in Elm.

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

World

type World
world : 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

world
    |> setGravity (vec3 0 0 -10)
addBody : Body -> World -> World

You can also add bodies to the world

Body

type Body

Body is a solid matter without any moving parts

body : Body

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

setMass : Float -> Body -> Body

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

addShape : Shape -> Body -> Body

Adds shapes to the body. For example, call this to add a box:

body
    |> addShape (box (vec3 1 1 1))
rotateBy : Vec3 -> Float -> Body -> Body

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

offsetBy : Vec3 -> Body -> Body

Move the body from its current position

Shape

type Shape

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

We support two types of shapes, a box and a plane.

box : Vec3 -> Shape

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

box (vec3 1 1 1)
plane : Shape

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

Physics

step : Time -> World -> World

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

Call this function on a message from the AnimationFrame 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 =
    foldl
    ({ 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 a body the shape belongs to, each new body in the world gets and id starting from 0

  • shapeId is an id of the shape, each new shape in a body gets and id starting from 0

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.

contacts : World -> List Vec3

Get the contact points in the world for visual debugging