-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapes.py
More file actions
63 lines (55 loc) · 2.05 KB
/
shapes.py
File metadata and controls
63 lines (55 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from vector2D import Vector2D
import mathUtil
class Shape:
def __init__(self, position):
self.position = position.Clone()
class CollisionResult:
def __init__(self, objA, objB, intersect, normal, depth):
self.objA = objA
self.objB = objB
self.intersect = intersect
self.normal = normal
self.depth = depth
class Circle(Shape):
def __init__(self, position, radius):
super().__init__(position)
self.radius = radius
def Intersects(self, other):
if isinstance(other, Circle):
d = other.position - self.position
dist = d.Length()
if dist < self.radius + other.radius:
depth = self.radius + other.radius - dist
normal = d.Clone()
normal.Normalize()
intersect = self.position + normal * (self.radius - depth)
return CollisionResult(None, None, intersect, normal, depth)
if isinstance(other, Rectangle):
pass
return None
class Rectangle(Shape):
def __init__(self, position, width, height):
super().__init__(position)
self.width = width
self.height = height
def ClosestPoint(self, point):
x = point.x
y = point.y
px = self.position.x
py = self.position.y
qx = px + self.width
qy = py + self.height
return Vector2D(mathUtil.Clamp(x, px, qx), mathUtil.Clamp(y, py, qy))
def Intersects(self, other):
if isinstance(other, Circle):
closest = self.ClosestPoint(other.position)
dist = other.position.DistanceToPoint(closest)
if dist < other.radius:
depth = other.radius - dist
normal = other.position - closest
normal.Normalize()
intersect = closest - normal * depth
return CollisionResult(None, None, intersect, normal, depth)
if isinstance(other, Rectangle):
pass
return None