Miscellaneous utility functions.
Vector math functions for position/rotation/velocity calculations.
Geometric collision detection predicates.
Misc utilities.
Miscellaneous utility functions. Vector math functions for position/rotation/velocity calculations. Geometric collision detection predicates. Misc utilities.
(center)
Create an [x y]
position at the centre of the current game width
and height.
Create an `[x y]` position at the centre of the current game width and height.
(coarse-polys-collide? poly-a poly-b)
Predicate to determine if two polygons possibly collide.
Checks if the minimum rectangles containing the polygons overlap. If
they do we should use fine-polys-collide?
to check properly.
Predicate to determine if two polygons possibly collide. Checks if the minimum rectangles containing the polygons overlap. If they do we should use `fine-polys-collide?` to check properly.
(coarse-pos-in-poly? [x y] poly)
Predicate to determine if a point is possibly inside a polygon.
Checks if the point is contanied by the minimum rectangle containing
the polygon. If the point is inside this rectangle we should use
fine-pos-in-poly?
to check properly.
Predicate to determine if a point is possibly inside a polygon. Checks if the point is contanied by the minimum rectangle containing the polygon. If the point is inside this rectangle we should use `fine-pos-in-poly?` to check properly.
(darken [_r _g _b a :as color])
Darken a colour by 30, preserving alpha component if present.
Darken a colour by 30, preserving alpha component if present.
(direction-vector r)
Calculate the unit direction vector based on the rotation angle.
Calculate the unit direction vector based on the rotation angle.
(equal-pos? pos-a pos-b)
Predicate to check if two positions are equal.
Predicate to check if two positions are equal.
(fine-polys-collide? poly-a poly-b)
Predicate to determine if two polygons overlap.
We first check if there are any points shared by the polygons, then we check if any of the lines intersect.
If no lines intersect it is still possible that one polygon is fully containing the other. In this case one polygon will contain all the points of the other. So we can just check if the first point of poly-a is contained in poly-b or vice versa.
Predicate to determine if two polygons overlap. We first check if there are any points shared by the polygons, then we check if any of the lines intersect. If no lines intersect it is still possible that one polygon is fully containing the other. In this case one polygon will contain all the points of the other. So we can just check if the first point of poly-a is contained in poly-b or vice versa.
(fine-pos-in-poly? pos poly)
Uses ray casting to check if a polygon encloses a pos.
We construct a line starting at our point and count how many of the polygon lines it intersects, an odd number of intersections means the point is inside the polygon.
Our line should be infinite, but in practice any large number will suffice.
Uses ray casting to check if a polygon encloses a pos. We construct a line starting at our point and count how many of the polygon lines it intersects, an odd number of intersections means the point is inside the polygon. Our line should be infinite, but in practice any large number will suffice.
(invert v)
Multiply each component of the vector by -1.
Represents a rotation of 180 degrees.
Multiply each component of the vector by -1. Represents a rotation of 180 degrees.
(lighten [_r _g _b a :as color])
Lighten a colour by 30, preserving alpha component if present.
Lighten a colour by 30, preserving alpha component if present.
(lines-intersect? [[x1 y1 :as p1] [x2 y2 :as p2] :as l1]
[[x3 y3 :as p3] [x4 y4 :as p4] :as l2])
Predicate to determine if two lines intersect.
We have decided that zero-length lines do not intersect as the complexity in determining their intersection is not worth the performance hit.
line a: (x1, y1) -> (x2, y2) line b: (x3, y3) -> (x4, y4)
lines intersect iff: 0.0 <= numerator-t/denominator-t <= 1.0 and 0.0 <= numerator-u/denominator-u <= 1.0
We can just assert that the fraction is bottom-heavy.
Predicate to determine if two lines intersect. We have decided that zero-length lines do not intersect as the complexity in determining their intersection is not worth the performance hit. line a: (x1, y1) -> (x2, y2) line b: (x3, y3) -> (x4, y4) lines intersect iff: 0.0 <= numerator-t/denominator-t <= 1.0 and 0.0 <= numerator-u/denominator-u <= 1.0 We can just assert that the fraction is bottom-heavy.
(magnitude v)
Calculate the length of a vector.
Calculate the length of a vector.
(make-pos [x-factor y-factor])
Create an [x y]
vector based on the supplied factors and the
current game width and height.
Create an `[x y]` vector based on the supplied factors and the current game width and height.
(ms->frames ms)
(ms->frames ms frame-rate)
Determines the expected number of frames to be processed in ms
milliseconds. Useful for defining tweens or delays based on time
instead of frame count directly.
Optionally takes a specific framerate (in case you want to use the
:average-fps
in the game state for a more stable result).
Minimum returned value is 1.
Determines the expected number of frames to be processed in `ms` milliseconds. Useful for defining tweens or delays based on time instead of frame count directly. Optionally takes a specific framerate (in case you want to use the `:average-fps` in the game state for a more stable result). Minimum returned value is 1.
(normalize v)
Calculate the unit vector of a given vector.
We calculate the reciprocal of the magnitude of the vector and multiply the components by this factor to avoid multiple division operations.
Calculate the unit vector of a given vector. We calculate the reciprocal of the magnitude of the vector and multiply the components by this factor to avoid multiple division operations.
(orthogonals [x y])
Calculate the two orthogonal vectors to a given 2D vector.
Y axis is inverted so this returns [90-degrees-right-vector 90-degrees-left-vector]
Calculate the two orthogonal vectors to a given 2D vector. Y axis is inverted so this returns [90-degrees-right-vector 90-degrees-left-vector]
(poly-lines poly)
Construct the lines that make up a polygon from its points.
Construct the lines that make up a polygon from its points.
(polys-collide? poly-a poly-b)
Predicate to check if two polygons overlap.
The fine-polys-collide?
predicate is expensive so we only do it if
the cheaper coarse-polys-collide?
says this is a possible
collision.
Predicate to check if two polygons overlap. The `fine-polys-collide?` predicate is expensive so we only do it if the cheaper `coarse-polys-collide?` says this is a possible collision.
(pos->ray [x y])
Creates an arbitrarily long line starting at the specified pos.
When doing poly->point collision detection a point lying on a horizontal edge of a poly would cause a division by zero if we used a horizontal ray.
This would be handled, but would not count as a collision so we increment y to make it much less likely that the intersecting lines are parallel.
Creates an arbitrarily long line starting at the specified pos. When doing poly->point collision detection a point lying on a horizontal edge of a poly would cause a division by zero if we used a horizontal ray. This would be handled, but would not count as a collision so we increment y to make it much less likely that the intersecting lines are parallel.
(pos-in-poly? pos poly)
Predicate to check if a pos is inside a polygon.
The fine-pos-in-poly?
predicate is expensive so we only do it if
the cheaper coarse-pos-in-poly?
says this is a possible
collision.
Predicate to check if a pos is inside a polygon. The `fine-pos-in-poly?` predicate is expensive so we only do it if the cheaper `coarse-pos-in-poly?` says this is a possible collision.
(pos-in-rect? [ax ay] [bx1 by1 bx2 by2])
Predicate to check if a position is inside a rectangle.
Predicate to check if a position is inside a rectangle.
(rects-overlap? [ax1 ay1 ax2 ay2] [bx1 by1 bx2 by2])
Predicate to determine if two rectangles overlap.
Predicate to determine if two rectangles overlap.
(rotate-vector [x y :as v] r)
Rotate a vector about the origin by r
degrees.
Checks first for r
representing an integer number of rotations, in
with case the vector will be unchanged.
Rotate a vector about the origin by `r` degrees. Checks first for `r` representing an integer number of rotations, in with case the vector will be unchanged.
(rotation-angle [x y])
Calculate the rotation angle of a vector.
Calculate the rotation angle of a vector.
(squared-magnitude [x y z])
Sum the squares of the components of a vector.
The if
check on z
is a lot faster than doing an apply
or
reduce
across the vector.
Sum the squares of the components of a vector. The `if` check on `z` is a lot faster than doing an `apply` or `reduce` across the vector.
(unit-vector v)
Calculate the unit vector of a given vector.
Calculate the unit vector of a given vector.
(v< a b)
Determine if the magnitude of a vector a
is less than the magnitude
of vector b
.
We can just compare the component squares to avoid the costly sqrt
operations.
Determine if the magnitude of a vector `a` is less than the magnitude of vector `b`. We can just compare the component squares to avoid the costly `sqrt` operations.
(v<= a b)
Determine if the magnitude of a vector a
is less than or equal to
the magnitude of vector b
.
We can just compare the component squares to avoid the costly sqrt
operations.
Determine if the magnitude of a vector `a` is less than or equal to the magnitude of vector `b`. We can just compare the component squares to avoid the costly `sqrt` operations.
(wrap-trans-rot [x y] r f)
Perform a translation, a rotation, invoke the supplied function (probably drawing a sprite, then reset the transform matrix to the identity.
Perform a translation, a rotation, invoke the supplied function (probably drawing a sprite, then reset the transform matrix to the identity.
(zero-vector? v)
Predicate to check if a vector has length 0.
Predicate to check if a vector has length 0.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close