DKL9 GitList
Repositories
DKL9 home
voxels
Code
Commits
Branches
Tags
Search
Tree:
a93b274
Branches
Tags
master
voxels
src
entity.py
Move source to src/
dkl9
commited
a93b274
at 2025-183 13:45:25
entity.py
Blame
History
Raw
from libs import * from raycast import intersect_tri class Entity: def __init__(self, verts, pos, **kwargs): self.verts = vec(verts) self.hud = kwargs.get("hud", False) self.pos = vec(pos) self.orient = np.identity(4, dtype=FLOAT) self.onclick = lambda s, b, n, h: print(f"{h}:{b} on {s}:{n}") def translate(self): m = np.identity(4, dtype=FLOAT) m[:3, 3] = self.pos return m def rotate(self, theta, axis): x, y, z = axis c, s = np.cos(theta), np.sin(theta) t = 1 - c self.orient = vec([ [t*x*x + c, t*x*y - s*z, t*x*z + s*y, 0], [t*x*y + s*z, t*y*y + c, t*y*z - s*x, 0], [t*x*z - s*y, t*y*z + s*x, t*z*z + c, 0], [0, 0, 0, 1] ]) @ self.orient def click(self, button, face, hand=1): return self.onclick(self, button, face, hand) def raycast(self, ro, rd): if self.hud or self.draw_mode == GL_LINES: return elif self.draw_mode == GL_TRIANGLE_STRIP: ir = range(len(self.verts) - 2) tl = ((i, self.verts[i:i + 3]) for i in ir) elif self.draw_mode == GL_TRIANGLES: ir = range(len(self.verts) // 3) tl = ((i, self.verts[3 * i:3 * (i + 1)]) for i in ir) else: raise NotImplementedError d, n = None, None for (i, t) in tl: t = np.dot(t, self.orient[:3, :3].T) + self.pos r = intersect_tri(ro, rd, t) if r and (d is None or r < d): d, n = r, i return d, n