w0rm/elm-physics/3.2.1

NOTE: the latest version is 6.2.0

Physics.World

type alias World data =
Protected data

Physical world is our abstract playground for physical simulations.

empty : World data

Let be the new world!

add : Body data -> World data -> World data

Adds a body to the world.

worldWithFloor =
    add planeBody world

Check the Physics.Body module for possible bodies.

{ x : Float
, y : Float
, z : Float
}
-> World data
-> World data

Set the standard gravity, e.g.:

planetEarth =
    setGravity { x = 0, y = 0, z = -9.80665 } world
simulate : Float -> World data -> World data

Simulate the world, given the number of milliseconds since the last frame.

Call this function on a message from the onAnimationFrame subscription.

getBodies : World data -> List (Body data)

Get all bodies from the world.

Use this to convert bodies into visual representation, e.g. WebGL entities.

{ from :
{ x : Float
, y : Float
, z : Float
}
, direction :
{ x : Float
, y : Float
, z : Float
}
}
-> World data
-> Maybe (RaycastResult data)

Find the closest intersection of a ray against all the bodies in the world.

type alias RaycastResult data =
{ body : Body data
, point :
{ x : Float
, y : Float
, z : Float
}
, normal :
{ x : Float
, y : Float
, z : Float
}
}

The Raycast result includes the intersected body, intersection point and normal vector on the face, expressed within the local body coordinate system.

keepIf : (Body data -> Bool) -> World data -> World data

Keep bodies that satisfy the test.

update : (Body data -> Body data) -> World data -> World data

Apply a function to every body in the world.

constrain : (Body data -> Body data -> List Constraint) -> World data -> World data

Configure constraints between pairs of bodies. Constraints allow to limit the freedom of movement of two bodies with relation to each other.

Check the Physics.Constraint module for possible constraints.

worldWithACar : World { part : String }
worldWithACar =
    constrain
        (\b1 b2 ->
            case ( (Body.getData b1).part, (Body.getData b2).part ) of
                ( "wheel1", "base" ) ->
                    [ hingeConstraint1 ]


                ( "wheel2", "base" ) ->
                    [ hingeConstraint2 ]


                ( "wheel3", "base" ) ->
                    [ hingeConstraint3 ]


                ( "wheel4", "base" ) ->
                    [ hingeConstraint4 ]


                _ ->
                    []
        )
        worldWithCarParts

Note that this example only works for a single car, otherwise it would connect wheels of one car with the base of another another. You might want to use constrainIf to apply constraints on a subset of bodies.

constrain =
    constrainIf (always True)
(Body data -> Bool)
-> (Body data -> Body data -> List Constraint)
-> World data
-> World data

Configure constraints for a subset of bodies that satisfy the test.

For the above example we can tag each part of a car with the carId, and preselect parts of a single car with:

constrainCar carId =
    constrainIf (\body -> (Body.getData body).carId == carId)