This commit is contained in:
n3tael 2024-04-03 16:12:23 +03:00
commit 54c276bd8b
Signed by: n3tael
GPG Key ID: F305925762F035A8
2 changed files with 28 additions and 19 deletions

12
gyro.py
View File

@ -1,6 +1,7 @@
from numpy import complex256
def ZeroCheck(cN): def ZeroCheck(cN):
if cN == complex(0,0): if cN == complex256(complex(0,0)):
return complex(1,0) return complex256(complex(1,0))
else: else:
return cN return cN
@ -30,12 +31,13 @@ def Poincare2Klein(cN):
class GyroVector: class GyroVector:
def __init__(self, cPos, cRot): def __init__(self, cPos, cRot):
self.cPos = complex(cPos) self.cPos = complex256(cPos)
self.cRot = complex(cRot) self.cRot = complex256(cRot)
self.normalize() self.normalize()
def __add__(gA, gB): def __add__(gA, gB):
cAdd, cGyr = MobiusAddGyr(gA.cPos, gB.cPos * (1 / gA.cRot)) #cAdd, cGyr = MobiusAddGyr(gA.cPos, gB.cPos / gA.cRot)
cAdd, cGyr = MobiusAddGyr(gA.cPos, gB.cPos)
return GyroVector(cAdd, gA.cRot * gB.cRot * cGyr) return GyroVector(cAdd, gA.cRot * gB.cRot * cGyr)
def __neg__(self): def __neg__(self):

35
main.py
View File

@ -1,8 +1,8 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from numpy import complex256
from time import time_ns, sleep from time import time_ns, sleep
from cmath import exp from cmath import exp, pi
import math
import pygame import pygame
import pygame.freetype import pygame.freetype
from gyro import * from gyro import *
@ -10,16 +10,19 @@ from lines import *
def deg2rad(rA): return rA/180*PI def deg2rad(rA): return rA/180*PI
I = complex(0,1) I = complex256(complex(0,1))
WHITE = (255,255,255) WHITE = (255,255,255)
BLACK = (0,0,0) BLACK = (0,0,0)
OFFSET = 0.015625 OFFSET = 0.015625
PI = math.pi PI = pi
ROT = cNorm(exp(deg2rad(2)*I)) ROT = cNorm(exp(deg2rad(2)*I))
IROT = 1/ROT IROT = 1/ROT
F = 5 * 10**7 F = 5 * 10**7
F_ = 10**9 F_ = 10**9
SKY = (127,127,255)
GROUND = (102, 51, 0)
class Segment: class Segment:
def __init__(self,bA,cA,cB,trColor): def __init__(self,bA,cA,cB,trColor):
self.isFinite = bA self.isFinite = bA
@ -33,10 +36,10 @@ class DrawnSegment:
self.color = color self.color = color
level = [ level = [
Segment(False,.5,I*.5,(255,0,0)), Segment(True,.6,I*.6,(255,0,0)),
Segment(False,.5,-I*.5,(0,255,0)), Segment(True,.6,-I*.6,(0,255,0)),
Segment(False,-.5,-I*.5,(0,0,255)), Segment(True,-.6,-I*.6,(0,0,255)),
Segment(False,-.5,I*.5,(255,255,0)) Segment(True,-.6,I*.6,(255,255,0))
] ]
def draw(level,gPlayer,fov,res): def draw(level,gPlayer,fov,res):
@ -50,10 +53,12 @@ def draw(level,gPlayer,fov,res):
tA = Poincare2Klein(MobiusAdd(j.pointA,-gPlayer.cPos)) tA = Poincare2Klein(MobiusAdd(j.pointA,-gPlayer.cPos))
tB = Poincare2Klein(MobiusAdd(j.pointB,-gPlayer.cPos)) tB = Poincare2Klein(MobiusAdd(j.pointB,-gPlayer.cPos))
try: # This function faults from time to time try: # This function faults from time to time
cInt = cLineIntersection(tA,tB,complex(0),rot) cInt = cLineIntersection(tA,tB,complex256(0),rot)
except ZeroDivisionError: except ZeroDivisionError:
continue continue
rDist = cDist(0,cInt) rDist = cDist(0,cInt)
if not (j.isFinite and cBetween(tA,tB,cInt)):
continue
if cDot(cInt,rot) > 0: if cDot(cInt,rot) > 0:
continue continue
if (1 - m.height) > rDist: if (1 - m.height) > rDist:
@ -85,16 +90,18 @@ def mainLoop():
if keys[pygame.K_a]: if keys[pygame.K_a]:
gPlayer.rotate(IROT) gPlayer.rotate(IROT)
if keys[pygame.K_w]: if keys[pygame.K_w]:
gPlayer -= GyroVector(OFFSET, 1) gPlayer -= GyroVector(OFFSET * gPlayer.cRot, 1)
if keys[pygame.K_s]: if keys[pygame.K_s]:
gPlayer += GyroVector(OFFSET, 1) gPlayer += GyroVector(OFFSET * gPlayer.cRot, 1)
display.fill(WHITE) display.fill(WHITE)
#pygame.draw.rect(display,BLACK, c_tr(Poincare2Klein(gPlayer.cPos) * -100) + (100,100),0) #pygame.draw.rect(display,BLACK, c_tr(Poincare2Klein(gPlayer.cPos) * -100) + (100,100),0)
drawn = draw(level,gPlayer,PI/2,640) drawn = draw(level,gPlayer,PI/2,160)
pygame.draw.rect(display,SKY, (0,0,1280,360))
pygame.draw.rect(display,GROUND, (0,360,1280,360))
n = 0 n = 0
for i in drawn: for i in drawn:
pygame.draw.rect(display,i.color, (n,360 - (cap(i.height) * 500),10,cap(i.height) * 1000)) pygame.draw.rect(display,i.color, (n,360 - (cap(i.height) * 360),10,cap(i.height) * 1000))
n += 2 n += 8
font.render_to(display, (8, 8), "cPos: " + str(gPlayer.cPos), (255, 255, 255), (0, 0, 0)) font.render_to(display, (8, 8), "cPos: " + str(gPlayer.cPos), (255, 255, 255), (0, 0, 0))
font.render_to(display, (8, 26), "cRot: " + str(gPlayer.cRot), (255, 255, 255), (0, 0, 0)) font.render_to(display, (8, 26), "cRot: " + str(gPlayer.cRot), (255, 255, 255), (0, 0, 0))