forked from bedohswe/p3he
Initial commit.
This commit is contained in:
commit
239d07c654
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
__pycache__/
|
5
LICENSE
Normal file
5
LICENSE
Normal file
@ -0,0 +1,5 @@
|
||||
Copyright (C) 2024 by bedohswe bedohswe@noreply.localhost
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
69
gyro.py
Normal file
69
gyro.py
Normal file
@ -0,0 +1,69 @@
|
||||
def ZeroCheck(cN):
|
||||
if cN == complex(0,0):
|
||||
return complex(1,0)
|
||||
else:
|
||||
return cN
|
||||
|
||||
def c_tr(cN):
|
||||
return cN.real, cN.imag
|
||||
|
||||
def cNorm(cN):
|
||||
return ZeroCheck(cN)/abs(ZeroCheck(cN))
|
||||
|
||||
def cDot(cA, cB):
|
||||
return (cA * cB.conjugate()).real
|
||||
|
||||
def cDist(cA, cB):
|
||||
return abs(cA-cB)
|
||||
|
||||
def MobiusAdd(cA, cB):
|
||||
return (cA + cB) / (1 + cA.conjugate() * cB)
|
||||
|
||||
def MobiusGyr(cA, cB):
|
||||
return (1 + cA * cB.conjugate()) / (1 + cA.conjugate() * cB)
|
||||
|
||||
def MobiusAddGyr(cA, cB):
|
||||
return MobiusAdd(cA, cB), MobiusGyr(cA, cB)
|
||||
|
||||
def Poincare2Klein(cN):
|
||||
return 2*cN / (1 + cDot(cN,cN))
|
||||
|
||||
class GyroVector:
|
||||
def __init__(self, cPos, cRot):
|
||||
self.cPos = complex(cPos)
|
||||
self.cRot = complex(cRot)
|
||||
self.normalize()
|
||||
|
||||
def __add__(gA, gB):
|
||||
cAdd, cGyr = MobiusAddGyr(gA.cPos, gB.cPos / ZeroCheck(gA.cRot))
|
||||
return GyroVector(cAdd, gA.cRot * gB.cRot * cGyr)
|
||||
|
||||
def __neg__(self):
|
||||
return GyroVector(-(self.cRot * self.cPos), 1/self.cRot)
|
||||
|
||||
def __sub__(gA, gB):
|
||||
return gA + (-gB)
|
||||
|
||||
def copy(self):
|
||||
return GyroVector(self.cPos,self.cRot)
|
||||
|
||||
def normalize(self):
|
||||
if abs(self.cPos) > 1:
|
||||
self.cPos = cNorm(self.cPos)
|
||||
self.cRot = cNorm(self.cRot)
|
||||
|
||||
def rotate(self,cRot):
|
||||
self.cRot *= cRot
|
||||
|
||||
def transform(self, cA):
|
||||
cAdd, cGyr = MobiusAddGyr(self.cPos, cA / ZeroCheck(self.cRot))
|
||||
self.cPos = cAdd
|
||||
self.cRot = self.cRot * cGyr
|
||||
|
||||
def transformed(self, cA):
|
||||
#cAdd, cGyr = MobiusAddGyr(self.cPos, cA / ZeroCheck(self.cRot))
|
||||
#return GyroVector(cAdd, self.cRot * cGyr)
|
||||
return gA.copy().transform(cA)
|
||||
|
||||
def nrtransformed(self, cA):
|
||||
return MobiusAdd(self.cPos, cA / ZeroCheck(self.cRot))
|
14
lines.py
Normal file
14
lines.py
Normal file
@ -0,0 +1,14 @@
|
||||
def LineIntersection(rX1,rY1,rX2,rY2,rX3,rY3,rX4,rY4):
|
||||
rX = ( (rX1*rY2-rY1*rX2)*(rX3-rX4)-(rX1-rX2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) )
|
||||
rY = ( (rX1*rY2-rY1*rX2)*(rY3-rY4)-(rY1-rY2)*(rX3*rY4-rY3*rX4) ) / ( (rX1-rX2)*(rY3-rY4)-(rY1-rY2)*(rX3-rX4) )
|
||||
return (rX, rY)
|
||||
|
||||
def cLineIntersection(cA0, cA1, cB0, cB1):
|
||||
rX, rY = LineIntersection(cA0.real, cA0.imag, cA1.real, cA1.imag, cB0.real, cB0.imag, cB1.real, cB1.imag)
|
||||
return complex(rX, rY)
|
||||
|
||||
def Between(rA,rB,rN):
|
||||
return (rA < rN and rN < rB) or (rB < rN and rN < rA)
|
||||
|
||||
def cBetween(cA,cB,cN):
|
||||
return Between(cA.real,cB.real,cN.real) and Between(cA.imag,cB.imag,cN.imag)
|
106
main.py
Executable file
106
main.py
Executable file
@ -0,0 +1,106 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from time import time_ns, sleep
|
||||
from cmath import exp
|
||||
import pygame
|
||||
import pygame.freetype
|
||||
from gyro import *
|
||||
from lines import *
|
||||
|
||||
|
||||
WHITE = (255,255,255)
|
||||
BLACK = (0,0,0)
|
||||
OFFSET = .00781250
|
||||
I = complex(0,1)
|
||||
PI = 3.14159265358979323846264338327
|
||||
ROT = cNorm(0.99939 + 0.0003490*I)
|
||||
IROT = 1/ROT
|
||||
F = 5 * 10**7
|
||||
F_ = 10**9
|
||||
|
||||
|
||||
class Segment:
|
||||
def __init__(self,bA,cA,cB,trColor):
|
||||
self.isFinite = bA
|
||||
self.pointA = cA
|
||||
self.pointB = cB
|
||||
self.color = trColor
|
||||
|
||||
class DrawnSegment:
|
||||
def __init__(self,height,color):
|
||||
self.height = height
|
||||
self.color = color
|
||||
|
||||
level = [
|
||||
# Segment(False,cNorm(-1),cNorm(I),(255,255,20)),
|
||||
Segment(False,cNorm(-1),cNorm(I),BLACK)
|
||||
]
|
||||
|
||||
def draw(level,gPlayer,fov,res):
|
||||
drawn = list()
|
||||
irot = exp(fov/res*I)
|
||||
for i in range(res):
|
||||
m = DrawnSegment(0,BLACK)
|
||||
rot = irot * gPlayer.cRot**(i-(res/2))
|
||||
for j in level:
|
||||
tA = Poincare2Klein(MobiusAdd(j.pointA,-gPlayer.cPos))
|
||||
tB = Poincare2Klein(MobiusAdd(j.pointB,-gPlayer.cPos))
|
||||
cInt = cLineIntersection(tA,tB,complex(0),rot)
|
||||
rDist = cDist(0,cInt)
|
||||
if cDot(cInt,rot) > 0:
|
||||
continue
|
||||
if (1 - m.height) > rDist:
|
||||
m = DrawnSegment(1-rDist,j.color)
|
||||
drawn.append(m)
|
||||
return drawn
|
||||
|
||||
def cap(rN):
|
||||
if rN < 0:
|
||||
return 0
|
||||
return rN
|
||||
|
||||
def mainLoop():
|
||||
gPlayer = GyroVector(0,1)
|
||||
display = pygame.display.set_mode((1280,720))
|
||||
font = pygame.freetype.Font(None,24)
|
||||
while True:
|
||||
framestart = time_ns()
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
return True
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_d]:
|
||||
gPlayer.rotate(IROT)
|
||||
if keys[pygame.K_a]:
|
||||
gPlayer.rotate(ROT)
|
||||
if keys[pygame.K_w]:
|
||||
gPlayer += GyroVector(OFFSET, 1)
|
||||
if keys[pygame.K_s]:
|
||||
gPlayer -= GyroVector(OFFSET, 1)
|
||||
display.fill(WHITE)
|
||||
#pygame.draw.rect(display,BLACK, c_tr(Poincare2Klein(gPlayer.cPos) * -100) + (100,100),0)
|
||||
drawn = draw(level,gPlayer,PI/2,40)
|
||||
n = 0
|
||||
for i in drawn:
|
||||
pygame.draw.rect(display,i.color, (n,500 - (cap(i.height) * 500),10,cap(i.height) * 1000))
|
||||
n += 10
|
||||
font.render_to(display, (20, 150), str(gPlayer.cPos), (0, 0, 0))
|
||||
font.render_to(display, (20, 250), str(gPlayer.cRot), (0, 0, 0))
|
||||
pygame.display.update()
|
||||
|
||||
frameend = time_ns()
|
||||
sleep((frameend-framestart) / F_)
|
||||
return True
|
||||
|
||||
def main():
|
||||
pygame.init()
|
||||
pygame.display.set_caption('P3HE')
|
||||
if not pygame.get_init():
|
||||
return False
|
||||
retstatus = mainLoop()
|
||||
return retstatus
|
||||
|
||||
if __name__ == "__main__":
|
||||
if not main():
|
||||
print("An error occured")
|
Loading…
Reference in New Issue
Block a user