Let be the new world!
Set the standard gravity and its direction, e.g.:
planetEarth =
withGravity
(Acceleration.metersPerSecondSquared 9.80665)
Direction3d.negativeZ
worldAdd a body to the world.
worldWithFloor =
add planeBody worldCheck the Physics.Body module for possible bodies.
Simulate the world, given the number of seconds since the last frame.
To animate, call this on a message from onAnimationFrameDelta:
simulatedWorld =
World.simulate (Duration.seconds (1 / 60)) worldMake sure to pass a short duration, such that bodies do not travel through each other during the single frame. This depends on minimum body size and maximum velocity.
Get all bodies from the world.
Use this to convert bodies into visual representation, e.g. WebGL entities.
Get all contacts from the last simulation frame.
Find the closest intersection of a ray against all the bodies in the world. Except for particles, because they have no size.
The Raycast result includes the intersected body, intersection point and normal vector on the face, expressed within the local body coordinate system.
Use the Frame3d from Body.frame
to transform the result into world coordinates.
Keep bodies that satisfy the test.
Apply a function to every body in the world.
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.data b1).part
, (Body.data b2).part
)
of
( "wheel1", "base" ) ->
[ hingeConstraint1 ]
( "wheel2", "base" ) ->
[ hingeConstraint2 ]
( "wheel3", "base" ) ->
[ hingeConstraint3 ]
( "wheel4", "base" ) ->
[ hingeConstraint4 ]
_ ->
[]
)
worldWithCarPartsNote that this example only works for a single car, otherwise it would
connect wheels of one car with the base of another. You might want
to use constrainIf to apply constraints on a subset of bodies.
constrain =
constrainIf (always True)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.data body).carId == carId
)
Physical world is our abstract playground for physical simulations.