Exemplo n.º 1
0
    def __init__(self, x, y, z, origin=(0, 0, 0), maximum=100, timestep=0.01):
        ccm.Model.__init__(self)
        self.dir = Vector3f(x, y, z)
        self.dir.normalize()
        self.dir.scale(maximum)

        self.origin = Vector3f(origin)

        self.range = 0
        self.timestep = timestep
Exemplo n.º 2
0
    def add(self, obj, x, y, z=None):
        if z is None:
            t = Transform()
            min = Vector3f()
            max = Vector3f()
            obj.physics.getAabb(min, max)
            z = -min.z
        self.temp = obj
        if hasattr(obj, 'physics'):
            t = Transform()
            ms = obj.physics.motionState
            ms.getWorldTransform(t)
            m = Matrix4f()
            t.getMatrix(m)
            m.m03 = x
            m.m13 = y
            m.m23 = z
            t.set(m)
            ms.worldTransform = t
            obj.physics.motionState = ms

            self.physics.addRigidBody(obj.physics)

            if hasattr(obj, 'wheels'):
                tuning = VehicleTuning()
                obj.vehicle = RaycastVehicle(tuning, obj.physics,
                                             self.vehicle_raycaster)
                obj.physics.setActivationState(
                    CollisionObject.DISABLE_DEACTIVATION)
                self.physics.addVehicle(obj.vehicle)
                obj.vehicle.setCoordinateSystem(1, 2, 0)

                for w in obj.wheels:
                    wheel = obj.vehicle.addWheel(Vector3f(w.x, w.y, w.z),
                                                 Vector3f(w.dir),
                                                 Vector3f(w.axle),
                                                 w.suspension_rest_len,
                                                 w.radius, tuning, False)
                    wheel.suspensionStiffness = w.suspension_stiffness
                    wheel.maxSuspensionTravelCm = w.suspension_max_travel * 100
                    wheel.frictionSlip = w.friction
                    wheel.wheelsDampingRelaxation = w.damping_relaxation
                    wheel.wheelsDampingCompression = w.damping_compression
                    wheel.rollInfluence = w.roll_influence

        self.objects.append(obj)
        self.world.addObject(obj.shape)
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    def update_neurons(self):
        while True:
            scale = 0.0003
            m1 = controlU.getOrigin('u1').getValues().getValues()[0] * scale
            m2 = controlU.getOrigin('u2').getValues().getValues()[0] * scale
            v1 = Vector3f(0, 0, 0)
            v2 = Vector3f(0, 0, 0)
            #java.lang.System.out.println("m1: %f   m2:%f"%(m1,m2))

            self.upperarm.physics.applyTorqueImpulse(Vector3f(0, 0, m1))
            self.lowerarm.physics.applyTorqueImpulse(Vector3f(0, 0, m2))

            self.hinge1 = -(self.shoulder.hingeAngle - pi / 2)
            self.hinge2 = -self.elbow.hingeAngle
            #java.lang.System.out.println("angle1: %f
            #angle2:%f"%(self.hinge1,self.hinge2))

            self.upperarm.physics.getAngularVelocity(v1)
            self.lowerarm.physics.getAngularVelocity(v2)
            # put bounds on the velocity possible
            if v1.z > 2:
                self.upperarm.physics.setAngularVelocity(Vector3f(0, 0, 2))
            if v1.z < -2:
                self.upperarm.physics.setAngularVelocity(Vector3f(0, 0, -2))
            if v2.z > 2:
                self.lowerarm.physics.setAngularVelocity(Vector3f(0, 0, 2))
            if v2.z < -2:
                self.lowerarm.physics.setAngularVelocity(Vector3f(0, 0, -2))
            self.upperarm.physics.getAngularVelocity(v1)
            self.lowerarm.physics.getAngularVelocity(v2)

            wt = Transform()
            #self.target.physics.motionState.getWorldTransform(wt)
            wt.setIdentity()

            tx = controlV.getTermination('inputRefX').input
            if tx is not None:
                wt.origin.x = tx.values[0] + 0.7
            else:
                wt.origin.x = 0.7
            ty = controlV.getTermination('inputRefY').input
            if ty is not None:
                wt.origin.y = ty.values[0] + 0.1
            else:
                wt.origin.y = 0.1
            wt.origin.z = 2

            ms = self.target.physics.motionState
            ms.worldTransform = wt
            self.target.physics.motionState = ms

            self.vel1 = v1.z
            self.vel2 = v2.z

            yield 0.0001
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    def start(self):
        while True:
            physics = self.parent.parent.physics

            t = Transform()
            self.parent.physics.getWorldTransform(t)
            pt1 = Vector3f(self.origin)
            pt2 = Vector3f(self.dir)
            t.transform(pt1)
            t.transform(pt2)

            callback = CollisionWorld.ClosestRayResultCallback(pt1, pt2)
            physics.rayTest(pt1, pt2, callback)

            delta = Vector3f(callback.hitPointWorld)
            delta.sub(pt1)
            self.range = delta.length()

            yield self.timestep
Exemplo n.º 7
0
    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))
Exemplo n.º 8
0
    def start(self):    
        
        self.target=space.Sphere(0.2,mass=1,color=Color(0xFF0000))
        self.add(self.target,0,0,2)
        
        torso=space.Box(0.1,0.1,1.5,mass=100000,draw_as_cylinder=True,
            color=Color(0x4444FF))
        self.add(torso,0,0,1)
        
        upperarm=space.Box(0.1,0.7,0.1,mass=0.5,draw_as_cylinder=True,
            color=Color(0x8888FF),overdraw_radius=1.2,overdraw_length=1.2)
        self.add(upperarm,0.7,0.5,2)
        upperarm.add_sphere_at(0,0.5,0,0.1,Color(0x4444FF),self)
        upperarm.add_sphere_at(0,-0.5,0,0.1,Color(0x4444FF),self)
        
        lowerarm=space.Box(0.1,0.75,0.1,mass=0.1,draw_as_cylinder=True,
            color=Color(0x8888FF),overdraw_radius=1.2,overdraw_length=1.1)
        self.add(lowerarm,0.7,1.5,2)

        shoulder=HingeConstraint(torso.physics,upperarm.physics,
                            Vector3f(0.7,0.1,1),Vector3f(0,-0.5,0),
                            Vector3f(0,0,1),Vector3f(0,0,1))
               
        elbow=HingeConstraint(upperarm.physics,lowerarm.physics,
                            Vector3f(0,0.5,0),Vector3f(0,-0.5,0),
                            Vector3f(0,0,1),Vector3f(0,0,1))
                            
        shoulder.setLimit(-pi/2,pi/2+.1)
        elbow.setLimit(-pi,0)
        
        self.physics.addConstraint(elbow)
        self.physics.addConstraint(shoulder)
       
        #upperarm.physics.applyTorqueImpulse(Vector3f(0,0,300))
        #lowerarm.physics.applyTorqueImpulse(Vector3f(0,0,300))
            
        self.sch.add(space.Room.start,args=(self,))
        self.update_neurons()
        self.upperarm=upperarm
        self.lowerarm=lowerarm
        self.shoulder=shoulder
        self.elbow=elbow
        self.hinge1=self.shoulder.hingeAngle
        self.hinge2=self.elbow.hingeAngle
        self.upperarm.physics.setSleepingThresholds(0,0)
        self.lowerarm.physics.setSleepingThresholds(0,0)
Exemplo n.º 9
0
source2 = audio.provideAudio("IAS2")
# Assign Audio Buffer to this Audio Source
source2.setAssignedBuffer("IAB1")
# Set the pitch of this Audio Source
source2.setPitch(1.1)
# Set the minimum and maximum number of times this Audio Source should loop
source2.setMinLoops(3)
source2.setMaxLoops(6)
# Set the position of this Audio Source using x, y, z coordinates
#   10 units to the right
#    0 units to the rear
#    0 units up
source2.setPosition(10.0, 0.0, 0.0)
# Set the velocity of this Audio Source using Vector3f object
#    1 units/sec to the left
#    0 units/sec to the rear
#    0 units/sec up
source2.setVelocity(Vector3f(-1.0, 0.0, 0.0))

# Start playing the first Audio Source
source1.play()
# Go to sleep for 3 seconds
time.sleep(3)
# Fade-in the second Audio Source
source2.fadeIn()
# Go to sleep for 1 second
time.sleep(1)
# Fade-out the first Audio Source
source1.fadeOut()
# Second Audio Source will stop after playback of the requisite number of loops
Exemplo n.º 10
0
    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()