def __init__(self, width=1280, height=720, color=(255, 255, 255), fullScreen=False): self.width = width self.height = height #full screen if (fullScreen): self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) else: self.screen = pygame.display.set_mode((width, height)) self.color = color self.screen.fill((self.color)) self.Objects = [] self.Lights = [] self.camera = Camera(Vector(0, 0, 0), width / 2, height / 2) self.Lights.append(Light(Vector(0, 0, 0))) #seznam prave se vykreslujicich trojuhelniku #face je (vertex, vertex, vertex) self.faces = []
def __init__(self, x, y, z, a=1): Vector.__init__(self, x, y, z) self.actual = Vector(0, 0, 0) self.a = Vector(x / a, y / a, z / a) #self.a.speak() self.t = 0 self.time = abs(a)
def updateMesh(self): self.vertices = [] for i, v in enumerate(self.verticesData): vertex = Vector(self.Position.x + v.Position.x*self.scale, self.Position.y + v.Position.y*self.scale, self.Position.z + v.Position.z*self.scale) vertex = vertex.rotateAround(self.Position, self.Rotation) self.vertices.append(Vertex(vertex))
def __init__(self, engine, x, y, z, faces): self.Position = Vector(x, y, z) self.Faces = faces self.mode = "faces" self.engine = engine self.showHitbox = True self.hitBox = [] self.updateFaces()
def __init__(self, engine, x, y, z, mesh=None, rigidBody=False): Component.__init__(self, engine, mesh, rigidBody) self.Position = Vector(x, y, z) self.rigidBody = rigidBody self.v = Vector(0, 0, 0) #pohybovy vektor telesa self.t = 0 self.deltaTime = 0 self.forces = [] self.solid = True self.screen = None
def Update(self): self.Input() self.S = Vector( self.Position.x + self.f.x, self.Position.y + self.f.y, self.Position.z + self.f.z, ) self.B = Vector( self.Position.x - self.f.x, self.Position.y - self.f.y, self.Position.z - self.f.z, )
def physics(self): if (self.rigidBody): self.deltaTime = time.time() - self.engine.oldTime self.v = Vector(0, 0, 0) self.applyForces() self.checkCollisions() self.gravity() self.Position.addVector(self.v)
def checkPosition(self): if (self.Position.x <= -1000): self.removeAll() self.Position = Vector(0, 20, 0) if (self.Position.y <= -1000): self.t = 0 self.removeAll() self.Position = Vector(0, 20, 0) if (self.Position.x >= 1000): self.removeAll() self.Position = Vector(0, 20, 0) if (self.Position.y >= 1000): self.removeAll() self.Position = Vector(0, 20, 0)
def __init__(self, engine, parent, x, y, z, faces): self.Position = Vector(x, y, z) self.Faces = faces self.mode = "faces" self.parent = parent self.engine = engine self.showHitbox = True self.updateFaces() self.updateRadius() self.collider = MeshCollider(self)
def loadMesh(self, mesh, scale, color): self.scale = scale x = self.Position.x y = self.Position.y z = self.Position.z with open(mesh, "r") as m: self.meshData = json.load(m) self.vertices = [] for v in self.meshData["vertices"]: self.verticesData.append(Vertex(Vector(v[0], v[1], v[2]))) self.triangles = [] for t in self.meshData["triangles"]: self.createTriangle(t[0], t[1], t[2]) #v json jsou data o barve if ("colors" in self.meshData): self.colors = [] for c in self.meshData["colors"]: self.colors.append(c) else: self.createColor(color)
def calculateNormal(self, v1, v2, v3): v = v1.Position.newVector(v2.Position) u = v1.Position.newVector(v3.Position) #normalovy vektor k rovine face return Vector(u.y*v.z - v.y*u.z, u.z*v.x - v.z*u.x, u.x*v.y - v.x*u.y)
def setView(self, keys): if (keys[K_2] or keys[K_KP2]): #zepredu self.Position = Vector(6, 5, -17) self.rotX = 0 self.rotY = 0 if (keys[K_6] or keys[K_KP6]): #zprava self.Position = Vector(29, 5, 6) self.rotX = 3 * math.pi / 2 self.rotY = 0 if (keys[K_8] or keys[K_KP8]): #zezadu self.Position = Vector(6, 5, 29) self.rotX = math.pi self.rotY = 0 if (keys[K_4] or keys[K_KP4]): #zleva self.Position = Vector(-17, 5, 6) self.rotX = math.pi / 2 self.rotY = 0
def gravity(self): self.t += self.deltaTime #self.v.y -= 0.981*self.t #self.v.y -= self.t answer = self.mesh.checkCollisions(self, Vector(0, -self.t, 0)) if (not answer[0] and self.v.y <= 0): self.v.y -= self.t else: self.t = 0
def update(self, deltaTime): self.t += deltaTime if (self.t <= self.time): self.actual.x = self.x - self.a.x * self.t self.actual.y = self.y - self.a.y * self.t self.actual.z = self.z - self.a.z * self.t else: return Vector(0, 0, 0) return self.actual
def getDirection(self, mesh): if (len(self.V) >= 3): #smerove vektory roviny face v = self.V[1].Position.newVector(self.V[0].Position) u = self.V[2].Position.newVector(self.V[0].Position) #normalovy vektor k rovine face n = Vector(u.y*v.z - v.y*u.z, u.z*v.x - v.z*u.x, u.x*v.y - v.x*u.y) #vektor od bodu teziste do stredu meshe a = mesh.Position.newVector(self.center) if (n.angle(a) <= math.pi/2): n = n.reverseVector() return n else: return Vector(0,0,0)
def __init__(self, vectors, color=(255, 0, 255), bothSides=False): self.v = vectors #vektory vedouci od stredu mesh k bodum self.V = [] #body definujici face while (len(self.v) < 2): self.v.append(Vector(0,0,0)) self.color = color self.bothSides = bothSides self.rFase = 0 self.tempColor = (0,0,0)
def __init__(self, position, rotation=Vector(0,0,0)): self.Position = position self.Rotation = rotation self.vertices = [] self.verticesData = [] self.triangles = [] self.colors = [] self.scale = 1 self.defaultColor = (255,0,255)
def Rotate(self, event): if (len(event) == 0): pygame.mouse.set_visible(True) xy = pygame.mouse.get_rel() x = 0 y = 0 if (event[0]): #leve tlacitko self.x = self.x elif (event[2]): #prave tlacitko #pygame.mouse.set_visible(False) x = xy[0] / 2 y = xy[1] / 2 self.rotX += x * (math.pi / 180) / 2 self.rotY -= y * (math.pi / 180) / 2 x = math.sin(self.rotX) y = math.sin(self.rotY) z = math.cos(self.rotX) self.f = Vector(x, y, z) x = math.sin(self.rotX + math.pi / 2) z = math.cos(self.rotX + math.pi / 2) self.r = Vector(x, 0, z) self.u = Vector(self.f.y * self.r.z - self.r.y * self.f.z, self.f.z * self.r.x - self.r.z * self.f.x, self.f.x * self.r.y - self.r.x * self.f.y)
def findG(self, vertex, newVertex, face): c1 = newVertex.x c2 = newVertex.y c3 = newVertex.z u = face.V[0].Position.newVector(face.V[1].Position) u1 = u.x u2 = u.y u3 = u.z r = face.V[2].Position.newVector(face.V[1].Position) r1 = r.x r2 = r.y r3 = r.z s1 = face.V[0].Position.x s2 = face.V[0].Position.y s3 = face.V[0].Position.z g1 = vertex.Position.x - c1 g2 = vertex.Position.y - c2 g3 = vertex.Position.z - c3 #miluju matlab try: t = (-(c1*r2*u3 - c1*r3*u2 - c2*r1*u3 + c2*r3*u1 + c3*r1*u2 - c3*r2*u1 + r1*s2*u3 - r1*s3*u2 - r2*s1*u3 + r2*s3*u1 + r3*s1*u2 - r3*s2*u1)/ (g1*r2*u3 - g1*r3*u2 - g2*r1*u3 + g2*r3*u1 + g3*r1*u2 - g3*r2*u1)) except: t = 0 G = Vector(c1 + t*g1,c2 + t*g2,c3 + t*g3) #delky trojuhelniku a = face.V[0].Position.pointLen(face.V[1].Position) b = face.V[0].Position.pointLen(face.V[2].Position) c = face.V[1].Position.pointLen(face.V[2].Position) #bod nalezi trojuhelniku if (G.pointLen(face.V[0].Position) <= a and G.pointLen(face.V[0].Position) <= b and G.pointLen(face.V[0].Position) <= c and G.pointLen(face.V[1].Position) <= a and G.pointLen(face.V[1].Position) <= b and G.pointLen(face.V[1].Position) <= c and G.pointLen(face.V[2].Position) <= a and G.pointLen(face.V[2].Position) <= b and G.pointLen(face.V[2].Position) <= c): return G return None
def Update(self): Cube.Update(self) if (self.tim >= self.timeCount): self.tim = 0 x = random.uniform(-2, 2) y = random.uniform(1, 2) z = random.uniform(-2, 2) Object3D.addForce(self, Force(0,y,0)) Object3D.addForce(self, Force(x,0,0,2)) self.tim += self.deltaTime if (self.Position.x <= -1000): self.removeAll() self.Position = Vector(0,0,0) if (self.Position.y <= -1000): self.t = 0 self.removeAll() self.Position = Vector(0,0,0) if (self.Position.x >= 1000): self.removeAll() self.Position = Vector(0,0,0) if (self.Position.y >= 1000): self.removeAll() self.Position = Vector(0,0,0)
def findG(self, camera): """c1 = self.Position.x c2 = self.Position.y c3 = self.Position.z u1 = camera.u.x u2 = camera.u.y u3 = camera.u.z r1 = camera.r.x r2 = camera.r.y r3 = camera.r.z s1 = camera.S.x s2 = camera.S.y s3 = camera.S.z""" if (self.isInFront): g1 = camera.Position.x - self.Position.x g2 = camera.Position.y - self.Position.y g3 = camera.Position.z - self.Position.z else: g1 = self.Position.x g2 = 0 g3 = self.Position.z #miluju matlab try: t = (-(self.Position.x*camera.r.y*camera.u.z - self.Position.x*camera.r.z*camera.u.y - self.Position.y*camera.r.x*camera.u.z + self.Position.y*camera.r.z*camera.u.x + self.Position.z*camera.r.x*camera.u.y - self.Position.z*camera.r.y*camera.u.x + camera.r.x*camera.S.y*camera.u.z - camera.r.x*camera.S.z*camera.u.y - camera.r.y*camera.S.x*camera.u.z + camera.r.y*camera.S.z*camera.u.x + camera.r.z*camera.S.x*camera.u.y - camera.r.z*camera.S.y*camera.u.x)/ (g1*camera.r.y*camera.u.z - g1*camera.r.z*camera.u.y - g2*camera.r.x*camera.u.z + g2*camera.r.z*camera.u.x + g3*camera.r.x*camera.u.y - g3*camera.r.y*camera.u.x)) except: t = 0 #speak #print((c1 + t*g1, c2 + t*g2, c3 + t*g3)) return Vector(self.Position.x + t*g1, self.Position.y + t*g2, self.Position.z + t*g3)
def createVertices(self): self.mesh.verticesData = [] self.mesh.triangles = [] for x in range(self.width): for z in range(self.height): self.mesh.verticesData.append( Vertex( Vector(self.Position.x + x, self.Position.y, self.Position.z + z))) i = 0 for x in range(self.width - 1): for z in range(self.height): if (i + self.width + 1 < len(self.mesh.verticesData)): self.mesh.createTriangle(i + 1, i, i + self.width) self.mesh.createTriangle(i + 1, i + self.width, i + self.width + 1) i += 1 self.mesh.scale = self.scale
def __init__(self, position, w, h): self.x = w #poloha na obrazovce self.y = h #poloha na obrazovce self.Position = position self.speed = 0.1 self.viewAngle = math.pi self.maxView = 1000 self.far = 1 self.f = Vector(0, 0, self.far) self.u = Vector(0, self.far, 0) self.r = Vector(self.far, 0, 0) self.S = self.Position #predek self.rotX = math.pi self.rotY = 0
def __init__(self, engine, x, y, z, w, h): Object3D.__init__(self, engine, x, y, z) self.x = w #poloha na obrazovce self.y = h #poloha na obrazovce self.speed = 0.1 self.viewAngle = math.pi self.maxView = 1000 self.far = 1 self.f = Vector(0, 0, self.far) self.u = Vector(0, self.far, 0) self.r = Vector(self.far, 0, 0) self.S = self.Position #predek self.rotX = 0 self.rotY = 0
def keys(self, keys): if (keys[K_w]): #dopredu self.Position = self.Position.addVector( self.f.multipleVector(self.speed)) elif (keys[K_s]): #dozadu self.Position = self.Position.addVector( self.f.multipleVector(self.speed).reverseVector()) if (keys[K_d]): #doprava self.Position = self.Position.addVector( self.r.multipleVector(self.speed).reverseVector()) elif (keys[K_a]): #doleva self.Position = self.Position.addVector( self.r.multipleVector(self.speed)) if (keys[K_SPACE]): #nahoru self.Position = self.Position.addVector( self.u.multipleVector(self.speed)) elif (keys[K_LSHIFT]): #dolu self.Position = self.Position.addVector( self.u.multipleVector(self.speed).reverseVector()) if (keys[K_q]): #snizeni rychlosti if (self.speed - 0.01 > 0): self.speed -= 0.01 print("speed: " + str(self.speed)) elif (keys[K_e]): #zvyseni rychlosti self.speed += 0.01 print("speed: " + str(self.speed)) elif (keys[K_r]): #reset rychlosti self.speed = 0.1 print("speed: " + str(self.speed)) if (keys[K_g]): #reset kamery self.Position = Vector(0, 0, 0) self.f = Vector(0, 0, self.far) self.u = Vector(0, self.far, 0) self.r = Vector(self.far, 0, 0) self.rotX = 0 self.rotY = 0
def getCenter(self): if (len(self.V) >= 3): S1 = Vector((self.V[0].Position.x + self.V[1].Position.x)/2, (self.V[0].Position.y + self.V[1].Position.y)/2, (self.V[0].Position.z + self.V[1].Position.z)/2) S2 = Vector((self.V[0].Position.x + self.V[2].Position.x)/2, (self.V[0].Position.y + self.V[2].Position.y)/2, (self.V[0].Position.z + self.V[2].Position.z)/2) v = S1.newVector(self.V[2].Position) u = S2.newVector(self.V[2].Position) #matlab try: t = ((S1.y*u.z - S1.z*u.y - S2.y*u.z + S2.z*u.y) /(u.y*v.z - u.z*v.y)) except: t = 0 self.center = Vector(S1.x + t*v.x, S1.y + t*v.y, S2.z + t*v.z)
def update(self): self.head.Position = Vector(self.Position.x, self.Position.y + self.scale, self.Position.z) self.peak.Position = Vector(self.Position.x, self.Position.y + 1.1, self.Position.z + 0.3) self.legL.Position = Vector(self.Position.x - 0.3, self.Position.y - 0.9, self.Position.z + 0.7) self.legP.Position = Vector(self.Position.x + 0.3, self.Position.y - 0.9, self.Position.z + 0.7) self.handL.Position = Vector(self.Position.x - 0.55, self.Position.y, self.Position.z) self.handP.Position = Vector(self.Position.x + 0.55, self.Position.y, self.Position.z) self.body.Position = self.Position
def __init__(self, position, scale, engine, color=(255, 0, 255)): Object.__init__(self, position) self.scale = scale self.body = Object(position) self.body.loadMesh("Engine/meshes/penguin/penguinBody.json", scale) self.head = Object(Vector(position.x, position.y + 1, position.z)) self.head.loadMesh("Engine/meshes/penguin/penguinHead.json", scale) self.peak = Object( Vector(position.x, position.y + 1.1, position.z + 0.3)) self.peak.loadMesh("Engine/meshes/penguin/penguinPeak.json", scale) self.legL = Object( Vector(position.x - 0.3, position.y - 0.9, position.z + 0.7)) self.legL.loadMesh("Engine/meshes/penguin/penguinLeg.json", scale) self.legP = Object( Vector(position.x + 0.3, position.y - 0.9, position.z + 0.7)) self.legP.loadMesh("Engine/meshes/penguin/penguinLeg.json", scale) self.handL = Object(Vector(position.x - 0.55, position.y, position.z)) self.handL.loadMesh("Engine/meshes/penguin/penguinHand.json", scale) self.handP = Object(Vector(position.x + 0.55, position.y, position.z)) self.handP.loadMesh("Engine/meshes/penguin/penguinHand.json", scale) engine.addObject(self.body) engine.addObject(self.head) engine.addObject(self.peak) engine.addObject(self.legL) engine.addObject(self.legP) engine.addObject(self.handL) engine.addObject(self.handP)
def __init__(self, position, rotation=Vector(0,0,0)): self.Position = position self.Rotation = rotation self.mesh = None
def faces(self): return [ Face(Vector(1, -2, 1), Vector(1, -2, -1), Vector(-1, -2, -1), self.black), Face(Vector(1, -2, 1), Vector(-1, -2, 1), Vector(-1, -2, -1), self.black), Face(Vector(1, 1, 1), Vector(1, 1, -1), Vector(-1, 1, -1), self.black), Face(Vector(1, 1, 1), Vector(-1, 1, 1), Vector(-1, 1, -1), self.black), Face(Vector(1, -2, 1), Vector(1, 1, 1), Vector(1, -2, -1), self.black), Face(Vector(1, 1, 1), Vector(1, 1, -1), Vector(1, -2, -1), self.black), Face(Vector(-1, -2, 1), Vector(-1, 1, 1), Vector(-1, -2, -1), self.black), Face(Vector(-1, 1, 1), Vector(-1, 1, -1), Vector(-1, -2, -1), self.black), Face(Vector(1, -2, -1), Vector(-1, -2, -1), Vector(-1, -1, -1), self.black), Face(Vector(1, -2, -1), Vector(-1, -1, -1), Vector(1, -1, -1), self.black), Face(Vector(0.5, 1, -1), Vector(0.5, -1, -1), Vector(1, 1, -1), self.black), Face(Vector(1, -1, -1), Vector(0.5, -1, -1), Vector(1, 1, -1), self.black), Face(Vector(-0.5, 1, -1), Vector(-0.5, -1, -1), Vector(-1, 1, -1), self.black), Face(Vector(-1, -1, -1), Vector(-0.5, -1, -1), Vector(-1, 1, -1), self.black), Face(Vector(-0.5, 1, -1), Vector(-0.5, 0, -1), Vector(0.5, 1, -1), self.black), Face(Vector(-0.5, 0, -1), Vector(0.5, 0, -1), Vector(0.5, 1, -1), self.black), Face(Vector(-0.25, -0.1, -1.25), Vector(-0.25, -0.1, -1), Vector(-0.25, 0.1, -1), self.yellow), Face(Vector(-0.25, -0.1, -1.25), Vector(-0.25, 0.1, -1), Vector(-0.25, 0.1, -1.25), self.yellow), Face(Vector(0.25, -0.1, -1.25), Vector(0.25, -0.1, -1), Vector(0.25, 0.1, -1), self.yellow), Face(Vector(0.25, -0.1, -1.25), Vector(0.25, 0.1, -1), Vector(0.25, 0.1, -1.25), self.yellow), Face(Vector(0.25, 0.1, -1.25), Vector(0.25, 0.1, -1), Vector(-0.25, 0.1, -1), self.yellow), Face(Vector(0.25, 0.1, -1.25), Vector(-0.25, 0.1, -1), Vector(-0.25, 0.1, -1.25), self.yellow), Face(Vector(0.25, 0.1, -1.25), Vector(0.25, -0.1, -1.25), Vector(-0.25, 0.1, -1.25), self.yellow), Face(Vector(-0.25, -0.1, -1.25), Vector(-0.25, 0.1, -1.25), Vector(0.25, -0.1, -1.25), self.yellow), Face(Vector(0.30, 0.15, -1.01), Vector(0.30, 0.30, -1.01), Vector(0.45, 0.3, -1.01), self.white), Face(Vector(0.30, 0.15, -1.01), Vector(0.45, 0.15, -1.01), Vector(0.45, 0.3, -1.01), self.white), Face(Vector(-0.30, 0.15, -1.01), Vector(-0.30, 0.30, -1.01), Vector(-0.45, 0.3, -1.01), self.white), Face(Vector(-0.30, 0.15, -1.01), Vector(-0.45, 0.15, -1.01), Vector(-0.45, 0.3, -1.01), self.white), ]