basilisk-engine 0.1.15__py3-none-any.whl → 0.1.17__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of basilisk-engine might be problematic. Click here for more details.
- basilisk/__init__.py +15 -14
- basilisk/audio/sound.py +27 -27
- basilisk/bsk_assets/cube.obj +48 -48
- basilisk/collisions/broad/broad_aabb.py +102 -102
- basilisk/collisions/broad/broad_bvh.py +137 -137
- basilisk/collisions/collider.py +95 -95
- basilisk/collisions/collider_handler.py +224 -224
- basilisk/collisions/narrow/contact_manifold.py +95 -95
- basilisk/collisions/narrow/dataclasses.py +34 -34
- basilisk/collisions/narrow/deprecated.py +46 -46
- basilisk/collisions/narrow/epa.py +91 -91
- basilisk/collisions/narrow/gjk.py +66 -66
- basilisk/collisions/narrow/graham_scan.py +24 -24
- basilisk/collisions/narrow/helper.py +29 -29
- basilisk/collisions/narrow/line_intersections.py +106 -106
- basilisk/collisions/narrow/sutherland_hodgman.py +75 -75
- basilisk/config.py +2 -2
- basilisk/draw/draw.py +100 -100
- basilisk/draw/draw_handler.py +179 -179
- basilisk/draw/font_renderer.py +28 -28
- basilisk/engine.py +206 -206
- basilisk/generic/abstract_bvh.py +15 -15
- basilisk/generic/abstract_custom.py +133 -133
- basilisk/generic/collisions.py +72 -72
- basilisk/generic/input_validation.py +66 -66
- basilisk/generic/math.py +6 -6
- basilisk/generic/matrices.py +35 -35
- basilisk/generic/meshes.py +72 -72
- basilisk/generic/quat.py +142 -142
- basilisk/generic/quat_methods.py +7 -7
- basilisk/generic/raycast_result.py +26 -26
- basilisk/generic/vec3.py +143 -143
- basilisk/input/mouse.py +61 -61
- basilisk/input/path.py +14 -14
- basilisk/mesh/cube.py +33 -33
- basilisk/mesh/mesh.py +230 -230
- basilisk/mesh/mesh_from_data.py +130 -130
- basilisk/mesh/model.py +271 -271
- basilisk/mesh/narrow_aabb.py +89 -89
- basilisk/mesh/narrow_bvh.py +91 -91
- basilisk/mesh/narrow_primative.py +23 -23
- basilisk/nodes/helper.py +28 -28
- basilisk/nodes/node.py +684 -684
- basilisk/nodes/node_handler.py +95 -95
- basilisk/particles/particle_handler.py +64 -63
- basilisk/particles/particle_renderer.py +93 -87
- basilisk/physics/impulse.py +112 -112
- basilisk/physics/physics_body.py +43 -43
- basilisk/physics/physics_engine.py +35 -35
- basilisk/render/batch.py +105 -105
- basilisk/render/camera.py +211 -211
- basilisk/render/chunk.py +106 -106
- basilisk/render/chunk_handler.py +165 -165
- basilisk/render/frame.py +101 -101
- basilisk/render/framebuffer.py +130 -130
- basilisk/render/image.py +87 -87
- basilisk/render/image_handler.py +122 -122
- basilisk/render/light.py +96 -96
- basilisk/render/light_handler.py +58 -58
- basilisk/render/material.py +219 -219
- basilisk/render/material_handler.py +135 -135
- basilisk/render/post_process.py +132 -132
- basilisk/render/shader.py +110 -110
- basilisk/render/shader_handler.py +80 -79
- basilisk/render/sky.py +120 -120
- basilisk/scene.py +276 -276
- basilisk/shaders/batch.frag +276 -276
- basilisk/shaders/batch.vert +115 -115
- basilisk/shaders/crt.frag +31 -31
- basilisk/shaders/draw.frag +21 -21
- basilisk/shaders/draw.vert +21 -21
- basilisk/shaders/filter.frag +22 -22
- basilisk/shaders/frame.frag +12 -12
- basilisk/shaders/frame.vert +13 -13
- basilisk/shaders/geometry.frag +8 -8
- basilisk/shaders/geometry.vert +41 -41
- basilisk/shaders/normal.frag +59 -59
- basilisk/shaders/normal.vert +96 -96
- basilisk/shaders/particle.frag +71 -71
- basilisk/shaders/particle.vert +84 -84
- basilisk/shaders/sky.frag +9 -9
- basilisk/shaders/sky.vert +13 -13
- {basilisk_engine-0.1.15.dist-info → basilisk_engine-0.1.17.dist-info}/METADATA +38 -45
- basilisk_engine-0.1.17.dist-info/RECORD +103 -0
- {basilisk_engine-0.1.15.dist-info → basilisk_engine-0.1.17.dist-info}/WHEEL +1 -1
- basilisk_engine-0.1.15.dist-info/RECORD +0 -103
- {basilisk_engine-0.1.15.dist-info → basilisk_engine-0.1.17.dist-info}/top_level.txt +0 -0
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
import glm
|
|
2
|
-
from random import randint
|
|
3
|
-
from .line_intersections import line_line_intersect, line_poly_intersect
|
|
4
|
-
from .graham_scan import graham_scan
|
|
5
|
-
from .sutherland_hodgman import sutherland_hodgman
|
|
6
|
-
from .dataclasses import ContactPoint
|
|
7
|
-
from ...generic.vec3 import Vec3
|
|
8
|
-
from ...generic.quat import Quat
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
# sutherland hodgman clipping algorithm
|
|
13
|
-
def get_contact_manifold(contact_plane_point:glm.vec3, contact_plane_normal:glm.vec3, points1:list[glm.vec3], points2:list[glm.vec3]) -> list[glm.vec3]:
|
|
14
|
-
"""
|
|
15
|
-
computes the contact manifold for a collision between two nearby polyhedra
|
|
16
|
-
"""
|
|
17
|
-
if len(points1) == 0 or len(points2) == 0: return []
|
|
18
|
-
|
|
19
|
-
# project vertices onto the 2d plane
|
|
20
|
-
points1 = project_points(contact_plane_point, contact_plane_normal, points1)
|
|
21
|
-
points2 = project_points(contact_plane_point, contact_plane_normal, points2)
|
|
22
|
-
|
|
23
|
-
# check if collsion was on a vertex
|
|
24
|
-
if len(points1) == 1: return points1
|
|
25
|
-
if len(points2) == 1: return points2
|
|
26
|
-
|
|
27
|
-
# convert points to 2d for intersection algorithms
|
|
28
|
-
points1, u1, v1 = points_to_2d(contact_plane_point, contact_plane_normal, points1)
|
|
29
|
-
points2, u2, v2 = points_to_2d(contact_plane_point, contact_plane_normal, points2, u1, v1) #TODO precalc orthogonal basis for 2d conversion
|
|
30
|
-
|
|
31
|
-
# convert arbitrary points to polygon
|
|
32
|
-
if len(points1) > 2: points1 = graham_scan(points1)
|
|
33
|
-
if len(points2) > 2: points2 = graham_scan(points2)
|
|
34
|
-
|
|
35
|
-
# run clipping algorithms
|
|
36
|
-
manifold = []
|
|
37
|
-
is_line1, is_line2 = len(points1) == 2, len(points2) == 2
|
|
38
|
-
if is_line1 and is_line2: manifold = line_line_intersect(points1, points2)
|
|
39
|
-
else:
|
|
40
|
-
if is_line1: manifold = line_poly_intersect(points1, points2)
|
|
41
|
-
elif is_line2: manifold = line_poly_intersect(points2, points1)
|
|
42
|
-
else: manifold = sutherland_hodgman(points1, points2)
|
|
43
|
-
|
|
44
|
-
# fall back if manifold fails to develope
|
|
45
|
-
if len(manifold) == 0: return []
|
|
46
|
-
|
|
47
|
-
# convert inertsection algorithm output to 3d
|
|
48
|
-
return points_to_3d(u1, v1, contact_plane_point, manifold)
|
|
49
|
-
|
|
50
|
-
def separate_polytope(points1: list[ContactPoint], points2: list[ContactPoint], contact_plane_normal, epsilon: float=1e-5) -> tuple[list[ContactPoint], list[ContactPoint]]:
|
|
51
|
-
"""
|
|
52
|
-
Determines the potential contact manifold points of each shape based on their position along the penetrating axis
|
|
53
|
-
"""
|
|
54
|
-
|
|
55
|
-
proj1 = [(glm.dot(point.vertex, contact_plane_normal), point) for point in points1]
|
|
56
|
-
proj2 = [(glm.dot(point.vertex, contact_plane_normal), point) for point in points2]
|
|
57
|
-
|
|
58
|
-
# min1 and max2 should be past the collising points of node2 and node1 respectively
|
|
59
|
-
min1 = min(proj1, key=lambda proj: proj[0])[0]
|
|
60
|
-
max2 = max(proj2, key=lambda proj: proj[0])[0]
|
|
61
|
-
|
|
62
|
-
proj1 = filter(lambda proj: proj[0] <= max2 + epsilon, proj1)
|
|
63
|
-
proj2 = filter(lambda proj: proj[0] + epsilon >= min1, proj2)
|
|
64
|
-
|
|
65
|
-
return [point[1] for point in proj1], [point[1] for point in proj2]
|
|
66
|
-
|
|
67
|
-
def distance_to_plane(contact_plane_point:glm.vec3, contact_plane_normal:glm.vec3, point:glm.vec3) -> float:
|
|
68
|
-
"""gets the smallest distance a point is from a plane"""
|
|
69
|
-
return glm.dot(point - contact_plane_point, contact_plane_normal) #TODO check this formula
|
|
70
|
-
|
|
71
|
-
def project_points(contact_plane_point:glm.vec3, contact_plane_normal:glm.vec3, points:list[Vec3]) -> list[glm.vec3]:
|
|
72
|
-
"""gets the projected positions of the given points onto the given plane"""
|
|
73
|
-
return [point - glm.dot(point - contact_plane_point, contact_plane_normal) * contact_plane_normal for point in points]
|
|
74
|
-
|
|
75
|
-
def points_to_2d(contact_plane_point:glm.vec3, contact_plane_normal:glm.vec3, points:list[glm.vec3], u = None, v = None) -> tuple[list[glm.vec2], glm.vec3, glm.vec3]:
|
|
76
|
-
"""converts a list of points on a plane to their 2d representation"""
|
|
77
|
-
# generate a new basis
|
|
78
|
-
k = get_noncolinear_vector(contact_plane_normal)
|
|
79
|
-
u = u if u else glm.normalize(glm.cross(contact_plane_normal, k))
|
|
80
|
-
v = v if v else glm.cross(contact_plane_normal, u)
|
|
81
|
-
|
|
82
|
-
# convert points to new basis
|
|
83
|
-
return [glm.vec2(glm.dot(vec := point - contact_plane_point, u), glm.dot(vec, v)) for point in points], u, v
|
|
84
|
-
|
|
85
|
-
def points_to_3d(u:glm.vec3, v:glm.vec3, contact_plane_point:glm.vec3, points:list[glm.vec2]) -> list[glm.vec3]:
|
|
86
|
-
"""converts a list of points on a plane to their 3d representation"""
|
|
87
|
-
return [contact_plane_point + point.x * u + point.y * v for point in points]
|
|
88
|
-
|
|
89
|
-
# vector math
|
|
90
|
-
def get_noncolinear_vector(vector:glm.vec3) -> glm.vec3:
|
|
91
|
-
"""generates a non colinear vector based on the given vector"""
|
|
92
|
-
test_vector = (1, 1, 1)
|
|
93
|
-
while glm.cross(test_vector, vector) == (0, 0, 0):
|
|
94
|
-
val = randint(0, 7) # 000 to 111
|
|
95
|
-
test_vector = (val & 1, val & 2, val & 4) # one random for three digits
|
|
1
|
+
import glm
|
|
2
|
+
from random import randint
|
|
3
|
+
from .line_intersections import line_line_intersect, line_poly_intersect
|
|
4
|
+
from .graham_scan import graham_scan
|
|
5
|
+
from .sutherland_hodgman import sutherland_hodgman
|
|
6
|
+
from .dataclasses import ContactPoint
|
|
7
|
+
from ...generic.vec3 import Vec3
|
|
8
|
+
from ...generic.quat import Quat
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# sutherland hodgman clipping algorithm
|
|
13
|
+
def get_contact_manifold(contact_plane_point:glm.vec3, contact_plane_normal:glm.vec3, points1:list[glm.vec3], points2:list[glm.vec3]) -> list[glm.vec3]:
|
|
14
|
+
"""
|
|
15
|
+
computes the contact manifold for a collision between two nearby polyhedra
|
|
16
|
+
"""
|
|
17
|
+
if len(points1) == 0 or len(points2) == 0: return []
|
|
18
|
+
|
|
19
|
+
# project vertices onto the 2d plane
|
|
20
|
+
points1 = project_points(contact_plane_point, contact_plane_normal, points1)
|
|
21
|
+
points2 = project_points(contact_plane_point, contact_plane_normal, points2)
|
|
22
|
+
|
|
23
|
+
# check if collsion was on a vertex
|
|
24
|
+
if len(points1) == 1: return points1
|
|
25
|
+
if len(points2) == 1: return points2
|
|
26
|
+
|
|
27
|
+
# convert points to 2d for intersection algorithms
|
|
28
|
+
points1, u1, v1 = points_to_2d(contact_plane_point, contact_plane_normal, points1)
|
|
29
|
+
points2, u2, v2 = points_to_2d(contact_plane_point, contact_plane_normal, points2, u1, v1) #TODO precalc orthogonal basis for 2d conversion
|
|
30
|
+
|
|
31
|
+
# convert arbitrary points to polygon
|
|
32
|
+
if len(points1) > 2: points1 = graham_scan(points1)
|
|
33
|
+
if len(points2) > 2: points2 = graham_scan(points2)
|
|
34
|
+
|
|
35
|
+
# run clipping algorithms
|
|
36
|
+
manifold = []
|
|
37
|
+
is_line1, is_line2 = len(points1) == 2, len(points2) == 2
|
|
38
|
+
if is_line1 and is_line2: manifold = line_line_intersect(points1, points2)
|
|
39
|
+
else:
|
|
40
|
+
if is_line1: manifold = line_poly_intersect(points1, points2)
|
|
41
|
+
elif is_line2: manifold = line_poly_intersect(points2, points1)
|
|
42
|
+
else: manifold = sutherland_hodgman(points1, points2)
|
|
43
|
+
|
|
44
|
+
# fall back if manifold fails to develope
|
|
45
|
+
if len(manifold) == 0: return []
|
|
46
|
+
|
|
47
|
+
# convert inertsection algorithm output to 3d
|
|
48
|
+
return points_to_3d(u1, v1, contact_plane_point, manifold)
|
|
49
|
+
|
|
50
|
+
def separate_polytope(points1: list[ContactPoint], points2: list[ContactPoint], contact_plane_normal, epsilon: float=1e-5) -> tuple[list[ContactPoint], list[ContactPoint]]:
|
|
51
|
+
"""
|
|
52
|
+
Determines the potential contact manifold points of each shape based on their position along the penetrating axis
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
proj1 = [(glm.dot(point.vertex, contact_plane_normal), point) for point in points1]
|
|
56
|
+
proj2 = [(glm.dot(point.vertex, contact_plane_normal), point) for point in points2]
|
|
57
|
+
|
|
58
|
+
# min1 and max2 should be past the collising points of node2 and node1 respectively
|
|
59
|
+
min1 = min(proj1, key=lambda proj: proj[0])[0]
|
|
60
|
+
max2 = max(proj2, key=lambda proj: proj[0])[0]
|
|
61
|
+
|
|
62
|
+
proj1 = filter(lambda proj: proj[0] <= max2 + epsilon, proj1)
|
|
63
|
+
proj2 = filter(lambda proj: proj[0] + epsilon >= min1, proj2)
|
|
64
|
+
|
|
65
|
+
return [point[1] for point in proj1], [point[1] for point in proj2]
|
|
66
|
+
|
|
67
|
+
def distance_to_plane(contact_plane_point:glm.vec3, contact_plane_normal:glm.vec3, point:glm.vec3) -> float:
|
|
68
|
+
"""gets the smallest distance a point is from a plane"""
|
|
69
|
+
return glm.dot(point - contact_plane_point, contact_plane_normal) #TODO check this formula
|
|
70
|
+
|
|
71
|
+
def project_points(contact_plane_point:glm.vec3, contact_plane_normal:glm.vec3, points:list[Vec3]) -> list[glm.vec3]:
|
|
72
|
+
"""gets the projected positions of the given points onto the given plane"""
|
|
73
|
+
return [point - glm.dot(point - contact_plane_point, contact_plane_normal) * contact_plane_normal for point in points]
|
|
74
|
+
|
|
75
|
+
def points_to_2d(contact_plane_point:glm.vec3, contact_plane_normal:glm.vec3, points:list[glm.vec3], u = None, v = None) -> tuple[list[glm.vec2], glm.vec3, glm.vec3]:
|
|
76
|
+
"""converts a list of points on a plane to their 2d representation"""
|
|
77
|
+
# generate a new basis
|
|
78
|
+
k = get_noncolinear_vector(contact_plane_normal)
|
|
79
|
+
u = u if u else glm.normalize(glm.cross(contact_plane_normal, k))
|
|
80
|
+
v = v if v else glm.cross(contact_plane_normal, u)
|
|
81
|
+
|
|
82
|
+
# convert points to new basis
|
|
83
|
+
return [glm.vec2(glm.dot(vec := point - contact_plane_point, u), glm.dot(vec, v)) for point in points], u, v
|
|
84
|
+
|
|
85
|
+
def points_to_3d(u:glm.vec3, v:glm.vec3, contact_plane_point:glm.vec3, points:list[glm.vec2]) -> list[glm.vec3]:
|
|
86
|
+
"""converts a list of points on a plane to their 3d representation"""
|
|
87
|
+
return [contact_plane_point + point.x * u + point.y * v for point in points]
|
|
88
|
+
|
|
89
|
+
# vector math
|
|
90
|
+
def get_noncolinear_vector(vector:glm.vec3) -> glm.vec3:
|
|
91
|
+
"""generates a non colinear vector based on the given vector"""
|
|
92
|
+
test_vector = (1, 1, 1)
|
|
93
|
+
while glm.cross(test_vector, vector) == (0, 0, 0):
|
|
94
|
+
val = randint(0, 7) # 000 to 111
|
|
95
|
+
test_vector = (val & 1, val & 2, val & 4) # one random for three digits
|
|
96
96
|
return test_vector
|
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
import glm
|
|
2
|
-
from dataclasses import dataclass
|
|
3
|
-
|
|
4
|
-
from basilisk.generic.vec3 import Vec3
|
|
5
|
-
# from ...nodes.node import Node
|
|
6
|
-
|
|
7
|
-
# frozen because data does not need to be mutable
|
|
8
|
-
# used in creating polytopes for GJK/EPA
|
|
9
|
-
@dataclass(frozen=True)
|
|
10
|
-
class SupportPoint():
|
|
11
|
-
support_point: glm.vec3
|
|
12
|
-
|
|
13
|
-
index1: int # index of the vertex in the mesh
|
|
14
|
-
vertex1: glm.vec3 # world space location of the vertex at collision
|
|
15
|
-
|
|
16
|
-
index2: int
|
|
17
|
-
vertex2: glm.vec3
|
|
18
|
-
|
|
19
|
-
# used for generating contact points for the contact manifold
|
|
20
|
-
@dataclass(frozen=True)
|
|
21
|
-
class ContactPoint():
|
|
22
|
-
index: int
|
|
23
|
-
vertex: Vec3
|
|
24
|
-
|
|
25
|
-
# contact manifold object used in the contact handler list
|
|
26
|
-
@dataclass
|
|
27
|
-
class ContactManifold():
|
|
28
|
-
normal: glm.vec3
|
|
29
|
-
contact_points1: dict[int : glm.vec3] # contact point index : collision position
|
|
30
|
-
contact_points2: dict[int : glm.vec3]
|
|
31
|
-
|
|
32
|
-
@dataclass
|
|
33
|
-
class Collision():
|
|
34
|
-
node: ...
|
|
1
|
+
import glm
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
|
|
4
|
+
from basilisk.generic.vec3 import Vec3
|
|
5
|
+
# from ...nodes.node import Node
|
|
6
|
+
|
|
7
|
+
# frozen because data does not need to be mutable
|
|
8
|
+
# used in creating polytopes for GJK/EPA
|
|
9
|
+
@dataclass(frozen=True)
|
|
10
|
+
class SupportPoint():
|
|
11
|
+
support_point: glm.vec3
|
|
12
|
+
|
|
13
|
+
index1: int # index of the vertex in the mesh
|
|
14
|
+
vertex1: glm.vec3 # world space location of the vertex at collision
|
|
15
|
+
|
|
16
|
+
index2: int
|
|
17
|
+
vertex2: glm.vec3
|
|
18
|
+
|
|
19
|
+
# used for generating contact points for the contact manifold
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class ContactPoint():
|
|
22
|
+
index: int
|
|
23
|
+
vertex: Vec3
|
|
24
|
+
|
|
25
|
+
# contact manifold object used in the contact handler list
|
|
26
|
+
@dataclass
|
|
27
|
+
class ContactManifold():
|
|
28
|
+
normal: glm.vec3
|
|
29
|
+
contact_points1: dict[int : glm.vec3] # contact point index : collision position
|
|
30
|
+
contact_points2: dict[int : glm.vec3]
|
|
31
|
+
|
|
32
|
+
@dataclass
|
|
33
|
+
class Collision():
|
|
34
|
+
node: ...
|
|
35
35
|
normal: glm.vec3
|
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
# def sat_manifold(self, points1: list[glm.vec3], points2: list[glm.vec3], axis: glm.vec3, plane_point: glm.vec3, digit: int) -> list[glm.vec3]:
|
|
2
|
-
# """
|
|
3
|
-
# Returns the contact manifold from an SAT OBB OBB collision
|
|
4
|
-
# """
|
|
5
|
-
# def get_test_points(contact_plane_normal:glm.vec3, points:list[glm.vec3], count: int):
|
|
6
|
-
# test_points = [(glm.dot(contact_plane_normal, p), p) for p in points]
|
|
7
|
-
# test_points.sort(key=lambda p: p[0])
|
|
8
|
-
# return [p[1] for p in test_points[:count]]
|
|
9
|
-
|
|
10
|
-
# def get_test_points_unknown(contact_plane_normal:glm.vec3, points:list[glm.vec3]):
|
|
11
|
-
# test_points = [(glm.dot(contact_plane_normal, p), p) for p in points]
|
|
12
|
-
# test_points.sort(key=lambda p: p[0])
|
|
13
|
-
# if test_points[2][0] - test_points[0][0] > 1e-3: return [p[1] for p in test_points[:2]]
|
|
14
|
-
# else: return [p[1] for p in test_points[:4]]
|
|
15
|
-
|
|
16
|
-
# if digit < 6: # there must be at least one face in the collision
|
|
17
|
-
# reference, incident = (get_test_points(-axis, points1, 4), get_test_points_unknown(axis, points2)) if digit < 3 else (get_test_points(axis, points2, 4), get_test_points_unknown(-axis, points1))
|
|
18
|
-
|
|
19
|
-
# # project vertices onto the 2d plane
|
|
20
|
-
# reference = project_points(plane_point, axis, reference)
|
|
21
|
-
# incident = project_points(plane_point, axis, incident)
|
|
22
|
-
|
|
23
|
-
# # convert points to 2d for intersection algorithms
|
|
24
|
-
# reference, u1, v1 = points_to_2d(plane_point, axis, reference)
|
|
25
|
-
# incident, u2, v2 = points_to_2d(plane_point, axis, incident, u1, v1)
|
|
26
|
-
|
|
27
|
-
# # convert arbitrary points to polygon
|
|
28
|
-
# reference = graham_scan(reference)
|
|
29
|
-
# if len(incident) == 4: incident = graham_scan(incident)
|
|
30
|
-
|
|
31
|
-
# # run clipping algorithms
|
|
32
|
-
# manifold = []
|
|
33
|
-
# if len(incident) == 2: manifold = line_poly_intersect(incident, reference)
|
|
34
|
-
# else: manifold = sutherland_hodgman(reference, incident)
|
|
35
|
-
|
|
36
|
-
# # # fall back if manifold fails to develope
|
|
37
|
-
# assert len(manifold), 'sat did not generate points'
|
|
38
|
-
|
|
39
|
-
# # # convert inertsection algorithm output to 3d
|
|
40
|
-
# return points_to_3d(u1, v1, plane_point, manifold)
|
|
41
|
-
|
|
42
|
-
# else: # there is an edge edge collision
|
|
43
|
-
|
|
44
|
-
# points1 = get_test_points(-axis, points1, 2)
|
|
45
|
-
# points2 = get_test_points(axis, points2, 2)
|
|
46
|
-
|
|
1
|
+
# def sat_manifold(self, points1: list[glm.vec3], points2: list[glm.vec3], axis: glm.vec3, plane_point: glm.vec3, digit: int) -> list[glm.vec3]:
|
|
2
|
+
# """
|
|
3
|
+
# Returns the contact manifold from an SAT OBB OBB collision
|
|
4
|
+
# """
|
|
5
|
+
# def get_test_points(contact_plane_normal:glm.vec3, points:list[glm.vec3], count: int):
|
|
6
|
+
# test_points = [(glm.dot(contact_plane_normal, p), p) for p in points]
|
|
7
|
+
# test_points.sort(key=lambda p: p[0])
|
|
8
|
+
# return [p[1] for p in test_points[:count]]
|
|
9
|
+
|
|
10
|
+
# def get_test_points_unknown(contact_plane_normal:glm.vec3, points:list[glm.vec3]):
|
|
11
|
+
# test_points = [(glm.dot(contact_plane_normal, p), p) for p in points]
|
|
12
|
+
# test_points.sort(key=lambda p: p[0])
|
|
13
|
+
# if test_points[2][0] - test_points[0][0] > 1e-3: return [p[1] for p in test_points[:2]]
|
|
14
|
+
# else: return [p[1] for p in test_points[:4]]
|
|
15
|
+
|
|
16
|
+
# if digit < 6: # there must be at least one face in the collision
|
|
17
|
+
# reference, incident = (get_test_points(-axis, points1, 4), get_test_points_unknown(axis, points2)) if digit < 3 else (get_test_points(axis, points2, 4), get_test_points_unknown(-axis, points1))
|
|
18
|
+
|
|
19
|
+
# # project vertices onto the 2d plane
|
|
20
|
+
# reference = project_points(plane_point, axis, reference)
|
|
21
|
+
# incident = project_points(plane_point, axis, incident)
|
|
22
|
+
|
|
23
|
+
# # convert points to 2d for intersection algorithms
|
|
24
|
+
# reference, u1, v1 = points_to_2d(plane_point, axis, reference)
|
|
25
|
+
# incident, u2, v2 = points_to_2d(plane_point, axis, incident, u1, v1)
|
|
26
|
+
|
|
27
|
+
# # convert arbitrary points to polygon
|
|
28
|
+
# reference = graham_scan(reference)
|
|
29
|
+
# if len(incident) == 4: incident = graham_scan(incident)
|
|
30
|
+
|
|
31
|
+
# # run clipping algorithms
|
|
32
|
+
# manifold = []
|
|
33
|
+
# if len(incident) == 2: manifold = line_poly_intersect(incident, reference)
|
|
34
|
+
# else: manifold = sutherland_hodgman(reference, incident)
|
|
35
|
+
|
|
36
|
+
# # # fall back if manifold fails to develope
|
|
37
|
+
# assert len(manifold), 'sat did not generate points'
|
|
38
|
+
|
|
39
|
+
# # # convert inertsection algorithm output to 3d
|
|
40
|
+
# return points_to_3d(u1, v1, plane_point, manifold)
|
|
41
|
+
|
|
42
|
+
# else: # there is an edge edge collision
|
|
43
|
+
|
|
44
|
+
# points1 = get_test_points(-axis, points1, 2)
|
|
45
|
+
# points2 = get_test_points(axis, points2, 2)
|
|
46
|
+
|
|
47
47
|
# return closest_two_lines(*points1, *points2)
|
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
import glm
|
|
2
|
-
from .helper import get_support_point
|
|
3
|
-
from .dataclasses import SupportPoint
|
|
4
|
-
from...nodes.node import Node
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
# TODO change these to structs when converting to C++
|
|
8
|
-
face_type = list[tuple[float, glm.vec3, glm.vec3, int, int, int]] # distance, normal, center, index 1, index 2, index 3
|
|
9
|
-
polytope_type = list[SupportPoint] # polytope vertex, node1 vertex, node2 vertex
|
|
10
|
-
|
|
11
|
-
def get_epa_from_gjk(node1: Node, node2: Node, polytope: polytope_type, epsilon: float=0) -> tuple[face_type, polytope_type]: # TODO determine the return type of get_epa_from_gjk and if epsilon is good value
|
|
12
|
-
"""
|
|
13
|
-
Determines the peneration vector from a collision using EPA. The returned face normal is normalized but the rest are not guarunteed to be.
|
|
14
|
-
"""
|
|
15
|
-
# orient faces to point normals counter clockwise
|
|
16
|
-
faces: face_type = []
|
|
17
|
-
for indices in [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]: faces = insert_face(polytope, faces, indices)
|
|
18
|
-
|
|
19
|
-
# develope the polytope until the nearest real face has been found, within epsilon
|
|
20
|
-
while True:
|
|
21
|
-
new_point = get_support_point(node1, node2, faces[0][1])
|
|
22
|
-
if new_point in polytope or glm.length(new_point.support_point) - faces[0][0] < epsilon: return faces[0], polytope
|
|
23
|
-
faces, polytope = insert_point(polytope, faces, new_point)
|
|
24
|
-
|
|
25
|
-
def insert_point(polytope: polytope_type, faces: face_type, point: glm.vec3, epsilon: float=0) -> tuple[face_type, polytope_type]:
|
|
26
|
-
"""
|
|
27
|
-
Inserts a point into the polytope sorting by distance from the origin
|
|
28
|
-
"""
|
|
29
|
-
# determine which faces are facing the new point
|
|
30
|
-
polytope.append(point)
|
|
31
|
-
support_index = len(polytope) - 1
|
|
32
|
-
visible_faces = [
|
|
33
|
-
face for face in faces
|
|
34
|
-
if glm.dot(face[1], polytope[support_index].support_point) >= epsilon and # if the normal of a face is pointing towards the added point
|
|
35
|
-
glm.dot(face[1], polytope[support_index].support_point - face[2]) >= epsilon # TODO check if this ever occurs
|
|
36
|
-
]
|
|
37
|
-
|
|
38
|
-
# generate new edges
|
|
39
|
-
edges = []
|
|
40
|
-
for face in visible_faces:
|
|
41
|
-
for p1, p2 in get_face_edges(face):
|
|
42
|
-
if (p2, p1) in edges: edges.remove((p2, p1)) # edges can only be shared by two faces, running opposite to each other.
|
|
43
|
-
elif (p1, p2) in edges: # TODO remove this
|
|
44
|
-
edges.remove((p1, p2))
|
|
45
|
-
# print('not reversed')
|
|
46
|
-
else: edges.append((p1, p2))
|
|
47
|
-
|
|
48
|
-
# remove visible faces
|
|
49
|
-
for face in sorted(visible_faces, reverse = True): faces.remove(face)
|
|
50
|
-
|
|
51
|
-
# add new faces
|
|
52
|
-
new_face_indices = [orient_face(polytope, (edge[0], edge[1], support_index)) for edge in edges] # edge[0], edge[1] is already ccw
|
|
53
|
-
for indices in new_face_indices: faces = insert_face(polytope, faces, indices)
|
|
54
|
-
|
|
55
|
-
return faces, polytope
|
|
56
|
-
|
|
57
|
-
def insert_face(polytope: polytope_type, faces: face_type, indices: tuple[int, int, int]) -> face_type:
|
|
58
|
-
"""
|
|
59
|
-
Inserts a face into the face priority queue based on the indices given in the polytope
|
|
60
|
-
"""
|
|
61
|
-
center = (polytope[indices[0]].support_point + polytope[indices[1]].support_point + polytope[indices[2]].support_point) / 3
|
|
62
|
-
normal = glm.cross(polytope[indices[1]].support_point - polytope[indices[0]].support_point, polytope[indices[2]].support_point - polytope[indices[0]].support_point) # closest face normal will be normalized once returned to avoid square roots and division
|
|
63
|
-
if glm.dot(center, normal) < 0:
|
|
64
|
-
normal *= -1
|
|
65
|
-
indices = (indices[2], indices[1], indices[0])
|
|
66
|
-
|
|
67
|
-
# TODO solve cases where face may contain origin
|
|
68
|
-
normal = glm.normalize(normal)
|
|
69
|
-
distance = abs(glm.dot(polytope[indices[0]].support_point, normal))
|
|
70
|
-
new_face = (distance, normal, center, *indices)
|
|
71
|
-
|
|
72
|
-
# insert faces into priority queue based on distance from origin
|
|
73
|
-
for i, face in enumerate(faces):
|
|
74
|
-
if face[0] < distance: continue
|
|
75
|
-
faces.insert(i, new_face)
|
|
76
|
-
break
|
|
77
|
-
else: faces.append(new_face)
|
|
78
|
-
|
|
79
|
-
return faces
|
|
80
|
-
|
|
81
|
-
def orient_face(polytope: polytope_type, indices: tuple[int, int, int]) -> tuple[int, int, int]:
|
|
82
|
-
"""
|
|
83
|
-
Orients the face indices to have a counter clockwise normal
|
|
84
|
-
"""
|
|
85
|
-
if glm.dot(glm.cross(polytope[indices[1]].support_point, polytope[indices[2]].support_point), polytope[indices[0]].support_point) < 0: return (indices[2], indices[1], indices[0])
|
|
86
|
-
return indices
|
|
87
|
-
|
|
88
|
-
def get_face_edges(face: tuple[float, glm.vec3, glm.vec3, int, int, int]) -> list[tuple[int, int]]:
|
|
89
|
-
"""
|
|
90
|
-
Permutes a tuple of three unique numbers (a, b, c) into 3 pairs (x, y), preserving order
|
|
91
|
-
"""
|
|
1
|
+
import glm
|
|
2
|
+
from .helper import get_support_point
|
|
3
|
+
from .dataclasses import SupportPoint
|
|
4
|
+
from...nodes.node import Node
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# TODO change these to structs when converting to C++
|
|
8
|
+
face_type = list[tuple[float, glm.vec3, glm.vec3, int, int, int]] # distance, normal, center, index 1, index 2, index 3
|
|
9
|
+
polytope_type = list[SupportPoint] # polytope vertex, node1 vertex, node2 vertex
|
|
10
|
+
|
|
11
|
+
def get_epa_from_gjk(node1: Node, node2: Node, polytope: polytope_type, epsilon: float=0) -> tuple[face_type, polytope_type]: # TODO determine the return type of get_epa_from_gjk and if epsilon is good value
|
|
12
|
+
"""
|
|
13
|
+
Determines the peneration vector from a collision using EPA. The returned face normal is normalized but the rest are not guarunteed to be.
|
|
14
|
+
"""
|
|
15
|
+
# orient faces to point normals counter clockwise
|
|
16
|
+
faces: face_type = []
|
|
17
|
+
for indices in [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]: faces = insert_face(polytope, faces, indices)
|
|
18
|
+
|
|
19
|
+
# develope the polytope until the nearest real face has been found, within epsilon
|
|
20
|
+
while True:
|
|
21
|
+
new_point = get_support_point(node1, node2, faces[0][1])
|
|
22
|
+
if new_point in polytope or glm.length(new_point.support_point) - faces[0][0] < epsilon: return faces[0], polytope
|
|
23
|
+
faces, polytope = insert_point(polytope, faces, new_point)
|
|
24
|
+
|
|
25
|
+
def insert_point(polytope: polytope_type, faces: face_type, point: glm.vec3, epsilon: float=0) -> tuple[face_type, polytope_type]:
|
|
26
|
+
"""
|
|
27
|
+
Inserts a point into the polytope sorting by distance from the origin
|
|
28
|
+
"""
|
|
29
|
+
# determine which faces are facing the new point
|
|
30
|
+
polytope.append(point)
|
|
31
|
+
support_index = len(polytope) - 1
|
|
32
|
+
visible_faces = [
|
|
33
|
+
face for face in faces
|
|
34
|
+
if glm.dot(face[1], polytope[support_index].support_point) >= epsilon and # if the normal of a face is pointing towards the added point
|
|
35
|
+
glm.dot(face[1], polytope[support_index].support_point - face[2]) >= epsilon # TODO check if this ever occurs
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
# generate new edges
|
|
39
|
+
edges = []
|
|
40
|
+
for face in visible_faces:
|
|
41
|
+
for p1, p2 in get_face_edges(face):
|
|
42
|
+
if (p2, p1) in edges: edges.remove((p2, p1)) # edges can only be shared by two faces, running opposite to each other.
|
|
43
|
+
elif (p1, p2) in edges: # TODO remove this
|
|
44
|
+
edges.remove((p1, p2))
|
|
45
|
+
# print('not reversed')
|
|
46
|
+
else: edges.append((p1, p2))
|
|
47
|
+
|
|
48
|
+
# remove visible faces
|
|
49
|
+
for face in sorted(visible_faces, reverse = True): faces.remove(face)
|
|
50
|
+
|
|
51
|
+
# add new faces
|
|
52
|
+
new_face_indices = [orient_face(polytope, (edge[0], edge[1], support_index)) for edge in edges] # edge[0], edge[1] is already ccw
|
|
53
|
+
for indices in new_face_indices: faces = insert_face(polytope, faces, indices)
|
|
54
|
+
|
|
55
|
+
return faces, polytope
|
|
56
|
+
|
|
57
|
+
def insert_face(polytope: polytope_type, faces: face_type, indices: tuple[int, int, int]) -> face_type:
|
|
58
|
+
"""
|
|
59
|
+
Inserts a face into the face priority queue based on the indices given in the polytope
|
|
60
|
+
"""
|
|
61
|
+
center = (polytope[indices[0]].support_point + polytope[indices[1]].support_point + polytope[indices[2]].support_point) / 3
|
|
62
|
+
normal = glm.cross(polytope[indices[1]].support_point - polytope[indices[0]].support_point, polytope[indices[2]].support_point - polytope[indices[0]].support_point) # closest face normal will be normalized once returned to avoid square roots and division
|
|
63
|
+
if glm.dot(center, normal) < 0:
|
|
64
|
+
normal *= -1
|
|
65
|
+
indices = (indices[2], indices[1], indices[0])
|
|
66
|
+
|
|
67
|
+
# TODO solve cases where face may contain origin
|
|
68
|
+
normal = glm.normalize(normal)
|
|
69
|
+
distance = abs(glm.dot(polytope[indices[0]].support_point, normal))
|
|
70
|
+
new_face = (distance, normal, center, *indices)
|
|
71
|
+
|
|
72
|
+
# insert faces into priority queue based on distance from origin
|
|
73
|
+
for i, face in enumerate(faces):
|
|
74
|
+
if face[0] < distance: continue
|
|
75
|
+
faces.insert(i, new_face)
|
|
76
|
+
break
|
|
77
|
+
else: faces.append(new_face)
|
|
78
|
+
|
|
79
|
+
return faces
|
|
80
|
+
|
|
81
|
+
def orient_face(polytope: polytope_type, indices: tuple[int, int, int]) -> tuple[int, int, int]:
|
|
82
|
+
"""
|
|
83
|
+
Orients the face indices to have a counter clockwise normal
|
|
84
|
+
"""
|
|
85
|
+
if glm.dot(glm.cross(polytope[indices[1]].support_point, polytope[indices[2]].support_point), polytope[indices[0]].support_point) < 0: return (indices[2], indices[1], indices[0])
|
|
86
|
+
return indices
|
|
87
|
+
|
|
88
|
+
def get_face_edges(face: tuple[float, glm.vec3, glm.vec3, int, int, int]) -> list[tuple[int, int]]:
|
|
89
|
+
"""
|
|
90
|
+
Permutes a tuple of three unique numbers (a, b, c) into 3 pairs (x, y), preserving order
|
|
91
|
+
"""
|
|
92
92
|
return [(face[3], face[4]), (face[4], face[5]), (face[5], face[3])]
|