gamengine3d 1.6.0__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.
@@ -0,0 +1,94 @@
1
+ from gamengine3d import vector3d, Context, Cuboid
2
+ import math
3
+
4
+
5
+ class PlayerMovement: # Class name must match file name in PascalCase
6
+ def __init__(self, obj: Cuboid, context: Context):
7
+ self.obj = obj
8
+ self.context = context
9
+
10
+ self.speed = 1 # units per second
11
+ self.rotate_speed = 100 # degrees per second
12
+ self.gravity = vector3d(0, -4.8, 0)
13
+ self.velocity = vector3d.zero
14
+
15
+ # Movement bindings
16
+ self.context.on_key_held("w", callback=self.handle_controls, dt=True, args=["forward"])
17
+ self.context.on_key_held("s", callback=self.handle_controls, dt=True, args=["backward"])
18
+ self.context.on_key_held("a", callback=self.handle_controls, dt=True, args=["left"])
19
+ self.context.on_key_held("d", callback=self.handle_controls, dt=True, args=["right"])
20
+ self.context.on_key_held("space", callback=self.handle_controls, dt=True, args=["up"])
21
+
22
+ self.obj.rotation = vector3d(0) # start facing forward
23
+
24
+
25
+ def update(self, dt):
26
+ # Gravity runs every frame
27
+ self.handle_gravity(dt)
28
+
29
+
30
+ def handle_controls(self, dt, direction):
31
+ forward = get_forward(self.obj.rotation) # forward relative to facing direction
32
+
33
+ if direction == "forward":
34
+ delta_pos = forward * self.speed * dt
35
+ self.obj.pos += delta_pos
36
+
37
+ # undo if hitting walls
38
+ if any([
39
+ self.context.functions.is_colliding(self.obj.name, "Left"),
40
+ self.context.functions.is_colliding(self.obj.name, "Right"),
41
+ self.context.functions.is_colliding(self.obj.name, "Back"),
42
+ ]):
43
+ self.obj.pos -= delta_pos
44
+
45
+ elif direction == "backward":
46
+ delta_pos = forward * self.speed * dt
47
+ self.obj.pos -= delta_pos
48
+
49
+ # undo if hitting walls
50
+ if any([
51
+ self.context.functions.is_colliding(self.obj.name, "Left"),
52
+ self.context.functions.is_colliding(self.obj.name, "Right"),
53
+ self.context.functions.is_colliding(self.obj.name, "Back"),
54
+ ]):
55
+ self.obj.pos += delta_pos
56
+
57
+ elif direction == "left":
58
+ self.obj.rotation.z -= self.rotate_speed * dt # turn left
59
+
60
+ elif direction == "right":
61
+ self.obj.rotation.z += self.rotate_speed * dt # turn right
62
+
63
+ elif direction == "up":
64
+ # jump only if grounded
65
+ if self.context.functions.is_colliding(self.obj.name, "Floor"):
66
+ self.velocity.y = 3
67
+ self.obj.pos.y += 0.1 # small lift to ensure gravity applies next frame
68
+
69
+
70
+ def handle_gravity(self, dt):
71
+ grounded = self.context.functions.is_colliding(self.obj.name, "Floor")
72
+
73
+ if not grounded:
74
+ self.velocity += self.gravity * dt # accelerate downward
75
+ else:
76
+ floor = self.context.functions.get_game_object("Floor")
77
+ self.obj.pos.y = floor.pos.y + floor.size.y/2 + self.obj.size.y/2 # snap to surface
78
+ self.velocity = vector3d.zero
79
+
80
+ # check predicted collision next frame
81
+ if self.context.functions.is_colliding_pos("Floor", self.obj.pos + self.velocity * dt):
82
+ floor = self.context.functions.get_game_object("Floor")
83
+ self.obj.pos.y = floor.pos.y + floor.size.y/2 + self.obj.size.y/2
84
+ else:
85
+ self.obj.pos += self.velocity * dt # apply gravity motion
86
+
87
+
88
+ def get_forward(rotation):
89
+ # Converts yaw (z rotation) into a directional vector on the XZ plane
90
+ yaw = math.radians(rotation.z)
91
+ fx = -math.sin(yaw)
92
+ fy = 0
93
+ fz = math.cos(yaw)
94
+ return vector3d(fx, fy, fz)
@@ -0,0 +1,6 @@
1
+ from gamengine3d import Color, vector3d
2
+
3
+ cylinder_colors = [Color.light_red, Color.light_green, Color.light_blue, Color.light_yellow, Color.orange, Color.magenta]
4
+ num_cylinder_colors = len(cylinder_colors)
5
+
6
+ sphere_pos = vector3d(0, 1, 4)
@@ -0,0 +1,42 @@
1
+ from gamengine3d import *
2
+ import random
3
+ from pathlib import Path
4
+
5
+ engine = Engine(1000, 800, name="The Everything DEMO", background_color=Color.grey)
6
+ engine.context.runtime_vars.from_settings("gamengine3d.examples.scripts.settings") # set the runtime vars from a module
7
+
8
+ positions = [
9
+ vector3d(0, 4, 0.5),
10
+ vector3d(-3, 2, 2),
11
+ vector3d(3, 3, -2),
12
+ vector3d(-2, 5, -3),
13
+ vector3d(4, 6, 4),
14
+ vector3d(1, 6, 7),
15
+ vector3d(-4, 3, 5),
16
+ vector3d(5, 5, 9),
17
+ vector3d(0, 7, -4),
18
+ vector3d(-5, 4, 3),
19
+ vector3d(2, 8, 2),
20
+ vector3d(3, 5, -5),
21
+ vector3d(-3, 6, 6),
22
+ vector3d(6, 3, 3)
23
+ ]
24
+
25
+ for pos in positions:
26
+ intensity = random.uniform(0.4, .8)
27
+ engine.add_light(Light(position=pos, color=Color.white, intensity=intensity))
28
+
29
+ EXAMPLE_DIR = Path(__file__).parent
30
+ scripts_dir = EXAMPLE_DIR / "scripts"
31
+
32
+ cylinder = Cylinder(vector3d.zero, 2, .5, Color.light_green, 64, vector3d(40, 60, 100), "Cylinder")
33
+ cylinder.attach(scripts_dir/"cylinder.py", engine.context)
34
+ engine.add_object(cylinder)
35
+
36
+ # cuboid = Cuboid(vector3d(3, 2, 5), color=Color.orange, size=vector3d(2, 1, 3))
37
+ # engine.add_object(cuboid)
38
+ #
39
+ # sphere = Sphere(vector3d(0, 5, 2), color=Color.light_blue, radius=1, rings=32, segments=64)
40
+ # engine.add_object(sphere)
41
+
42
+ engine.run()
@@ -0,0 +1,185 @@
1
+ import numpy as np
2
+ from itertools import product
3
+ from pyrr import Matrix44
4
+ import math
5
+
6
+ from .helper import Color, vector3d, Renderer, Script
7
+
8
+ class GameObject:
9
+ def __init__(self, name):
10
+ self.name = name
11
+ self.scripts = []
12
+
13
+ def update(self, dt):
14
+ for script in self.scripts:
15
+ script.update(dt)
16
+
17
+ def draw(self, renderer: Renderer):
18
+ pass
19
+
20
+ def attach(self, file_path, context):
21
+ script = Script(self, file_path, context)
22
+ script.init_instance()
23
+ self.scripts.append(script)
24
+
25
+ def init_scripts(self):
26
+ for script in self.scripts:
27
+ script.init_instance()
28
+
29
+ class Cuboid(GameObject):
30
+ def __init__(self, pos: vector3d = vector3d.zero,
31
+ size: vector3d = vector3d(5),
32
+ color: Color = Color.green,
33
+ name: str = "Cuboid",
34
+ rotation: vector3d = vector3d.zero):
35
+ super().__init__(name)
36
+ self.name = name
37
+ self.pos = pos
38
+ self.size = size
39
+ self.color = color
40
+ self.rotation = rotation
41
+ self.visible = True
42
+
43
+ self.faces = [
44
+ [0, 1, 3, 2],
45
+ [4, 6, 7, 5],
46
+ [0, 4, 5, 1],
47
+ [2, 3, 7, 6],
48
+ [0, 2, 6, 4],
49
+ [1, 5, 7, 3],
50
+ ]
51
+
52
+ def get_rotation_matrix(self):
53
+ """Return 3x3 rotation matrix (without translation)."""
54
+ return Matrix44.from_eulers((
55
+ math.radians(self.rotation.x),
56
+ math.radians(self.rotation.y),
57
+ math.radians(self.rotation.z)
58
+ )).transpose()[:3, :3] # extract 3x3 rotation
59
+
60
+ def get_corners(self):
61
+ """Return the 8 corners of the cuboid with rotation applied."""
62
+ corners = []
63
+ half_size = self.size / 2
64
+ rot = self.get_rotation_matrix()
65
+
66
+ for dx, dy, dz in product([-1, 1], repeat=3):
67
+ local = np.array([dx * half_size.x, dy * half_size.y, dz * half_size.z])
68
+ rotated = rot @ local
69
+ world = self.pos + vector3d(*rotated)
70
+ corners.append(world)
71
+
72
+ return corners
73
+
74
+ def is_point_in(self, point: vector3d) -> bool:
75
+ """
76
+ Checks if a point is inside the cuboid, accounting for full 3D rotation.
77
+ """
78
+ # Convert to numpy for math
79
+ p = np.array([point.x, point.y, point.z])
80
+ center = np.array([self.pos.x, self.pos.y, self.pos.z])
81
+ half = np.array([self.size.x / 2, self.size.y / 2, self.size.z / 2])
82
+
83
+ # Get inverse rotation matrix (undo rotation)
84
+ rot = self.get_rotation_matrix()
85
+ inv_rot = rot.T # inverse of rotation matrix is its transpose
86
+
87
+ # Transform point into cuboid's local coordinate system
88
+ local_point = inv_rot @ (p - center)
89
+
90
+ # Inside check in local space
91
+ return np.all(np.abs(local_point) <= half + 1e-6)
92
+
93
+ def draw(self, renderer: Renderer):
94
+ if not self.visible:
95
+ return
96
+ corners = self.get_corners()
97
+ for face_idx in self.faces:
98
+ renderer.render_quad(
99
+ corners[face_idx[0]],
100
+ corners[face_idx[1]],
101
+ corners[face_idx[2]],
102
+ corners[face_idx[3]],
103
+ color=self.color
104
+ )
105
+
106
+ class Sphere(GameObject):
107
+ def __init__(self, pos: vector3d = vector3d.zero,
108
+ radius: float = 5,
109
+ color: Color = Color.green,
110
+ segments: int = 32,
111
+ rings: int = 16,
112
+ name: str = "Sphere"):
113
+ super().__init__(name)
114
+ self.name = name
115
+ self.pos = pos
116
+ self.radius = radius
117
+ self.color = color
118
+ self.segments = segments
119
+ self.rings = rings
120
+ self.visible = True
121
+
122
+ def is_point_in(self, point: vector3d) -> bool:
123
+ dx = point.x - self.pos.x
124
+ dy = point.y - self.pos.y
125
+ dz = point.z - self.pos.z
126
+ distance_squared = dx*dx + dy*dy + dz*dz
127
+ return distance_squared <= self.radius * self.radius
128
+
129
+ def draw(self, renderer: Renderer):
130
+ if not self.visible:
131
+ return
132
+ renderer.render_sphere(self.pos, self.radius, self.color, self.segments, self.rings)
133
+
134
+ class Cylinder(GameObject):
135
+ def __init__(self, pos: vector3d = vector3d.zero,
136
+ length: float = 2,
137
+ radius: float = 0.5,
138
+ color: Color = Color.light_yellow,
139
+ segments: int = 32,
140
+ rotation: vector3d = vector3d.zero,
141
+ name: str = "Cylinder"):
142
+ super().__init__(name)
143
+ self.name = name
144
+ self.pos = pos
145
+ self.length = length
146
+ self.radius = radius
147
+ self.color = color
148
+ self.segments = segments
149
+ self.rotation = rotation # Euler angles in degrees
150
+ self.visible = True
151
+
152
+ def get_rotation_matrix(self):
153
+ rx, ry, rz = math.radians(self.rotation.x), math.radians(self.rotation.y), math.radians(self.rotation.z)
154
+ cx, sx = math.cos(rx), math.sin(rx)
155
+ cy, sy = math.cos(ry), math.sin(ry)
156
+ cz, sz = math.cos(rz), math.sin(rz)
157
+ # Rotation matrices
158
+ Rx = np.array([[1,0,0],[0,cx,-sx],[0,sx,cx]])
159
+ Ry = np.array([[cy,0,sy],[0,1,0],[-sy,0,cy]])
160
+ Rz = np.array([[cz,-sz,0],[sz,cz,0],[0,0,1]])
161
+ return Rz @ Ry @ Rx # apply in XYZ order
162
+
163
+ def is_point_in(self, point: vector3d, eps=1e-9) -> bool:
164
+ p = np.array([point.x, point.y, point.z])
165
+ center = np.array([self.pos.x, self.pos.y, self.pos.z])
166
+
167
+ # Transform point into cylinder local space
168
+ R = self.get_rotation_matrix()
169
+ invR = R.T # inverse rotation
170
+ local = invR @ (p - center)
171
+
172
+ # Check axial bounds (Y axis)
173
+ half_length = self.length / 2
174
+ if not (-half_length - eps <= local[1] <= half_length + eps):
175
+ return False
176
+
177
+ # Check radial distance in XZ plane
178
+ radial_dist_sq = local[0]**2 + local[2]**2
179
+ return radial_dist_sq <= (self.radius + eps)**2
180
+
181
+ def draw(self, renderer: Renderer):
182
+ if not self.visible:
183
+ return
184
+ renderer.render_cylinder(center=self.pos, height=self.length, radius=self.radius,
185
+ color=self.color, segments=self.segments, rotation=self.rotation)