Collision detection library for 2D objects.
Representation of a container that stores multiple triads of a key, object, and objects' bounding box. The type of key must be comparable.
{ range : { minX : Basics.Float
, maxX : Basics.Float
, minY : Basics.Float
, maxY : Basics.Float }
, boundingBox : object -> { minX : Basics.Float
, maxX : Basics.Float
, minY : Basics.Float
, maxY : Basics.Float }
, isCollided : object -> object -> Basics.Bool
}
These parameters are used to evaluate which area an object belongs to.
For example, if you want to handle circular object { x: Float, y: Float, r: Float }
on a canvas 640x480,
Constraints
would be as shown:
{ range = { minX = 0, minY = 0, maxX = 639, maxY = 479 }
, boundingBox = \c -> { minX = c.x - c.r, minY = c.y - c.r, maxX = c.x + c.r, maxY = c.y + c.r }
, isCollided = \c0 c1 -> getLength ( c0.x, c0.y ) ( c1.x, c1.y ) |> lessThan (c0.r + c1.r)
}
Represents data structure used for space partitioning.
default : DataStructure
Default data structure.
fixedDepthTree : { depth : Basics.Int } -> DataStructure
Use quadtree as a data structure. Tree-depth is fixed from start to end.
merelyDict : DataStructure
Use Dict
as a data structure.
empty : DataStructure -> Constraint object -> Container comparable object
Create an empty container.
fromDict : DataStructure -> Constraint object -> Dict comparable object -> Container comparable object
Convert a dictionary into a container.
detect : Container comparable object -> Dict comparable { object : object, collidedWith : List ( comparable, object ) }
Detect collisions.
mapWithDetection : (List ( comparable, object ) -> comparable -> object -> object) -> Container comparable object -> Container comparable object
Map with collision results. For example, if you want to swap a color of object with the one of collided, the code would be as shown.
swapColor : List ( key, obj ) -> key -> obj -> obj
swapColor collidedObjs key obj =
collidedObjs |> List.head |> Maybe.map (\cObj -> { obj | color = cObj }) |> Maybe.withDefault obj
swapped =
mapWithDetection swapColor container
get : comparable -> Container comparable object -> Maybe object
Get an object from a container if it exists.
insert : comparable -> object -> Container comparable object -> Container comparable object
Insert key and object to a container.
remove : comparable -> Container comparable object -> Container comparable object
Remove an object from a container.
size : Container comparable object -> Basics.Int
Returns number of objects a container stores.
toDict : Container comparable object -> Dict comparable object
Convert a container to a Dict
map : (comparable -> object -> object) -> Container comparable object -> Container comparable object
Apply functions to all objects in a container.
foldl : (comparable -> object -> a -> a) -> a -> Container comparable object -> a
Foldl. It is equivalent to toDict |> Dict.foldl
foldr : (comparable -> object -> a -> a) -> a -> Container comparable object -> a
Foldr. It is equivalent to toDict |> Dict.foldr