def __init__(self, filename, texture, scale=1.0, mass=1, overdraw_scale=1.0): if not TextureManager.getInstance().containsTexture(texture): TextureManager.getInstance().addTexture(texture, Texture(texture)) if (filename, scale) not in MD2._shapes: shape = Loader.loadMD2(filename, scale) shape.rotateAxis(SimpleVector(1, 0, 0), math.pi/2) shape.rotateAxis(SimpleVector(0, 0, 1), -math.pi/2) shape.rotateMesh() shape.rotationMatrix = Matrix() MD2._shapes[(filename, scale)] = shape # self.shape = MD2._shapes[(filename, scale)].cloneObject() self.shape = Object3D(MD2._shapes[(filename, scale)], False) self.shape.build() self.shape.setTexture(texture) minx, maxx, miny, maxy, minz, maxz = self.shape.mesh.boundingBox shape = BoxShape(Vector3f((maxx - minx) / overdraw_scale / 2.0, (maxy - miny) / overdraw_scale / 2.0, (maxz - minz) / 2.0)) t = Transform() t.setIdentity() t.origin.x = (maxx + minx) / 2 t.origin.y = (maxy + miny) / 2 t.origin.z = (maxz + minz) / 2 ms = DefaultMotionState(t) inertia = Vector3f(0, 0, 0) shape.calculateLocalInertia(mass, inertia) rb = RigidBodyConstructionInfo(mass, ms, shape, inertia) self.physics = RigidBody(rb) self.shape.setOrigin(SimpleVector(-(maxx + minx) / 2, -(maxy + miny) / 2, -(maxz + minz) / 2))
def add_sphere_at(self, x, y, z, radius, color, room): s = Primitives.getSphere(radius) colorname = 'added_sphere_%d' % id(s) TextureManager.getInstance().addTexture( colorname, Texture(1, 1, color)) s.setTexture(colorname) s.build() if not hasattr(self, 'extra_shapes'): self.extra_shapes = [] self.extra_shapes.append((s, x, y, z)) room.world.addObject(s)
def __init__(self, r, mass=1, color=Color.red): colorname = 'sphere_%d' % id(self) self.shape = Primitives.getSphere(r) TextureManager.getInstance().addTexture( colorname, Texture(1, 1, color)) self.shape.setTexture(colorname) self.shape.build() shape = BoxShape(Vector3f(r / 2.0, r / 2.0, r / 2.0)) inertia = Vector3f(0, 0, 0) shape.calculateLocalInertia(mass, inertia) t = Transform() t.setIdentity() ms = DefaultMotionState(t) rb = RigidBodyConstructionInfo(mass, ms, shape, inertia) self.physics = RigidBody(rb)
def __init__(self, x, y, z, mass=1, scale=1, draw_as_cylinder=False, color=Color.blue, overdraw_length=1, overdraw_radius=1, flat_shading=False): if draw_as_cylinder: if y is max(x, y, z): radius = (x + z) / 4 * overdraw_radius self.shape = Primitives.getCylinder( 90, radius, y / (radius * 2) * overdraw_length) if z is max(x, y, z): radius = (x + y) / 4 * overdraw_radius self.shape = Primitives.getCylinder( 90, radius, z / (radius * 2) * overdraw_length) self.shape.rotateX(math.pi / 2) self.shape.rotateMesh() else: self.shape = createBox(x / 2.0 * scale, y / 2.0 * scale, z / 2.0 * scale) if flat_shading: self.shape.shadingMode = Object3D.SHADING_FAKED_FLAT colorname = 'box%d' % id(self) TextureManager.getInstance().addTexture(colorname, Texture(1, 1, color)) self.shape.setTexture(colorname) self.shape.build() shape = BoxShape(Vector3f(x / 2.0, y / 2.0, z / 2.0)) inertia = Vector3f(0, 0, 0) shape.calculateLocalInertia(mass, inertia) t = Transform() t.setIdentity() ms = DefaultMotionState(t) rb = RigidBodyConstructionInfo(mass, ms, shape, inertia) self.physics = RigidBody(rb)
def __init__(self, x, y, z=1, gravity=10, color=[ Color(0xFFFFFF), Color(0xFFFFFF), Color(0xEEEEEE), Color(0xDDDDDD), Color(0xCCCCCC), Color(0xBBBBBB)], camera=(0, 0, 10), dt=0.0001): ccm.Model.__init__(self) self.size = (x, y, z) self.world = World() self.dt = dt brightness = 180 self.world.setAmbientLight(brightness, brightness, brightness) self.world.addLight(SimpleVector(2, 2, 5), 10, 10, 10) self.world.camera.setPosition(*camera) self.world.camera.setOrientation( SimpleVector(0, 0, -1), SimpleVector(0, -1, 0)) self.objects = [] collisionConfiguration = DefaultCollisionConfiguration() dispatcher = CollisionDispatcher(collisionConfiguration) overlappingPairCache = AxisSweep3( Vector3f(-x / 2.0, -y / 2.0, -z / 2.0), Vector3f(x / 2.0, y / 2.0, z / 2.0), 1024) solver = SequentialImpulseConstraintSolver() self.physics = DiscreteDynamicsWorld( dispatcher, overlappingPairCache, solver, collisionConfiguration) self.physics.setGravity(Vector3f(0, 0, -gravity)) t = Transform() t.setIdentity() rbi = RigidBodyConstructionInfo( 0, DefaultMotionState(t), StaticPlaneShape( Vector3f(0, 0, 1), 0), Vector3f(0, 0, 0)) self.physics.addRigidBody(RigidBody(rbi)) rbi = RigidBodyConstructionInfo( 0, DefaultMotionState(t), StaticPlaneShape( Vector3f(0, 1, 0), -y / 2.0), Vector3f(0, 0, 0)) self.physics.addRigidBody(RigidBody(rbi)) rbi = RigidBodyConstructionInfo( 0, DefaultMotionState(t), StaticPlaneShape( Vector3f(0, -1, 0), -y / 2.0), Vector3f(0, 0, 0)) self.physics.addRigidBody(RigidBody(rbi)) rbi = RigidBodyConstructionInfo( 0, DefaultMotionState(t), StaticPlaneShape( Vector3f(1, 0, 0), -x / 2.0), Vector3f(0, 0, 0)) self.physics.addRigidBody(RigidBody(rbi)) rbi = RigidBodyConstructionInfo( 0, DefaultMotionState(t), StaticPlaneShape( Vector3f(-1, 0, 0), -x / 2.0), Vector3f(0, 0, 0)) self.physics.addRigidBody(RigidBody(rbi)) self.vehicle_raycaster = DefaultVehicleRaycaster(self.physics) if not isinstance(color, (list, tuple)): room = createBox(x / 2.0, y / 2.0, z / 2.0) room.translate(0, 0, z / 2.0) room.invert() colorname = 'room%d' % id(self) TextureManager.getInstance().addTexture( colorname, Texture(1, 1, color)) room.setTexture(colorname) room.shadingMode = Object3D.SHADING_FAKED_FLAT room.build() self.world.addObject(room) else: room = createBoxPieces(x / 2.0, y / 2.0, z / 2.0) for i, r in enumerate(room): r.translate(0, 0, z / 2.0) r.invert() colorname = 'room%d_%d' % (id(self), i) TextureManager.getInstance().addTexture( colorname, Texture(1, 1, color[i % len(color)])) r.setTexture(colorname) r.shadingMode = Object3D.SHADING_FAKED_FLAT r.build() self.world.addObject(r) self.ratelimit = RateLimit()