This module has types and functions for dealing with collision detection on simple 2D shapes.
- data Shape' a
- moveShape :: Num a => Rel2' a -> Shape' a -> Shape' a
- rotateShape :: forall a. Floating a => a -> Shape' a -> Shape' a
- scaleShape :: Num a => a -> Shape' a -> Shape' a
- shapePoints :: Num a => Shape' a -> [Point2' a]
- boundingBox :: (Num a, Ord a) => Shape' a -> (Point2' a, Point2' a)
- overlap :: (Floating a, Ord a) => Shape' a -> Shape' a -> Maybe (Rel2' a, Rel2' a)
- intersectLineShape :: forall a. (Floating a, Ord a) => Line2' a -> Shape' a -> Maybe (a, a)
A type for simple 2D convex shapes. It is expected that you will define a
synonym in your own application such as
type Shape = Shape' Double, hence
the funny name.
A rectangle with a centre, and a width (distance from the centre
to the left or right side of the rectangle) and a height (distance
from the centre to the top or bottom side of the rectangle. So the
rectangle with corners (1,1) and (3,2) is
A circle with a centre and a radius.
A polygon with a centre, and a list of points. The points are relative
vectors from the centre of the polygon, and are expected to be in clockwise
order. For example, the triangle with corners (1,1) (3,3) and (3,1)
Note that whereabouts the centre is inside the polygon is up to you (it does not have to be the geometric average of the points), but it should at least be inside the polygon, or else some algorithms will behave strangely with it.
The list of points should have at least 3 points in it, or else some algorithms will behave strangely.
If your points are not in clockwise order (with the X-Y axes being how they are in graphs, not on screens), funny things will happen with the collision detection.
Moves a shape by a given vector (by moving the centre).
Given an angle in radians, rotates the shape by that angle in an anti-clockwise direction. A circle will remain untouched, a polygon will have its points rotated, and a rectangle will become a polygon and get rotated (even if you pass 0 as the angle).
Scales the size of the shape (for all edges, from the centre) by the given factor.
Given a shape, gets a list of points that make up the vertices of the shape. For circles, this list will be empty.
Gives back the bounding box of a shape in terms of the minimum X-Y and the maximum X-Y corners of the bounding box.
Checks for overlap between the two shapes. If they do not collide, returns Nothing. If they do collide, gives back suggested angles away from each other. These are not necessarily the shortest direction to separate the two shapes, but should be decent for doing collision resolution (by using them as surface normals, or push-away vectors)
The first vector returned is the direction in which the first shape should head (or the surface normal to bounce the first shape off), whereas the second vector returned is the direction in which the second shape should head (or the surface normal to bounce the second shape off).
This function includes an initial quick test, followed by a more detailed test if necessary.
Given a line and a shape, finds all possible intersections of the line
with the shape. Since the shapes are convex, continuous 2D shapes, there
will either be no intersections or two (which could be the same point).
The returned value is distance along the line in multiples of the direction
vector (the return value is the same idea as