def getClosestAxis(normal): absNormal = Vec3(abs(normal.x), abs(normal.y), abs(normal.z)) if absNormal.almostEqual(Vec3.unitX(), 0.5): return Vec3.unitX() elif absNormal.almostEqual(Vec3.unitY(), 0.5): return Vec3.unitY() else: return Vec3.unitZ()
def ik_leg(self): floor_pos = self.get_floor_spot() target_pos = self._get_target_pos() hip_pos = self.hip_bone.get_pos(self.foot_ref) if floor_pos and target_pos.y < floor_pos.y: floor_pos.x = target_pos.x target_vector = hip_pos - floor_pos self.is_on_ground = True else: target_vector = hip_pos - target_pos self.is_on_ground = False #turn off the ground for debugging #target_vector = hip_pos - target_pos pt_length = target_vector.length() if .01 < pt_length < (TOP_LEG_LENGTH + BOTTOM_LEG_LENGTH +.1): tt_angle_cos = (math.pow(TOP_LEG_LENGTH, 2) + math.pow(pt_length, 2) - math.pow(BOTTOM_LEG_LENGTH,2))/(2*TOP_LEG_LENGTH*pt_length) try: target_top_angle = rad2Deg(math.acos(tt_angle_cos)) except ValueError: return target_vector.normalize() self.hip_bone.get_relative_vector(self.foot_ref, target_vector) delta = target_vector.get_xy().signed_angle_deg(Vec3.unitY().get_xy()) self.top_bone.set_p(90 - target_top_angle + delta) tb_angle_cos = ((math.pow(TOP_LEG_LENGTH, 2) + math.pow(BOTTOM_LEG_LENGTH, 2) - math.pow(pt_length,2))/(2*TOP_LEG_LENGTH*BOTTOM_LEG_LENGTH)) target_bottom_angle = rad2Deg(math.acos(tb_angle_cos)) self.bottom_bone.set_p((180 - target_bottom_angle) * -1) else: return
def __build_ocean_mesh(self): vdata = GeomVertexData('data', GeomVertexFormat.getV3n3c4t2(), Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') texcoord = GeomVertexWriter(vdata, 'texcoord') color = GeomVertexWriter(vdata, 'color') axes = [Vec3.unitX(), Vec3.unitY(), Vec3.unitZ()] face = 0 for x in range(3): for s in [-1, 1]: for i in range(self.__n + 1): for j in range(self.__n + 1): a = (i * 1.0 / self.__n) * (pi / 2) - (pi / 4) b = (j * 1.0 / self.__n) * (pi / 2) - (pi / 4) xAxis = axes[(x + 3) % 3] yAxis = axes[(x + 4) % 3] zAxis = axes[(x + 5) % 3] v = (xAxis * (-cos(a) * sin(b)) + yAxis * (sin(a) * cos(b)) + zAxis * (cos(a) * cos(b))) * s v.normalize() normal.addData3f(v) vertex.addData3f(v * self.__radius) texcoord.addData2f(i * 1.0, j * 1.0) color.addData4f(self.__ocean_color) face = face + 1 prim = self.__heightmap_primitive() geom = Geom(vdata) geom.addPrimitive(prim) return geom
def __init__(self): ShowBase.__init__(self) self.disableMouse() self.keyMap = { "up": False, "down": False, "right": False, "left": False, "sink": False, "rise": False } self.keymapping = zip( (",", "a", "e", "o", "lshift", "space"), # ("w", "a", "s", "d", "lshift", "space"), ("up", "left", "right", "down", "sink", "rise")) for key, keyname in self.keymapping: self.accept(key, self.updateKeyMap, [keyname, True]) self.accept(key + "-up", self.updateKeyMap, [keyname, False]) self.accept('escape', sys.exit, [0]) self.movementDirs = [("up", Vec3.unitY()), ("left", -Vec3.unitX()), ("right", Vec3.unitX()), ("down", -Vec3.unitY())] self.plane = self.loader.loadModel("models/plane.bam") self.plane.reparentTo(self.render) self.cube = self.loader.loadModel("models/cube.bam") self.cube.reparentTo(self.render) self.cTrav = CollisionTraverser() self.camera.setPos(0, -20, 3) self.cube.setPos(0, 0, 1) self.camera.lookAt(self.cube) self.taskMgr.add(self.mouseWatchTask, "Mouse Watch Task") self.taskMgr.add(self.keyboardWatchTask, "Keyboard Watch Task") self.debugText = self.genLabelText("", 1) wp = WindowProperties() wp.setMouseMode(WindowProperties.M_relative) wp.setCursorHidden(True) self.win.requestProperties(wp)
def getClosestAxisToNormal(self): # VHE Prioritizes the axes in order of X, Y, Z norm = self.getNormal() norm[0] = abs(norm[0]) norm[1] = abs(norm[1]) norm[2] = abs(norm[2]) if norm.x >= norm.y and norm.x >= norm.z: return Vec3.unitX() if norm.y >= norm.z: return Vec3.unitY() return Vec3.unitZ()
def getCirclesBorder(cls, circle, numSlices=32, closed=False): degPerSlice = 360.0 / numSlices zVec = Vec3.unitZ() rotMat = Mat3.rotateMat(degPerSlice, zVec) projVec = Vec3.unitY() + circle.center projVec.normalize() border = [ Point3(projVec * circle.radius + circle.center), ] projVec = rotMat.xform(projVec) for i in range(1, numSlices): pt = Point3(projVec * circle.radius + circle.center) border.append(pt) projVec = rotMat.xform(projVec) if closed and border: border.append(border[0]) return border
def __build_land_mesh(self): vdata = GeomVertexData('data', GeomVertexFormat.getV3n3c4t2(), Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') texcoord = GeomVertexWriter(vdata, 'texcoord') color = GeomVertexWriter(vdata, 'color') axes = [Vec3.unitX(), Vec3.unitY(), Vec3.unitZ()] face = 0 for x in range(3): for s in [-1, 1]: for i in range(self.__n + 1): for j in range(self.__n + 1): a = (i * 1.0 / self.__n) * (pi / 2) - (pi / 4) b = (j * 1.0 / self.__n) * (pi / 2) - (pi / 4) xAxis = axes[(x + 3) % 3] yAxis = axes[(x + 4) % 3] zAxis = axes[(x + 5) % 3] v = (xAxis * (-cos(a) * sin(b)) + yAxis * (sin(a) * cos(b)) + zAxis * (cos(a) * cos(b))) * s v.normalize() normal.addData3f(v) index = self.__height_i(face, i, j) v = v * ( 1.0 + (self.__height_factor * self.__height_unit * (self.__heightmap[index] - 0.5))) * self.__radius vertex.addData3f(v) texcoord.addData2f(i * 1.0, j * 1.0) c = self.__gradient(self.__color_gradient, self.__heightmap[index]) color.addData4f(c[0] / 255.0, c[1] / 255.0, c[2] / 255.0, 1.0) face = face + 1 prim = self.__heightmap_primitive() geom = Geom(vdata) geom.addPrimitive(prim) return geom