def boardToLevelCoord(width, length, coordinate): coord = vector.tVector3(0.0, 0.0, 0.0) # create a new vector xzero = -0.5*width + 0.5*SQUARE_SIZE # 3D x-coordinate for 2D coordinate x = 0 zzero = (0.5*length - 0.5*SQUARE_SIZE) # 3D z-coordinate for 2D coordinate y = 0 toAdd = vector.tVector3(xzero + (coordinate['x'] ), 0.0, zzero - (coordinate['y'] )) coord = coord + toAdd return coord
def initGraphics(): pygame.init() # constants global SCREEN_SIZE SCREEN_SIZE = (1024,600) # screen size when not fullscreen global DISPLAY_FLAGS DISPLAY_FLAGS = OPENGL|DOUBLEBUF #|FULLSCREEN # display settings global INIT_DIRECTION INIT_DIRECTION = vector.tVector3(1.0, 0.0, 0.0) # inital bomberman direction # global variables global pos pos = vector.tVector3(0, 0, 0) # Position Vector Camera global view view = vector.tVector3(0, 0, 0) # View Vector Camera global up up = vector.tVector3(0, 0, 0) # Up Vector Camera global bdirection bdirection = vector.tVector3(0.0, 0.0, 0.0) # direction of bomberman global angle angle = 0.0 # angle for rotating the bomberman # dispplay settings os.environ["SDL_VIDEO_CENTERED"] = "1" # Center the graphics window. global screen screen = pygame.display.set_mode(SCREEN_SIZE,DISPLAY_FLAGS) pygame.display.set_caption("Bomb3D v"+__version__)
def boardToLevelCoord(width, length, coordinate): coord = vector.tVector3(0.0, 0.0, 0.0) # create a new vector xzero = -0.5 * width + 0.5 * SQUARE_SIZE # 3D x-coordinate for 2D coordinate x = 0 zzero = (0.5 * length - 0.5 * SQUARE_SIZE ) # 3D z-coordinate for 2D coordinate y = 0 toAdd = vector.tVector3(xzero + (coordinate['x']), 0.0, zzero - (coordinate['y'])) coord = coord + toAdd return coord
def setCamera(graph, coordinate, direction): pos = vector.tVector3(0.0, 0.0, 0.0) view = vector.tVector3(0.0, 0.0, 0.0) up = vector.tVector3(0.0, 1.0, 0.0) pos.x = coordinate.x - 25*direction['x'] pos.y = 4.0 pos.z = coordinate.z - 25*direction['y'] view.x = coordinate.x + direction['x'] view.y = 4.0 view.z = coordinate.z + direction['y'] graph.camera.Position_Camera(pos.x, pos.y, pos.z, view.x, view.y, view.z, up.x, up.y, up.z)
def setCamera(graph, coordinate, direction): pos = vector.tVector3(0.0, 0.0, 0.0) view = vector.tVector3(0.0, 0.0, 0.0) up = vector.tVector3(0.0, 1.0, 0.0) pos.x = coordinate.x - 25 * direction['x'] pos.y = 4.0 pos.z = coordinate.z - 25 * direction['y'] view.x = coordinate.x + direction['x'] view.y = 4.0 view.z = coordinate.z + direction['y'] graph.camera.Position_Camera(pos.x, pos.y, pos.z, view.x, view.y, view.z, up.x, up.y, up.z)
def Rotate_View(self, angle, x, y, z): vNewView = vector.tVector3(0, 0, 0) # Get the view vector (The direction we are facing) vView = self.mView - self.mPos # Calculate the sine and cosine of the angle once cosTheta = math.cos(angle) sinTheta = math.sin(angle) # Find the new x position for the new rotated point vNewView.x = (cosTheta + (1 - cosTheta) * x * x) * vView.x vNewView.x = vNewView.x + ( (1 - cosTheta) * x * y - z * sinTheta) * vView.y vNewView.x = vNewView.x + ( (1 - cosTheta) * x * z + y * sinTheta) * vView.z # Find the new y position for the new rotated point vNewView.y = ((1 - cosTheta) * x * y + z * sinTheta) * vView.x vNewView.y = vNewView.y + (cosTheta + (1 - cosTheta) * y * y) * vView.y vNewView.y = vNewView.y + ( (1 - cosTheta) * y * z - x * sinTheta) * vView.z # Find the new z position for the new rotated point vNewView.z = ((1 - cosTheta) * x * z - y * sinTheta) * vView.x vNewView.z = vNewView.z + ( (1 - cosTheta) * y * z + x * sinTheta) * vView.y vNewView.z = vNewView.z + (cosTheta + (1 - cosTheta) * z * z) * vView.z # Now we just add the newly rotated vector to our position to set # our new rotated view of our camera. self.mView = self.mPos + vNewView
def Rotate_View(self, angle, x, y, z): vNewView = vector.tVector3(0,0,0) # Get the view vector (The direction we are facing) vView = self.mView - self.mPos # Calculate the sine and cosine of the angle once cosTheta = math.cos(angle) sinTheta = math.sin(angle) # Find the new x position for the new rotated point vNewView.x = (cosTheta + (1 - cosTheta) * x * x) * vView.x vNewView.x = vNewView.x + ((1 - cosTheta) * x * y - z * sinTheta) * vView.y vNewView.x = vNewView.x + ((1 - cosTheta) * x * z + y * sinTheta) * vView.z # Find the new y position for the new rotated point vNewView.y = ((1 - cosTheta) * x * y + z * sinTheta) * vView.x vNewView.y = vNewView.y + (cosTheta + (1 - cosTheta) * y * y) * vView.y vNewView.y = vNewView.y + ((1 - cosTheta) * y * z - x * sinTheta) * vView.z # Find the new z position for the new rotated point vNewView.z = ((1 - cosTheta) * x * z - y * sinTheta) * vView.x vNewView.z = vNewView.z + ((1 - cosTheta) * y * z + x * sinTheta) * vView.y vNewView.z = vNewView.z + (cosTheta + (1 - cosTheta) * z * z) * vView.z # Now we just add the newly rotated vector to our position to set # our new rotated view of our camera. self.mView = self.mPos + vNewView
def Strafe_Camera(self, speed): vVector = self.mView - self.mPos # Get the view vector vOrthoVector = vector.tVector3(0.0, 0.0, 0.0) # Orthogonal vector for the view vector vOrthoVector.x = -vVector.z vOrthoVector.z = vVector.x # left positive cameraspeed and right negative -cameraspeed. self.mPos.x = self.mPos.x + vOrthoVector.x * speed self.mPos.z = self.mPos.z + vOrthoVector.z * speed self.mView.x = self.mView.x + vOrthoVector.x * speed self.mView.z = self.mView.z + vOrthoVector.z * speed
def GetCollisionOffset(vNormal, radius, distance): vOffset = vector.tVector3(0, 0, 0) # Once we find if a collision has taken place, we need to make sure the sphere # doesn't move into the wall. In our app, the position will actually move into # the wall, but we check our collision detection before we render the scene, which # eliminates the bounce back effect it would cause. The question is, how do we # know which direction to move the sphere back? In our collision detection, we # account for collisions on both sides of the polygon. Usually, you just need # to worry about the side with the normal vector and positive distance. If # you don't want to back face cull and have 2 sided planes, I check for both sides. # Let me explain the math that is going on here. First, we have the normal to # the plane, the radius of the sphere, as well as the distance the center of the # sphere is from the plane. In the case of the sphere colliding in the front of # the polygon, we can just subtract the distance from the radius, then multiply # that new distance by the normal of the plane. This projects that leftover # distance along the normal vector. For instance, say we have these values: # vNormal = (1, 0, 0) radius = 5 distance = 3 # If we subtract the distance from the radius we get: (5 - 3 = 2) # The number 2 tells us that our sphere is over the plane by a distance of 2. # So basically, we need to move the sphere back 2 units. How do we know which # direction though? This part is easy, we have a normal vector that tells us the # direction of the plane. # If we multiply the normal by the left over distance we get: (2, 0, 0) # This new offset vectors tells us which direction and how much to move back. # We then subtract this offset from the sphere's position, giving is the new # position that is lying right on top of the plane. Ba da bing! # If we are colliding from behind the polygon (not usual), we do the opposite # signs as seen below: # If our distance is greater than zero, we are in front of the polygon if (distance > 0): # Find the distance that our sphere is overlapping the plane, then # find the direction vector to move our sphere. distanceOver = radius - distance vOffset = vNormal * distanceOver else: # Else colliding from behind the polygon # Find the distance that our sphere is overlapping the plane, then # find the direction vector to move our sphere. distanceOver = radius + distance vOffset = vNormal * -distanceOver # There is one problem with check for collisions behind the polygon, and that # is if you are moving really fast and your center goes past the front of the # polygon, it will then assume you were colliding from behind and not let # you back in. Most likely you will take out the if / else check, but I # figured I would show both ways in case someone didn't want to back face cull. # Return the offset we need to move back to not be intersecting the polygon. return vOffset
def Strafe_Camera(self, speed): vVector = self.mView - self.mPos # Get the view vector vOrthoVector = vector.tVector3( 0.0, 0.0, 0.0) # Orthogonal vector for the view vector vOrthoVector.x = -vVector.z vOrthoVector.z = vVector.x # left positive cameraspeed and right negative -cameraspeed. self.mPos.x = self.mPos.x + vOrthoVector.x * speed self.mPos.z = self.mPos.z + vOrthoVector.z * speed self.mView.x = self.mView.x + vOrthoVector.x * speed self.mView.z = self.mView.z + vOrthoVector.z * speed
def Cross(vVector1,vVector2): vNormal = vector.tVector3(0,0,0) # The vector to hold the cross product # The X value for the vector is: (V1.y * V2.z) - (V1.z * V2.y) vNormal.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y)) # The Y value for the vector is: (V1.z * V2.x) - (V1.x * V2.z) vNormal.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z)) # The Z value for the vector is: (V1.x * V2.y) - (V1.y * V2.x) vNormal.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x)) return vNormal # Return the cross product (Direction the polygon is facing - Normal)
def Cross(vVector1, vVector2): vNormal = vector.tVector3(0, 0, 0) # The vector to hold the cross product # The X value for the vector is: (V1.y * V2.z) - (V1.z * V2.y) vNormal.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y)) # The Y value for the vector is: (V1.z * V2.x) - (V1.x * V2.z) vNormal.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z)) # The Z value for the vector is: (V1.x * V2.y) - (V1.y * V2.x) vNormal.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x)) return vNormal # Return the cross product (Direction the polygon is facing - Normal)
def LoadVertices(): global g_NumberOfVerts # This function reads in the vertices from an ASCII text file (World.raw). # First, we read in the vertices with a temp CVector3 to get the number of them. # Next, we rewind the file pointer and then actually read in the vertices into # our allocated CVector3 array g_vWorld[]. # Create a file and load the model from a file of vertices fp = open(FILE_NAME, "r") # Read in the vertices that are stored in the file arr = [] for line in fp.readlines(): # Read line after line arr.append(map(float, line.split())) # Map the vertices into a 2-dim array new = vector.tVector3(arr[g_NumberOfVerts][0], arr[g_NumberOfVerts][1], arr[g_NumberOfVerts][2]) g_vWorld.append(new) g_NumberOfVerts = g_NumberOfVerts + 1 # Close our file because we are done fp.close()
def __init__(self): self.mPos = vector.tVector3(0.0, 0.0, 0.0) self.mView = vector.tVector3(0.0, 0.0, 0.0) self.mUp = vector.tVector3(0.0, 0.0, 0.0) self.mStrafe = vector.tVector3(0.0, 0.0, 0.0) self.mRadius = 1
def Position_Camera(self, pos_x, pos_y, pos_z, view_x, view_y, view_z, up_x, up_y, up_z): self.mPos = vector.tVector3(pos_x, pos_y, pos_z) # set position self.mView = vector.tVector3(view_x, view_y, view_z) # set view self.mUp = vector.tVector3(up_x, up_y, up_z) # set the up vector
DISPLAY_FLAGS = PyGLoc.OPENGL|PyGLoc.DOUBLEBUF FILE_NAME = "World.raw" MAP = [] MAP.append((1,1,1,1,1)) MAP.append((1,2,2,0,1)) MAP.append((1,2,1,0,1)) MAP.append((1,0,0,0,0)) MAP.append((1,1,1,1,1)) # player location: x,y,angle,color PLAYERS = [(1.5,1.5,50,(255,0,0)),(6.5,7.5,280,(0,255,0))] # global variables objCamera = camera.CCamera() # Our Camera pos = vector.tVector3(0, 0, 0) # Vector Camera Position view = vector.tVector3(0, 0, 0) # View Vector Camera up = vector.tVector3(0, 0, 0) # Up Vector Camera g_vWorld = [] # Array containing all the triangles our world consists of g_NumberOfVerts = 0 # Number of vertices used in our world # Load the vertices specified in our world file def LoadVertices(): global g_NumberOfVerts # This function reads in the vertices from an ASCII text file (World.raw). # First, we read in the vertices with a temp CVector3 to get the number of them. # Next, we rewind the file pointer and then actually read in the vertices into # our allocated CVector3 array g_vWorld[]. # Create a file and load the model from a file of vertices fp = open(FILE_NAME, "r")
from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from math import floor, pi import math3D import vector import pygame pygame.init() SCREEN_SIZE = (640,480) # screen size when not fullscreen SQUARE_SIZE = 80 MAP_SCREEN_RATIO = 3; INIT_DIRECTION = vector.tVector3(0.0, 1.0, 0.0) # inital bomberman direction # global variables bdirection = vector.tVector3(0.0, 0.0, 0.0) # direction of bomberman angle = 0.0 class HUD(): # The HUD def __init__(self, textures): self.visuals = textures def Draw_Mini_Map(self, gamestate): self.HudMode(True) # map size real_map_width = float(gamestate.level.size['x']) real_map_height = float(gamestate.level.size['y']) relative_map_width = gamestate.level.size['x'] / SQUARE_SIZE
from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from math import floor, pi import math3D import vector import pygame pygame.init() SCREEN_SIZE = (640, 480) # screen size when not fullscreen SQUARE_SIZE = 80 MAP_SCREEN_RATIO = 3 INIT_DIRECTION = vector.tVector3(0.0, 1.0, 0.0) # inital bomberman direction # global variables bdirection = vector.tVector3(0.0, 0.0, 0.0) # direction of bomberman angle = 0.0 class HUD(): # The HUD def __init__(self, textures): self.visuals = textures def Draw_Mini_Map(self, gamestate): self.HudMode(True) # map size real_map_width = float(gamestate.level.size['x']) real_map_height = float(gamestate.level.size['y'])