collision.py
Info
Collision is a python library meant for collision detection between convex and concave polygons, circles, and points.
Insallation
To get the latest development version:
pip install https://github.com/QwekoDev/collision/archive/master.zip
Classes
collision.Vector(x, y)
class A 2D vector/point class
Properties:
-
x
(int) or (float) - The x-coordinate -
y
(int) or (float) - The y-coordinate
Methods:
copy()
→ collision.vec
func Return a copy of the vector
set(v)
func Copy another vectors values onto the vector
-
v
(collision.vec) - The vector to copy from
perp()
→ collision.vec
func Return the vector rotated perpandicularly
rotate(angle)
→ collision.vec
func Return the vector rotated the angle
-
angle
(int) or (float) - Radians to rotate the point
reverse()
→ collision.vec
func Return a reversed version of the vector
normalize()
→ collision.vec
func Return a normalized version of the vector
project(v)
→ collision.vec
func Return the vector projected onto the passed vector
-
v
(collision.vec) - The vector to project upon
project_n(v)
→ collision.vec
func Return the vector projected onto a unit vector
-
v
(collision.vec) - The vector to project upon
reflect(axis)
→ collision.vec
func Return the vector reflected upon the passed axis vector
-
axis
(collision.vec) - The axis to reflect upon
reflect_n(axis)
→ collision.vec
func Return the vector reflected upon the passed axis unit vector
-
axis
(collision.vec) - The axis to reflect upon
dot(v)
→ int or float
func Returns the dot product of the vector and another
-
v
(collision.vec) - The other vector for the dot product
ln()
→ int or float
func Returns the length of the vector
ln2()
→ int or float
func Returns the squared length of the vector
collision.Circle(pos, radius)
class A simple circle with a position and radius
Properties:
-
pos
(collision.vec) - The center coordinate of the circle -
radius
(int) or (float) - The radius of the circle -
aabb
(tuple(tuple(int or float)) - The axis alligned bounding box of the circle
collision.Poly(pos, points, angle = 0)
class A convex polygon with a position, a list of points relative to that position, and an angle
Properties:
-
pos
(collision.vec) - The center coordinate of the circle -
points
(list[collision.vec]) - A list of absolute points (each relative point + the position of the polygon.) Can not be directly edited. -
rel_points
(list[collision.vec]) - A list of points relative to the position. This property should not be directly changed. -
angle
(int) or (float) - The angle which the polygon is rotated. Changing this will cause the polygon to be recalculated. -
aabb
(tuple(tuple(int or float)) - The axis alligned bounding box of the Poly
Class Methods:
Poly.from_box(pos, width, height)
→ collision.Poly
func Creates a polygon from
-
pos
(collision.vec) - The center coordinate of the polygon/box -
width
(int) or (float) - The width of the box -
height
(int) or (float) - The height of the box
Methods:
set_points(points)
func Change the base points relative to the position. After this is done, the polygon will be recalculated again. Angle will be preserved. Use this instead of editing the points
property.
get_centroid()
→ collision.vec
func Get the centroid of the polygon. The arithmatic mean of all of the points.
collision.Concave_Poly(pos, points, angle = 0)
class A concave polygon with a position, a list of points relative to that position, and an angle. This takes longer to collide than a regular Poly
does, so only use this if your shape must be concave.
Properties:
-
pos
(collision.vec) - The center coordinate of the circle -
points
(list[collision.vec]) - A list of absolute points (each relative point + the position of the polygon.) Can not be directly edited. -
rel_points
(list[collision.vec]) - A list of points relative to the position. This property should not be directly changed. -
tris
(list[collision.Poly]) - A list of triangles relative to the position on the poly that make up the concave polygon. Used for concave collisions. -
angle
(int) or (float) - The angle which the polygon is rotated. Changing this will cause the polygon to be recalculated. -
aabb
(tuple(tuple(int or float)) - The axis alligned bounding box of the Poly
Methods:
set_points(points)
func Change the base points relative to the position. After this is done, the polygon will be recalculated again. Angle will be preserved. Use this instead of editing the points
property.
get_centroid()
→ collision.vec
func Get the centroid of the polygon. The arithmatic mean of all of the points.
collision.Response()
class The result of a collision between two objects. May optionally be passed to collision tests to retrieve additional information. At its cleared state, it may seem to have odd values. Ignore these, they are just there to make generating the response more efficient. The response should be ignored unless there is a successful collision.
Properties:
-
a
(collision shape) - The first object in the collision test -
b
(collision shape) - The second object in the collision test -
overlap
(int) or (float) - Magnitude of the overlap on the shortest colliding axis -
overlap_n
(collision.vec) - The shortest colliding axis (unit vector) -
overlap_v
(collision.vec) - The overlap vector. If this is subtracted from the position ofa
,a
andb
will no longer be colliding. -
a_in_b
(bool) - Whethera
is fully inside ofb
-
b_in_a
(bool) - Whetherb
is fully inside ofa
Methods:
reset()
→ collision.Response
func Reset the Response for re-use, and returns itself
Collisions
collision.collide(a, b, response = None)
→ bool
func Test two shapes against each other. If a response is passed, and there is a collision, that response will be updated to the response values. The response will not be generated if there is no collision and it will be at it's default values.
-
a
(collision shape) - The first shape to test -
b
(collision shape) - The second shape to test -
response
(collision.Response) - Optional response that will be updated if there is a collision.
collision.test_aabb(a, b)
→ bool
func Test two axis aligned bounding boxes against each other. This is already done in collision.collide
so there is no need for you to do it for optimization.
-
a
(tuple(tuple(int or float))) The first AABB -
b
(tuple(tuple(int or float))) The second AABB