gamengine3d 1.17.3__tar.gz → 1.18.0__tar.gz
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.
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/PKG-INFO +1 -1
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/__init__.py +1 -1
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/context.py +2 -1
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/engine.py +2 -2
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/player_movement.py +11 -9
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/scripts/player_movement.py +12 -8
- gamengine3d-1.18.0/gamengine3d/examples/scripts/solar_system.py +39 -0
- gamengine3d-1.18.0/gamengine3d/examples/solar_system.py +10 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/game_objects.py +2 -1
- gamengine3d-1.18.0/gamengine3d/helper.py +423 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/renderer.py +105 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d.egg-info/PKG-INFO +1 -1
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d.egg-info/SOURCES.txt +2 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/pyproject.toml +1 -1
- gamengine3d-1.17.3/gamengine3d/helper.py +0 -758
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/LICENSE.txt +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/README.md +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/cli.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/__init__.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/basics.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/from_obj.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/load_scene.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/messages_demo.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/objects/__init__.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/objects/icosahedron.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/save_scene.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/scripts/cylinder.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/scripts/message_receiver.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/scripts/messanger_script.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/scripts/settings.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/examples/the_everything_demo.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/obj_reader.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/0.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/1.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/2.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/3.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/4.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/5.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/6.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/7.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/8.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/9.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/A_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/B_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/C_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/D_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/E_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/F_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/G_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/H_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/I_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/J_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/K_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/L_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/M_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/N_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/O_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/P_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/Q_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/R_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/S_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/T_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/U_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/V_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/W_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/X_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/Y_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/Z_capital.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/__init__.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/a.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/ampersand.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/asterisk.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/at.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/b.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/backslash.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/brace_close.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/brace_open.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/bracket_close.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/bracket_open.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/c.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/caret.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/colon.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/comma.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/d.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/dash.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/dollar.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/dot.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/doublequote.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/e.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/equals.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/exclamation.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/f.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/g.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/greater.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/h.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/hash.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/i.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/j.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/k.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/l.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/less.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/m.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/n.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/o.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/p.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/paren_close.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/paren_open.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/percent.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/plus.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/q.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/question.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/r.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/s.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/semicolon.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/singlequote.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/slash.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/t.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/u.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/underscore.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/v.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/w.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/x.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/y.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/objects/z.obj +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/shaders/__init__.py +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/shaders/default.frag +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/shaders/default.vert +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/shaders/depth.frag +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d/shaders/depth.vert +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d.egg-info/dependency_links.txt +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d.egg-info/entry_points.txt +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d.egg-info/requires.txt +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/gamengine3d.egg-info/top_level.txt +0 -0
- {gamengine3d-1.17.3 → gamengine3d-1.18.0}/setup.cfg +0 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from .game_objects import GameObject
|
|
2
|
-
from .helper import Color, vector3d, Light,
|
|
2
|
+
from .helper import Color, vector3d, Light, EngineError
|
|
3
3
|
from pygame.time import Clock
|
|
4
|
+
from .renderer import Renderer, Camera
|
|
4
5
|
import time
|
|
5
6
|
import importlib
|
|
6
7
|
import warnings
|
|
@@ -4,8 +4,8 @@ from pyrr import Matrix44
|
|
|
4
4
|
from .context import Context
|
|
5
5
|
import pygame
|
|
6
6
|
from .game_objects import Cuboid, GameObject, Sphere, Cylinder, ObjModel, Text
|
|
7
|
-
from .helper import vector3d, Color,
|
|
8
|
-
from .renderer import Renderer
|
|
7
|
+
from .helper import vector3d, Color, Light, EngineError, Script
|
|
8
|
+
from .renderer import Renderer, Camera
|
|
9
9
|
import math
|
|
10
10
|
|
|
11
11
|
class Engine:
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from pathlib import Path
|
|
2
|
-
from gamengine3d import Engine, vector3d, Color, Cuboid, Light
|
|
2
|
+
from gamengine3d import Engine, vector3d, Color, Cuboid, Light, DirectionalLight
|
|
3
3
|
|
|
4
4
|
# Initialize engine
|
|
5
5
|
engine = Engine(
|
|
@@ -68,15 +68,17 @@ player.attach(player_script, engine.context) # attach movement script
|
|
|
68
68
|
engine.add_object(player)
|
|
69
69
|
|
|
70
70
|
# Lights
|
|
71
|
-
engine.add_light(
|
|
72
|
-
vector3d(1/3 * room_size.x, room_size.y, 1),
|
|
73
|
-
Color.white,
|
|
74
|
-
light_intensity
|
|
71
|
+
engine.add_light(DirectionalLight(
|
|
72
|
+
position=vector3d(1/3 * room_size.x, room_size.y, 1),
|
|
73
|
+
color=Color.white,
|
|
74
|
+
intensity=light_intensity,
|
|
75
|
+
rotation=vector3d.zero
|
|
75
76
|
))
|
|
76
|
-
engine.add_light(
|
|
77
|
-
vector3d(-1/3 * room_size.x, room_size.y, 1),
|
|
78
|
-
Color.white,
|
|
79
|
-
light_intensity
|
|
77
|
+
engine.add_light(DirectionalLight(
|
|
78
|
+
position=vector3d(-1/3 * room_size.x, room_size.y, 1),
|
|
79
|
+
color=Color.white,
|
|
80
|
+
intensity=light_intensity,
|
|
81
|
+
rotation=vector3d.zero
|
|
80
82
|
))
|
|
81
83
|
|
|
82
84
|
engine.run(dynamic_view=True)
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
from gamengine3d import vector3d, Context, Cuboid
|
|
2
|
-
import
|
|
3
|
-
|
|
2
|
+
from gamengine3d.helper import get_forward
|
|
4
3
|
|
|
5
4
|
class PlayerMovement: # Class name must match file name in PascalCase
|
|
6
5
|
def __init__(self, obj: Cuboid, context: Context):
|
|
@@ -11,6 +10,7 @@ class PlayerMovement: # Class name must match file name in PascalCase
|
|
|
11
10
|
self.rotate_speed = 100 # degrees per second
|
|
12
11
|
self.gravity = vector3d(0, -4.8, 0)
|
|
13
12
|
self.velocity = vector3d.zero
|
|
13
|
+
self.obj.visible = False
|
|
14
14
|
|
|
15
15
|
# Movement bindings
|
|
16
16
|
self.context.on_key_held("w", callback=self.handle_controls, dt=True, args=["forward"])
|
|
@@ -20,11 +20,13 @@ class PlayerMovement: # Class name must match file name in PascalCase
|
|
|
20
20
|
self.context.on_key_held("space", callback=self.handle_controls, dt=True, args=["up"])
|
|
21
21
|
|
|
22
22
|
self.obj.rotation = vector3d(0) # start facing forward
|
|
23
|
+
self.context.camera.set_orbiting(False)
|
|
23
24
|
|
|
24
25
|
|
|
25
26
|
def update(self, dt):
|
|
26
27
|
# Gravity runs every frame
|
|
27
28
|
self.handle_gravity(dt)
|
|
29
|
+
self.handle_camera()
|
|
28
30
|
|
|
29
31
|
|
|
30
32
|
def handle_controls(self, dt, direction):
|
|
@@ -85,9 +87,11 @@ class PlayerMovement: # Class name must match file name in PascalCase
|
|
|
85
87
|
self.obj.pos += self.velocity * dt # apply gravity motion
|
|
86
88
|
|
|
87
89
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
|
|
91
|
+
def handle_camera(self):
|
|
92
|
+
forward = get_forward(self.obj.rotation).normalized # ensure no zero vector issues
|
|
93
|
+
eye_height = self.obj.size.y * 0.9 # adjust for player height
|
|
94
|
+
cam_pos = self.obj.pos + vector3d(0, eye_height, 0) # camera at eye level
|
|
95
|
+
look_target = cam_pos + forward # look straight ahead
|
|
96
|
+
self.context.camera.move_to(cam_pos)
|
|
97
|
+
self.context.camera.look_at(look_target)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from gamengine3d import *
|
|
2
|
+
|
|
3
|
+
class Planet:
|
|
4
|
+
def __init__(self, context: Context, color, radius, pos, mass, name, initial_vel):
|
|
5
|
+
self.context = context
|
|
6
|
+
self.color = color
|
|
7
|
+
self.radius = radius
|
|
8
|
+
self.pos = pos
|
|
9
|
+
self.velocity = initial_vel
|
|
10
|
+
self.mass = mass
|
|
11
|
+
self.name = name
|
|
12
|
+
|
|
13
|
+
def draw(self):
|
|
14
|
+
self.context.functions.draw_sphere(pos=self.pos, radius=self.radius, color=self.color)
|
|
15
|
+
|
|
16
|
+
def update(self, dt, planets):
|
|
17
|
+
for planet in planets:
|
|
18
|
+
if planet.name != self.name:
|
|
19
|
+
sqr_dst = (self.pos - planet.pos).sqr_magnitude
|
|
20
|
+
force_dir = (planet.pos - self.pos).normalized
|
|
21
|
+
force = force_dir * planet.mass * self.mass / sqr_dst
|
|
22
|
+
acceleration = force / self.mass
|
|
23
|
+
self.velocity += acceleration * dt
|
|
24
|
+
|
|
25
|
+
self.pos += self.velocity * dt
|
|
26
|
+
|
|
27
|
+
class SolarSystem:
|
|
28
|
+
def __init__(self, obj: Engine, context: Context):
|
|
29
|
+
self.engine = obj
|
|
30
|
+
self.context = context
|
|
31
|
+
self.planets = [Planet(self.context, Color.light_yellow, 2, vector3d.zero, 50, "Sun", vector3d.zero),
|
|
32
|
+
Planet(self.context, Color.light_grey, .06, vector3d(-17, 0, 0), .07, "Moon", vector3d(0, 0, -3.5)),
|
|
33
|
+
Planet(self.context, Color.light_blue, .2, vector3d(-15, 0, 0), 5, "Earth", vector3d(0, 0, -2))]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def update(self, dt):
|
|
37
|
+
for planet in self.planets:
|
|
38
|
+
planet.update(dt, self.planets)
|
|
39
|
+
planet.draw()
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from gamengine3d import *
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
engine = Engine(1200, 800, name="Solar System Simulation", background_color=Color.black, ambient_light=.6)
|
|
5
|
+
|
|
6
|
+
EXAMPLES_DIR = Path(__file__).parent
|
|
7
|
+
|
|
8
|
+
engine.attach(EXAMPLES_DIR / "scripts" / "solar_system.py")
|
|
9
|
+
|
|
10
|
+
engine.run(60)
|
|
@@ -5,7 +5,8 @@ from itertools import product
|
|
|
5
5
|
from pyrr import Matrix44
|
|
6
6
|
import math
|
|
7
7
|
from .obj_reader import OBJReader
|
|
8
|
-
from .helper import Color, vector3d,
|
|
8
|
+
from .helper import Color, vector3d, Script, Message, EngineError
|
|
9
|
+
from .renderer import Renderer
|
|
9
10
|
from pathlib import Path
|
|
10
11
|
|
|
11
12
|
class GameObject:
|
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import importlib.util
|
|
3
|
+
import math
|
|
4
|
+
import moderngl
|
|
5
|
+
import pygame
|
|
6
|
+
from pygame.locals import DOUBLEBUF, OPENGL
|
|
7
|
+
import importlib.resources as pkg_resources
|
|
8
|
+
import numpy as np
|
|
9
|
+
from pyrr import Matrix44
|
|
10
|
+
import os
|
|
11
|
+
import time
|
|
12
|
+
|
|
13
|
+
class classproperty(property):
|
|
14
|
+
def __get__(self, cls, owner):
|
|
15
|
+
return self.fget(owner)
|
|
16
|
+
|
|
17
|
+
class vector3d:
|
|
18
|
+
def __init__(self, x, y=None, z=None):
|
|
19
|
+
self.x = x
|
|
20
|
+
self.y = y
|
|
21
|
+
self.z = z
|
|
22
|
+
|
|
23
|
+
if y is None and z is None:
|
|
24
|
+
self.y = self.x
|
|
25
|
+
self.z = self.x
|
|
26
|
+
elif z is None:
|
|
27
|
+
self.z = 0.0
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def magnitude(self):
|
|
31
|
+
return math.sqrt(self.x**2 + self.y**2 + self.z**2)
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def sqr_magnitude(self):
|
|
35
|
+
return self.x**2 + self.y**2 + self.z**2
|
|
36
|
+
|
|
37
|
+
def normalize(self):
|
|
38
|
+
magnitude = self.magnitude
|
|
39
|
+
if magnitude != 0:
|
|
40
|
+
self.x /= magnitude
|
|
41
|
+
self.y /= magnitude
|
|
42
|
+
self.z /= magnitude
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def normalized(self):
|
|
46
|
+
magnitude = self.magnitude
|
|
47
|
+
if magnitude == 0:
|
|
48
|
+
return vector3d(0, 0, 0)
|
|
49
|
+
return vector3d(self.x / magnitude, self.y / magnitude, self.z / magnitude)
|
|
50
|
+
|
|
51
|
+
def dot(self, other):
|
|
52
|
+
return self.x * other.x + self.y * other.y + self.z * other.z
|
|
53
|
+
|
|
54
|
+
def cross(self, other):
|
|
55
|
+
return vector3d(
|
|
56
|
+
self.y * other.z - self.z * other.y,
|
|
57
|
+
self.z * other.x - self.x * other.z,
|
|
58
|
+
self.x * other.y - self.y * other.x,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def __add__(self, other):
|
|
62
|
+
if isinstance(other, vector3d):
|
|
63
|
+
return vector3d(self.x + other.x, self.y + other.y, self.z + other.z)
|
|
64
|
+
else:
|
|
65
|
+
return vector3d(self.x + other, self.y + other, self.z + other)
|
|
66
|
+
|
|
67
|
+
def __sub__(self, other):
|
|
68
|
+
if isinstance(other, vector3d):
|
|
69
|
+
return vector3d(self.x - other.x, self.y - other.y, self.z - other.z)
|
|
70
|
+
else:
|
|
71
|
+
return vector3d(self.x - other, self.y - other, self.z - other)
|
|
72
|
+
|
|
73
|
+
def __mul__(self, other):
|
|
74
|
+
if isinstance(other, vector3d):
|
|
75
|
+
return vector3d(self.x * other.x, self.y * other.y, self.z * other.z)
|
|
76
|
+
else:
|
|
77
|
+
return vector3d(self.x * other, self.y * other, self.z * other)
|
|
78
|
+
|
|
79
|
+
def __truediv__(self, other):
|
|
80
|
+
if isinstance(other, vector3d):
|
|
81
|
+
return vector3d(self.x / other.x, self.y / other.y, self.z / other.z)
|
|
82
|
+
else:
|
|
83
|
+
return vector3d(self.x / other, self.y / other, self.z / other)
|
|
84
|
+
|
|
85
|
+
def totuple(self):
|
|
86
|
+
return (self.x, self.y, self.z)
|
|
87
|
+
|
|
88
|
+
def __radd__(self, other):
|
|
89
|
+
return vector3d(self.x + other, self.y + other, self.z + other)
|
|
90
|
+
|
|
91
|
+
def __rsub__(self, other):
|
|
92
|
+
return vector3d(self.x - other, self.y - other, self.z - other)
|
|
93
|
+
|
|
94
|
+
def __rtruediv__(self, other):
|
|
95
|
+
return vector3d(self.x / other, self.y / other, self.z / other)
|
|
96
|
+
|
|
97
|
+
def __rmul__(self, other):
|
|
98
|
+
return vector3d(self.x * other, self.y * other, self.z * other)
|
|
99
|
+
|
|
100
|
+
@classmethod
|
|
101
|
+
def fromtuple(cls, tuple):
|
|
102
|
+
return cls(*tuple)
|
|
103
|
+
|
|
104
|
+
def __repr__(self):
|
|
105
|
+
return f"vector3d({self.x}, {self.y}, {self.z})"
|
|
106
|
+
|
|
107
|
+
def copy(self):
|
|
108
|
+
return vector3d(self.x, self.y, self.z)
|
|
109
|
+
|
|
110
|
+
def __neg__(self):
|
|
111
|
+
return vector3d(-self.x, -self.y, -self.z)
|
|
112
|
+
|
|
113
|
+
@classproperty
|
|
114
|
+
def up(cls):
|
|
115
|
+
return cls(0, 1, 0)
|
|
116
|
+
|
|
117
|
+
@classproperty
|
|
118
|
+
def down(cls):
|
|
119
|
+
return cls(0, -1, 0)
|
|
120
|
+
|
|
121
|
+
@classproperty
|
|
122
|
+
def right(cls):
|
|
123
|
+
return cls(1, 0, 0)
|
|
124
|
+
|
|
125
|
+
@classproperty
|
|
126
|
+
def left(cls):
|
|
127
|
+
return cls(-1, 0, 0)
|
|
128
|
+
|
|
129
|
+
@classproperty
|
|
130
|
+
def forward(cls):
|
|
131
|
+
return cls(0, 0, 1)
|
|
132
|
+
|
|
133
|
+
@classproperty
|
|
134
|
+
def back(cls):
|
|
135
|
+
return cls(0, 0, -1)
|
|
136
|
+
|
|
137
|
+
@classproperty
|
|
138
|
+
def one(cls):
|
|
139
|
+
return cls(1, 1, 1)
|
|
140
|
+
|
|
141
|
+
@classproperty
|
|
142
|
+
def zero(cls):
|
|
143
|
+
return cls(0, 0, 0)
|
|
144
|
+
|
|
145
|
+
class Color:
|
|
146
|
+
def __init__(self, r, g, b):
|
|
147
|
+
self.r = r
|
|
148
|
+
self.g = g
|
|
149
|
+
self.b = b
|
|
150
|
+
|
|
151
|
+
@staticmethod
|
|
152
|
+
def RGB(r, g, b): return Color(r, g, b)
|
|
153
|
+
|
|
154
|
+
@staticmethod
|
|
155
|
+
def hex(hex_string):
|
|
156
|
+
if not hex_string.startswith("#"): raise Exception("String must start with #")
|
|
157
|
+
if not len(hex_string) == 7: raise Exception("String must contain 7 characters")
|
|
158
|
+
r = int(hex_string[1:3], 16)
|
|
159
|
+
g = int(hex_string[3:5], 16)
|
|
160
|
+
b = int(hex_string[5:7], 16)
|
|
161
|
+
return Color(r, g, b)
|
|
162
|
+
|
|
163
|
+
def __eq__(self, other):
|
|
164
|
+
if self.r == other.r and self.g == other.g and self.b == other.b:
|
|
165
|
+
return True
|
|
166
|
+
return False
|
|
167
|
+
|
|
168
|
+
def to_hex(self):
|
|
169
|
+
if not all(0 <= x <= 255 for x in (self.r, self.g, self.b)):
|
|
170
|
+
raise ValueError("RGB values must be in the range 0-255")
|
|
171
|
+
return "#{:02X}{:02X}{:02X}".format(self.r, self.g, self.b)
|
|
172
|
+
|
|
173
|
+
def to_rgb(self):
|
|
174
|
+
return self.r, self.g, self.b
|
|
175
|
+
|
|
176
|
+
@classproperty
|
|
177
|
+
def black(cls):
|
|
178
|
+
return cls(0, 0, 0)
|
|
179
|
+
|
|
180
|
+
@classproperty
|
|
181
|
+
def white(cls):
|
|
182
|
+
return cls(255, 255, 255)
|
|
183
|
+
|
|
184
|
+
@classproperty
|
|
185
|
+
def red(cls): return cls(255, 0, 0)
|
|
186
|
+
|
|
187
|
+
@classproperty
|
|
188
|
+
def green(cls): return cls(0, 255, 0)
|
|
189
|
+
|
|
190
|
+
@classproperty
|
|
191
|
+
def blue(cls): return cls(0, 0, 255)
|
|
192
|
+
|
|
193
|
+
@classproperty
|
|
194
|
+
def yellow(cls): return cls(255, 255, 0)
|
|
195
|
+
|
|
196
|
+
@classproperty
|
|
197
|
+
def cyan(cls): return cls(0, 255, 255)
|
|
198
|
+
|
|
199
|
+
@classproperty
|
|
200
|
+
def magenta(cls): return cls(255, 0, 255)
|
|
201
|
+
|
|
202
|
+
@classproperty
|
|
203
|
+
def grey(cls): return cls(33, 33, 33)
|
|
204
|
+
|
|
205
|
+
@classproperty
|
|
206
|
+
def light_grey(cls): return cls(80, 80, 80)
|
|
207
|
+
|
|
208
|
+
@classproperty
|
|
209
|
+
def light_red(cls): return cls(253, 89, 111)
|
|
210
|
+
|
|
211
|
+
@classproperty
|
|
212
|
+
def light_green(cls): return cls(0, 190, 160)
|
|
213
|
+
|
|
214
|
+
@classproperty
|
|
215
|
+
def light_yellow(cls): return cls(254, 208, 95)
|
|
216
|
+
|
|
217
|
+
@classproperty
|
|
218
|
+
def orange(cls): return cls(254, 166, 93)
|
|
219
|
+
|
|
220
|
+
@classproperty
|
|
221
|
+
def light_blue(cls): return cls(109, 154, 218)
|
|
222
|
+
|
|
223
|
+
class Light:
|
|
224
|
+
def __init__(self, position: vector3d, color: Color, intensity: float = 1.0, show=False, name="Light"):
|
|
225
|
+
self.position = position
|
|
226
|
+
self.color = color
|
|
227
|
+
self.intensity = intensity
|
|
228
|
+
self.show = show
|
|
229
|
+
self.name = name
|
|
230
|
+
|
|
231
|
+
def serialize(self):
|
|
232
|
+
return {
|
|
233
|
+
"position": self.position.totuple(),
|
|
234
|
+
"color": self.color.to_rgb(),
|
|
235
|
+
"intensity": self.intensity,
|
|
236
|
+
"show": self.show,
|
|
237
|
+
"name": self.name,
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
@staticmethod
|
|
241
|
+
def deserialize(data):
|
|
242
|
+
position = data["position"]
|
|
243
|
+
color = data["color"]
|
|
244
|
+
intensity = data["intensity"]
|
|
245
|
+
show = data["show"]
|
|
246
|
+
name = data["name"]
|
|
247
|
+
|
|
248
|
+
return Light(position=vector3d.fromtuple(position), color=Color.RGB(*color), intensity=intensity, show=show, name=name)
|
|
249
|
+
|
|
250
|
+
class DirectionalLight:
|
|
251
|
+
def __init__(
|
|
252
|
+
self,
|
|
253
|
+
rotation: vector3d, # rotation defines direction
|
|
254
|
+
color: Color,
|
|
255
|
+
intensity: float = 1.0,
|
|
256
|
+
position: vector3d = vector3d.zero, # directional lights can have a position
|
|
257
|
+
show: bool = False,
|
|
258
|
+
name: str = "DirectionalLight"
|
|
259
|
+
):
|
|
260
|
+
self.rotation = rotation
|
|
261
|
+
self.position = position
|
|
262
|
+
self.color = color
|
|
263
|
+
self.intensity = intensity
|
|
264
|
+
self.show = show
|
|
265
|
+
self.name = name
|
|
266
|
+
|
|
267
|
+
# compute forward direction from rotation
|
|
268
|
+
self.direction = self._compute_direction()
|
|
269
|
+
|
|
270
|
+
def _compute_direction(self):
|
|
271
|
+
"""Turn rotation (Euler angles in degrees) into a normalized direction vector."""
|
|
272
|
+
|
|
273
|
+
# Convert to radians
|
|
274
|
+
rx = math.radians(self.rotation.x)
|
|
275
|
+
ry = math.radians(self.rotation.y)
|
|
276
|
+
rz = math.radians(self.rotation.z)
|
|
277
|
+
|
|
278
|
+
# Rotation matrices in ZYX order (standard for camera/light)
|
|
279
|
+
cz, sz = math.cos(rz), math.sin(rz)
|
|
280
|
+
cy, sy = math.cos(ry), math.sin(ry)
|
|
281
|
+
cx, sx = math.cos(rx), math.sin(rx)
|
|
282
|
+
|
|
283
|
+
# Forward vector through rotation matrix
|
|
284
|
+
# This matches right-handed forward = +Z convention
|
|
285
|
+
m00 = cy * cz
|
|
286
|
+
m01 = cz * sx * sy - cx * sz
|
|
287
|
+
m02 = sx * sz + cx * cz * sy
|
|
288
|
+
|
|
289
|
+
# Forward direction = (m02, m12, m22) depending on convention
|
|
290
|
+
# You use Z-forward, so:
|
|
291
|
+
forward = vector3d(m02, -(sx * cy), cy * cx)
|
|
292
|
+
|
|
293
|
+
return forward.normalized
|
|
294
|
+
|
|
295
|
+
# ---------------------- Serialization -------------------------
|
|
296
|
+
|
|
297
|
+
def serialize(self):
|
|
298
|
+
return {
|
|
299
|
+
"rotation": self.rotation.totuple(),
|
|
300
|
+
"position": self.position.totuple(),
|
|
301
|
+
"color": self.color.to_rgb(),
|
|
302
|
+
"intensity": self.intensity,
|
|
303
|
+
"show": self.show,
|
|
304
|
+
"name": self.name,
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
@staticmethod
|
|
308
|
+
def deserialize(data):
|
|
309
|
+
rotation = vector3d.fromtuple(data["rotation"])
|
|
310
|
+
position = vector3d.fromtuple(data["position"])
|
|
311
|
+
color = Color.RGB(*data["color"])
|
|
312
|
+
intensity = data["intensity"]
|
|
313
|
+
show = data["show"]
|
|
314
|
+
name = data["name"]
|
|
315
|
+
|
|
316
|
+
return DirectionalLight(
|
|
317
|
+
rotation=rotation,
|
|
318
|
+
position=position,
|
|
319
|
+
color=color,
|
|
320
|
+
intensity=intensity,
|
|
321
|
+
show=show,
|
|
322
|
+
name=name
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
class Script:
|
|
326
|
+
def __init__(self, obj, script_path, context):
|
|
327
|
+
self.obj = obj
|
|
328
|
+
self.context = context
|
|
329
|
+
self.script_path = script_path
|
|
330
|
+
self.module = None
|
|
331
|
+
self.cls = None
|
|
332
|
+
self.instance = None
|
|
333
|
+
self.load(script_path)
|
|
334
|
+
|
|
335
|
+
def load(self, path):
|
|
336
|
+
if not os.path.exists(path):
|
|
337
|
+
print(f"[Script] File not found: {path}")
|
|
338
|
+
return
|
|
339
|
+
module_name = os.path.splitext(os.path.basename(path))[0]
|
|
340
|
+
spec = importlib.util.spec_from_file_location(module_name, path)
|
|
341
|
+
module = importlib.util.module_from_spec(spec)
|
|
342
|
+
sys.modules[module_name] = module
|
|
343
|
+
try:
|
|
344
|
+
spec.loader.exec_module(module)
|
|
345
|
+
except Exception as e:
|
|
346
|
+
print(f"[Script] Error loading module {path}: {e}")
|
|
347
|
+
return
|
|
348
|
+
self.module = module
|
|
349
|
+
|
|
350
|
+
# Class is PascalCase version of file name
|
|
351
|
+
class_name = ''.join(word.capitalize() for word in module_name.split('_'))
|
|
352
|
+
if hasattr(module, class_name):
|
|
353
|
+
self.cls = getattr(module, class_name)
|
|
354
|
+
else:
|
|
355
|
+
print(f"[Script] No class '{class_name}' found in {path}")
|
|
356
|
+
|
|
357
|
+
def init_instance(self):
|
|
358
|
+
if self.cls is None or self.instance is not None:
|
|
359
|
+
return
|
|
360
|
+
try:
|
|
361
|
+
self.instance = self.cls(self.obj, self.context)
|
|
362
|
+
except Exception as e:
|
|
363
|
+
print(f"[Script] Failed to instantiate script for {self.obj.name if hasattr(self.obj, "name") else "Engine"}: {e}")
|
|
364
|
+
self.instance = None
|
|
365
|
+
|
|
366
|
+
if self.instance and hasattr(self.instance, "start"):
|
|
367
|
+
try:
|
|
368
|
+
self.instance.start()
|
|
369
|
+
except Exception:
|
|
370
|
+
pass
|
|
371
|
+
|
|
372
|
+
def update(self, dt):
|
|
373
|
+
if self.instance is None:
|
|
374
|
+
return
|
|
375
|
+
self.instance.update(dt)
|
|
376
|
+
|
|
377
|
+
def run(self, func_name):
|
|
378
|
+
if self.instance is None:
|
|
379
|
+
return
|
|
380
|
+
|
|
381
|
+
if hasattr(self.instance, func_name):
|
|
382
|
+
func = getattr(self.instance, func_name)
|
|
383
|
+
func()
|
|
384
|
+
|
|
385
|
+
class EngineError(Exception):
|
|
386
|
+
pass
|
|
387
|
+
|
|
388
|
+
class Message:
|
|
389
|
+
def __init__(self, message, sender):
|
|
390
|
+
self.message = message
|
|
391
|
+
self.sender = sender
|
|
392
|
+
self.time = time.time()
|
|
393
|
+
|
|
394
|
+
def __repr__(self):
|
|
395
|
+
return f"Message({self.message=}, {self.sender=}, {self.time=})"
|
|
396
|
+
|
|
397
|
+
class RigidBody:
|
|
398
|
+
def __init__(self, mass=1.0, bounce=0.3, gravity=True):
|
|
399
|
+
self.mass = mass
|
|
400
|
+
self.bounce = bounce
|
|
401
|
+
self.gravity = gravity
|
|
402
|
+
|
|
403
|
+
self.velocity = vector3d.zero
|
|
404
|
+
self.forces = vector3d.zero
|
|
405
|
+
|
|
406
|
+
def add_force(self, f):
|
|
407
|
+
self.forces += f
|
|
408
|
+
|
|
409
|
+
class ShadowVolume:
|
|
410
|
+
def __init__(self, minx, maxx, miny, maxy, minz, maxz):
|
|
411
|
+
self.minx = minx
|
|
412
|
+
self.maxx = maxx
|
|
413
|
+
self.miny = miny
|
|
414
|
+
self.maxy = maxy
|
|
415
|
+
self.minz = minz
|
|
416
|
+
self.maxz = maxz
|
|
417
|
+
|
|
418
|
+
def get_forward(rotation):
|
|
419
|
+
yaw = math.radians(rotation.z)
|
|
420
|
+
fx = -math.sin(yaw)
|
|
421
|
+
fy = 0
|
|
422
|
+
fz = math.cos(yaw)
|
|
423
|
+
return vector3d(fx, fy, fz)
|