|Portability||portable (needs FFI)|
Shapes used for collisions, their properties and some useful polygon functions.
- data Shape
- data ShapeType
- newShape :: Body -> ShapeType -> Position -> IO Shape
- type CollisionType = Word32
- getCollisionType :: Shape -> IO CollisionType
- setCollisionType :: Shape -> CollisionType -> IO ()
- type Group = Word32
- getGroup :: Shape -> IO Group
- setGroup :: Shape -> Group -> IO ()
- type Layers = Word32
- getLayers :: Shape -> IO Layers
- setLayers :: Shape -> Layers -> IO ()
- type Elasticity = CpFloat
- getElasticity :: Shape -> IO Elasticity
- setElasticity :: Shape -> Elasticity -> IO ()
- type Friction = CpFloat
- getFriction :: Shape -> IO Friction
- setFriction :: Shape -> Friction -> IO ()
- type SurfaceVel = Vector
- getSurfaceVel :: Shape -> IO SurfaceVel
- setSurfaceVel :: Shape -> SurfaceVel -> IO ()
- getBody :: Shape -> Body
- momentForCircle :: CpFloat -> (CpFloat, CpFloat) -> Position -> CpFloat
- momentForPoly :: CpFloat -> [Position] -> Position -> CpFloat
- shapeQuery :: Shape -> Position -> IO Bool
- type Segment = (Position, Position)
- data Intersection
- epsilon :: CpFloat
- (.==.) :: CpFloat -> CpFloat -> Bool
- isLeft :: (Position, Position) -> Position -> Ordering
- isClockwise :: [Position] -> Bool
- isConvex :: [Position] -> Bool
- intersects :: Segment -> Segment -> Intersection
- polyReduce :: CpFloat -> [Position] -> [Position]
- polyCenter :: [Position] -> Position
- convexHull :: [Position] -> [Position]
A collision shape is attached to a
Body to define its
shape. Multiple shapes may be attached, including
overlapping ones (shapes of a body don't generate collisions
with each other).
There are three types of shapes that can be attached to bodies:
A circle is the fastest collision type. It also rolls smoothly.
A line segment is meant to be used as a static shape. (It can be used with moving bodies, however two line segments never generate collisions between each other.)
Polygons are the slowest of all shapes but the most flexible. The list of vertices must form a convex hull with clockwise winding. Note that if you want a non-convex polygon you may add several convex polygons to the body.
newShape b type off creates a new shape attached to
b at offset
off. Note that you have to
add the shape to a space otherwise it won't generate
The collision type is used to determine which collision
Physics.Hipmunk.Space.Callback will be called. Its
actual value doesn't have a meaning for Chipmunk other
than the correspondence between shapes and the collision
pair functions you add. (default is zero)
Groups are used to filter collisions between shapes. If the group is zero, then it imposes no restriction to the collisions. However, if the group is non-zero then the shape will not collide with other shapes in the same non-zero group. (default is zero)
This is primarely used to create multi-body, multi-shape objects such as ragdolls. It may be thought as a lightweight alternative to creating a callback that filters the collisions.
Layers are similar to groups, but use a bitmask. For a collision
to occur, two shapes must have at least one layer in common.
In other words,
layer1 .&. layer2 should be non-zero.
Note that although this type may have more than 32 bits, for portability you should only rely on the lower 32 bits.
The elasticity of the shape is such that
0.0 gives no bounce
1.0 give a "perfect" bounce. Note that due to
1.0 or greater is not recommended.
The amount of elasticity applied during a collision is calculated by multiplying the elasticity of both shapes. (default is zero)
IMPORTANT: by default no elastic iterations are done
when the space
Physics.Hipmunk.Space.steps. This means
that all shapes react as they had zero elasticity.
So, if you want some elasticity, remember to call
Physics.Hipmunk.Space.setElasticIterations to something
greater than zero, maybe
The friction coefficient of the shape according
to Coulumb friction model (i.e.
0.0 is frictionless,
iron on iron is around
1.0, and it could be greater
The amount of friction applied during a collision is determined by multiplying the friction coefficient of both shapes. (default is zero)
The surface velocity of the shape. Useful to create conveyor belts and players that move around. This value is only used when calculating friction, not collision. (default is zero)
getBody s is the body that this shape is associated
to. Useful especially in
momentForCircle m (ri,ro) off is the moment of inertia
of a circle of
m mass, inner radius of
ri, outer radius
ro and at an offset
off from the center of the body.
momentForPoly m verts off is the moment of inertia of a
m mass, at offset
off from the center of
the body and comprised of
verts vertices. This is similar
shapePoly (and the same restrictions for the vertices
apply as well).
shapeQuery shape p returns
True iff the point in
p (in world's coordinates) lies within
This section is inspired by
a Python module made from http://code.google.com/p/pymunk/,
although implementations are quite different.
Also, unless noted otherwise all polygons are assumed to be simple (i.e. no overlapping edges).
A possible intersection between two segments.
Intercept in a point.
Share a segment.
The epsilon used in the algorithms below when necessary to compare floats for "equality".
epsilon. That is,
a .==. b
abs (a - b) <= epsilon.
isLeft (p1,p2) vert is
vertis at the left of the line defined by
vertis at the line
isClockwise verts is
a clockwise polygon.
intersects seg1 seg2 is the intersection between
the two segments
polyReduce delta verts removes from
points that have less than
in relation to the one preceding it.
Note that a very small polygon may be completely "eaten"
if all its vertices are within a
delta radius from the
polyCenter verts is the position in the center
of the polygon formed by
O(n log n).
convexHull verts is the convex hull of the
polygon defined by
verts. The vertices of the convex
hulls are given in clockwise winding. The polygon
doesn't have to be simple.
Implemented using Graham scan, see http://cgm.cs.mcgill.ca/~beezer/cs507/3coins.html.