Code Monkey home page Code Monkey logo

collision's Introduction

Info

Collision is a python library that is meant for collision detection between convex and concave polygons, circles, and points.

Installation

To get the latest stable version:

pip install collision

To get the latest development version run:

pip install https://github.com/qwertyquerty/collision/archive/master.zip

Classes

class collision.Vector(x, y)

A 2D vector/point class

Properties:

  • x (int) or (float) - The x-coordinate
  • y (int) or (float) - The y-coordinate

Methods:

func copy()collision.vec

Return a copy of the vector

func set(v)

Copy another vector's values onto the target vector

  • v (collision.vec) - The vector to copy from
func perp()collision.vec

Return the vector rotated perpendicularly

func rotate(angle)collision.vec

Return the vector rotated to the angle

  • angle (int) or (float) - Radians to rotate the point
func reverse()collision.vec

Return a reversed version of the vector

func normalize()collision.vec

Return a normalized version of the vector

func project(v)collision.vec

Return the vector projected onto the passed vector

  • v (collision.vec) - The vector to project upon
func project_n(v)collision.vec

Return the vector projected onto a unit vector

  • v (collision.vec) - The vector to project upon
func reflect(axis)collision.vec

Return the vector reflected upon the passed axis vector

  • axis (collision.vec) - The axis to reflect upon
func reflect_n(axis)collision.vec

Return the vector reflected upon the passed axis unit vector

  • axis (collision.vec) - The axis to reflect upon
func dot(v)int or float

Returns the dot product of the vector and another

  • v (collision.vec) - The other vector for the dot product
func ln()int or float

Returns the length of the vector

func ln2()int or float

Returns the squared length of the vector


class collision.Circle(pos, radius)

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

class collision.Poly(pos, points, angle = 0)

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-aligned bounding box of the Poly

Class Methods:

func Poly.from_box(pos, width, height)collision.Poly

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:

func set_points(points)

Change the base points relative to the position. After this is done, the polygon will be recalculated again. The angle will be preserved. Use this instead of editing the points property.

func get_centroid()collision.vec

Get the centroid of the polygon. Arithmetic means all of the points.


class collision.Concave_Poly(pos, points, angle = 0)

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 makes up the concave polygon is 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:

func set_points(points)

Change the base points relative to the position. After this is done, the polygon will be recalculated again. The angle will be preserved. Use this instead of editing the points property.

func get_centroid()collision.vec

Get the centroid of the polygon. Arithmetic means all of the points.


class collision.Response()

The result of a collision between two objects. It 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 of a, a and b will no longer be colliding.
  • a_in_b (bool) - Whether a is fully inside of b
  • b_in_a (bool) - Whether b is fully inside of a

Methods:

func reset()collision.Response

Reset the Response for re-use, and returns itself

Collisions

func collision.collide(a, b, response = None)bool

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 its default values. Concave polys currently do not support responses.

  • 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's a collision.

func collision.test_aabb(a, b)bool

Test two axis-aligned bounding boxes against each other. This is already done in collision.collide so there's 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

collision's People

Contributors

niknikovsky avatar qwertyquerty avatar thespookycat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

collision's Issues

Return type of Poly.points

The return type of points is "list of Vector2".
Has this a reason? Isn't better to return "list of Vector" (eg Vector2 has no copy() method)?
Somehow this breaks the architecture... For example I cannot (easily) construct a new Poly out of existing Poly.points.

I also miss the possibility to construct Vector as Vector(Vector).
Regards
Gogo

Split or minimize the overlapping polygons from the intersection line between the overlapped region.

Hi I want to seperate overlapping polygons. I have convex hulls of different masks on the same image. Some of the polygons are overlapping. I want to minimize those polygons so they now dont overlap at all. My idea is to find an intersection line as shown in the image below. Is this possible? How to do it?

I want to move or reduce them just as much as the intersection line could be each polygon's one sided boundary

MicrosoftTeams-image (2)
MicrosoftTeams-image (1)

Python 2.7 Compatibility

I tried using this library with Python 2.7 (installed via pip) but had no success.

import collision

yields the following error:

  File "/home/stangier/.local/lib/python2.7/site-packages/collision/__init__.py", line 8, in <module>
    from .circle import Circle
  File "/home/stangier/.local/lib/python2.7/site-packages/collision/circle.py", line 3, in <module>
    from . import poly
  File "/home/stangier/.local/lib/python2.7/site-packages/collision/poly.py", line 3, in <module>
    from .util import Vector
  File "/home/stangier/.local/lib/python2.7/site-packages/collision/util.py", line 19
    def __add__(self, other: Any):

I suppose this raises an error because this library uses Python type hints, which were introduced in version 3.5. I don't expect you to port this library to Python 2.7 because it's outdated (although it would be nice to have), but it would be great if you could change you PyPi entry accordingly and remove it from the Python 2.7 pip repository.

collide() response does not work

When I collide 2 polygons and check the response for the overlap the response is not modified. I have attached a short piece of code to show what I mean.

from collision import *
from collision import Vector as v

p1 = Concave_Poly(v(0,0), [v(0,0), v(10,10), v(10,0)])
p2 = Concave_Poly(v(0,0), [v(0,0), v(0,5), v(5,5), v(5,0)])

r = Response()
r.reset()
c = collide(p1, p2, response = r)

if c:
    print(r)

The two polygons clearly overlap but the response is:

Response [
        a = None

        b = None

        overlap = inf
        overlap_n = Vector [0, 0]
        overlap_v = Vector [0, 0]
        a_in_b = True
        b_in_a = True
]

I'm running this with Python 3.9.1

Algorithm issue

I believe, there is an issue in the collision recognition. Example:

`import pygame as pg
import sys
import os
from collision import *

SCREENSIZE = (1200,900)
screen = pg.display.set_mode(SCREENSIZE, pg.DOUBLEBUF|pg.HWACCEL)

v = Vector
p0 = Concave_Poly(v(0,0), [])
p0.set_points([
Vector(697.1654965313376, 179.9115879140274),
Vector(764.9587442373818, 222.3859986276198),
Vector(743.3817435948768, 256.82496846229026),
Vector(675.5884958888325, 214.35055774869784),
])
p1 = Concave_Poly(v(0,0), [])
p1.set_points([
Vector(875.0138897016246, 123.17802988652383),
Vector(943.1811884599225, 165.04949248334466),
Vector(949.304599935167, 170.46229454571227),
Vector(1019.8568670514846, 271.73421253085047),
Vector(1040.1360798659869, 381.7005038144338),
Vector(1020.1348828367004, 479.85313011280556),
Vector(974.5176923937827, 555.6645122896266),
Vector(939.825961238999, 534.4962957968701),
Vector(980.5408637494689, 470.69216659798064),
Vector(999.8705586307079, 387.20481118696586),
Vector(985.0376168510401, 292.6920133572453),
Vector(927.0230429423466, 204.44967187871185),
Vector(921.9104854607375, 199.67848025255998),
Vector(853.7431867024396, 157.80701765573914),
])

clock = pg.time.Clock()
stop = False
cont = False
while 1:
for event in pg.event.get():
if event.type == pg.QUIT:
sys.exit()
pressed = pg.key.get_pressed()

    # Hit SPACE to continue after the error
    if pressed[pg.K_SPACE]: cont = True

screen.fill((0,0,0))

if not stop or cont: p0.pos.x += 0.1

p0c, p1c, p2c = (0,255,255),(0,255,255),(0,255,255)
p0bc = (255,255,255)
p1bc = (255,255,255)

if collide(p0,p1): 
    p1c = (255,0,0); p0c = (255,0,0);
    stop = True
    
if test_aabb(p0.aabb,p1.aabb): p1bc = (255,0,0); p0bc = (255,0,0);

pg.draw.polygon(screen, p0c, p0.points, 3)
pg.draw.polygon(screen, p1c, p1.points, 3)
pg.draw.polygon(screen, p0bc, (p0.aabb[0],p0.aabb[1],p0.aabb[3],p0.aabb[2]), 3)
pg.draw.polygon(screen, p1bc, (p1.aabb[0],p1.aabb[1],p1.aabb[3],p1.aabb[2]), 3)

pg.display.flip()
clock.tick(100)

`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.