コード例 #1
0
 def setupScene(self):
   #create a new scene
   scene = scn.Scene()
   
   #global environment
   self.setupEnvironment(scene)
       
   #add elements
   self.setupSceneElements(scene)
       
   #setup vehicle
   self.myVehicleNode = self.setupVehicle(scene)
       
   #create a main camera
   self.myCameraNode = scn.Node()
   self.myCameraNode.camera = scn.Camera()
   self.myCameraNode.camera.xFov = 75
   self.myCameraNode.camera.zFar = 500
   self.myCameraNode.position = scn.Vector3(0, 60, 50)
   self.myCameraNode.rotation  = scn.Vector4(1, 0, 0, -M_PI_4*0.75)
   scene.rootNode.addChildNode(self.myCameraNode)
       
   #add a secondary camera to the car
   frontCameraNode = scn.Node()
   frontCameraNode.position = scn.Vector3(0, 3.5, 2.5)
   frontCameraNode.rotation = scn.Vector4(0, 1, 0, M_PI)
   frontCameraNode.camera = scn.Camera()
   frontCameraNode.camera.xFov = 75
   frontCameraNode.camera.zFar = 500
       
   self.myVehicleNode.addChildNode(frontCameraNode)
   
   self.pointOfViews = [self.myCameraNode, frontCameraNode]
   
   return scene
コード例 #2
0
  def setupEnvironment(self, scene):
    #add an ambient light
    ambientLight = scn.Node()
    ambientLight.light = scn.Light()
    ambientLight.light.type = scn.LightTypeAmbient
    ambientLight.light.color = (0.3, 0.3, 0.3, 1.0)
    scene.rootNode.addChildNode(ambientLight)
        
    #add a key light to the scene
    #keep an ivar for later manipulation
    self.mySpotLightNode = scn.Node()
    self.mySpotLightNode.light = scn.Light()
    self.mySpotLightNode.light.type = scn.LightTypeSpot        
    self.mySpotLightNode.light.castsShadow = True    
    self.mySpotLightNode.light.color = (0.8, 0.8, 0.8, 1.0)
    self.mySpotLightNode.position = scn.Vector3(0, 80, 30)
    self.mySpotLightNode.rotation = scn.Vector4(1, 0, 0, -M_PI/2.8)
    self.mySpotLightNode.light.spotInnerAngle = 0
    self.mySpotLightNode.light.spotOuterAngle = 50
    self.mySpotLightNode.light.shadowColor = 'black'
    self.mySpotLightNode.light.zFar = 40
    self.mySpotLightNode.light.zNear = 5
    self.mySpotLightNode.shadowSampleCount = 16
    scene.rootNode.addChildNode(self.mySpotLightNode)

    floor = scn.Node()
    floor.geometry = scn.Floor()
    floor.geometry.firstMaterial.diffuse.contents = "resources/wood.png"
    scale = list(scn.Matrix4Identity)
    scale[0], scale[5], scale[10] = 2., 2., 1.
    floor.geometry.firstMaterial.diffuse.contentsTransform = scale
    floor.geometry.firstMaterial.locksAmbientWithDiffuse = True
    floor.geometry.reflectionFalloffEnd = 10
        
    staticBody = scn.PhysicsBody.staticBody()
    floor.physicsBody = staticBody
    scene.rootNode.addChildNode(floor)
コード例 #3
0
  def didSimulatePhysics(self, view, atTime):
      
    engineForce = 0
    brakingForce = 0
    
    accelerometer = motion.get_gravity()
    if accelerometer[0] > 0:
      self.myOrientation = accelerometer[1] * 1.3
    else:
      self.myOrientation = -accelerometer[1] * 1.3
      
    orientation = self.myOrientation
    
    #drive: 1 touch = accelerate, 2 touches = backward, 3 touches = brake
    if self.touchCount == 1:
      engineForce = self.defaultEngineForce
      self.myReactor.birthRate = self.myReactorDefaultBirthRate
    elif self.touchCount == 2:
      engineForce = -self.defaultEngineForce
      self.myReactor.birthRate = 0
    elif self.touchCount == 3:
      brakingForce = 100
      self.myReactor.birthRate = 0
    else:
      brakingForce = self.defaultBrakingForce
      self.myReactor.birthRate = 0
      
    myVehicleSteering = -orientation
    if orientation == 0:
      myVehicleSteering = myVehicleSteering * 0.9
    if myVehicleSteering < -self.steeringClamp:
      myVehicleSteering = -self.steeringClamp
    elif myVehicleSteering > self.steeringClamp:
      myVehicleSteering = self.steeringClamp
      
    #update the vehicle steering and acceleration
    self.myVehicle.setSteeringAngle(myVehicleSteering, forWheelAtIndex=0)
    self.myVehicle.setSteeringAngle(myVehicleSteering, forWheelAtIndex=1)

    self.myVehicle.applyEngineForce(engineForce, forWheelAtIndex=2)
    self.myVehicle.applyEngineForce(engineForce, forWheelAtIndex=3)

    self.myVehicle.applyBrakingForce(brakingForce, forWheelAtIndex=2)
    self.myVehicle.applyBrakingForce(brakingForce, forWheelAtIndex=3)
    
    self.ReorientCarIfNeeded()
    
    if self.overlayScene.myCameraButton.clicked:
      #play a sound
      self.myCameraNode.runAction(self.overlayScene.myCameraButton.clickAction)
      #change the point of view
      self.changePointOfView()
      return
    
    #make camera follow the car node
    car = self.myVehicleNode.presentationNode
    carPos = car.position

    targetPos = scn.Vector3(carPos.x, 30.0, carPos.z + 25.0)
    cameraPos = self.myCameraNode.position

    cameraPos = scn.Vector3(cameraPos.x * (1.0 - self.cameraDamping) + targetPos.x * self.cameraDamping,
                            cameraPos.y * (1.0 - self.cameraDamping) + targetPos.y * self.cameraDamping,
                            cameraPos.z * (1.0 - self.cameraDamping) + targetPos.z * self.cameraDamping)
                 
    self.myCameraNode.position = cameraPos
        
    if not self.inCarView:
      #move spot light in front of the camera
      frontPosition = self.scnView.pointOfView.presentationNode.convertPosition(scn.Vector3(0, 0, -30), toNode=scn.nil)
      self.mySpotLightNode.position = scn.Vector3(frontPosition.x, 80.0, frontPosition.z)
      self.mySpotLightNode.rotation = scn.Vector4(1,0,0, -M_PI/2)
    else:
      #move spot light on top of the car
      self.mySpotLightNode.position = scn.Vector3(carPos.x, 80.0, carPos.z + 30.0)
      self.mySpotLightNode.rotation = scn.Vector4(1,0,0, -M_PI/2.8)
      
    self.overlayScene.mySpeedNeedle.zRotation = -(abs(self.myVehicle.speedInKilometersPerHour) * M_PI / self.maxSpeed)
コード例 #4
0
  def setupVehicle(self, scene):
    chassisNode = scn.Node()
    carScene = scn.Scene(url="resources/rc_car_PY.scn")
    chassisNode = carScene.rootNode.childNodeWithName("rccarBody", recursively=False)
        
    #setup the chassis
    chassisNode.position = scn.Vector3(0, 10, 30)
    chassisNode.rotation = scn.Vector4(0, 1, 0, M_PI)
        
    body = scn.PhysicsBody.dynamicBody()
    body.allowsResting = False
    body.mass = 80
    body.restitution = 0.1
    body.friction = 0.5
    body.rollingFriction = 0
        
    chassisNode.physicsBody = body
    scene.rootNode.addChildNode(chassisNode)
        
    self.pipeNode = chassisNode.childNodeWithName("pipe", recursively=False)
    self.myReactor = scn.ParticleSystem()
    
    self.myReactor.birthRate = 1000
    self.myReactor.birthLocation = scn.ParticleBirthLocation.SCNParticleBirthLocationSurface
    self.myReactor.loops = True
    self.myReactor.emissionDuration = 1
    self.myReactor.emissionDurationVariation = 0
    self.myReactor.idleDuration = 0
    self.myReactor.idleDurationVariation = 0.
    self.myReactor.emittingDirection = (0, 1, 0)
    self.myReactor.spreadingAngle = 10
    self.myReactor.particleAngleVariation = 180
    self.myReactor.particleDiesOnCollision = False
    self.myReactor.particleLifeSpan = 0.15
    self.myReactor.particleLifeSpanVariation = 0.05
    self.myReactor.particleVelocity = 40.
    self.myReactor.particleVelocityVariation = 40.
    self.myReactor.particleImage = ui.Image.named('resources/spark.png')
    self.myReactor.particleSize = 1.
    self.myReactor.particleSizeVariation = 1.
    self.myReactor.particleIntensity = 1.
    self.myReactor.particleIntensityVariation = 0.4
    self.myReactor.stretchFactor = 0.0
    self.myReactor.particleColor = (.87, .45, .0)
    self.myReactor.blendMode = scn.ParticleBlendMode.SCNParticleBlendModeAdditive
        
    self.myReactorDefaultBirthRate = self.myReactor.birthRate
    self.myReactor.birthRate = 0
    self.pipeNode.addParticleSystem(self.myReactor)
        
    #add wheels
    wheel0Node = chassisNode.childNodeWithName("wheelLocator_FL", recursively=True)
    wheel1Node = chassisNode.childNodeWithName("wheelLocator_FR", recursively=True)
    wheel2Node = chassisNode.childNodeWithName("wheelLocator_RL", recursively=True)
    wheel3Node = chassisNode.childNodeWithName("wheelLocator_RR", recursively=True)

    wheel0 = scn.PhysicsVehicleWheel(node=wheel0Node)
    wheel1 = scn.PhysicsVehicleWheel(node=wheel1Node)
    wheel2 = scn.PhysicsVehicleWheel(node=wheel2Node)
    wheel3 = scn.PhysicsVehicleWheel(node=wheel3Node)

    min, max = wheel0Node.getBoundingBox()
       
    wheelHalfWidth = 0.5 * (max.x - min.x)

    p = wheel0Node.convertPosition((0, 0, 0), toNode=chassisNode)
    p = (p.x+wheelHalfWidth, p.y, p.z)
    wheel0.connectionPosition = p
    
    p = wheel1Node.convertPosition((0, 0, 0), toNode=chassisNode)
    p = (p.x-wheelHalfWidth, p.y, p.z)
    wheel1.connectionPosition = p
        
    p = wheel2Node.convertPosition((0, 0, 0), toNode=chassisNode)
    p = (p.x+wheelHalfWidth, p.y, p.z)
    wheel2.connectionPosition = p
    
    p = wheel3Node.convertPosition((0, 0, 0), toNode=chassisNode)
    p = (p.x-wheelHalfWidth, p.y, p.z)
    wheel3.connectionPosition = p
        
    #create the physics vehicle
    vehicle = scn.PhysicsVehicle(chassisBody=chassisNode.physicsBody, wheels=[wheel0, wheel1, wheel2, wheel3])
    scene.physicsWorld.addBehavior(vehicle)
        
    self.myVehicle = vehicle
    return chassisNode
コード例 #5
0
  def setupSceneElements(self, scene):
    #add walls
    wall = scn.Node(geometry=scn.Box(width=400, height=100, length=4, chamferRadius=0))
    wall.geometry.firstMaterial.diffuse.contents = "resources/wall.jpg"
    scale = list(scn.Matrix4Identity)
    scale[0], scale[5], scale[13], scale[10] = 24., 2., 1., 1.
    
    wall.geometry.firstMaterial.diffuse.contentsTransform = scale
    wall.geometry.firstMaterial.diffuse.wrapS = scn.WrapMode.Repeat
    wall.geometry.firstMaterial.diffuse.wrapT = scn.WrapMode.Mirror
    wall.geometry.firstMaterial.doubleSided = False
    wall.castsShadow = False
    wall.geometry.firstMaterial.locksAmbientWithDiffuse = False
        
    wall.position = scn.Vector3(0, 50, -92)
    wall.physicsBody = scn.PhysicsBody.staticBody()
    scene.rootNode.addChildNode(wall)

    wall = wall.clone()
    wall.position = scn.Vector3(-202, 50, 0)
    wall.rotation = scn.Vector4(0, 1, 0, M_PI_2)
    scene.rootNode.addChildNode(wall)
        
    wall = wall.clone()
    wall.position = scn.Vector3(202, 50, 0)
    wall.rotation = scn.Vector4(0, 1, 0, -M_PI_2)
    scene.rootNode.addChildNode(wall)
        
    backWall = scn.Node(geometry=scn.Plane(width=400, height=100))
    backWall.geometry.firstMaterial = wall.geometry.firstMaterial
    backWall.position = scn.Vector3(0, 50, 200)
    backWall.rotation = scn.Vector4(0, 1, 0, M_PI)
    backWall.castsShadow = False
    backWall.physicsBody = scn.PhysicsBody.staticBody()
    scene.rootNode.addChildNode(backWall)
        
    #add ceil
    ceilNode = scn.Node(geometry=scn.Plane(width=400, height=400))
    ceilNode.position = scn.Vector3(0, 100, 0)
    ceilNode.rotation = scn.Vector4(1, 0, 0, M_PI_2)
    ceilNode.geometry.firstMaterial.doubleSided = False
    ceilNode.castsShadow = False
    ceilNode.geometry.firstMaterial.locksAmbientWithDiffuse = False
    scene.rootNode.addChildNode(ceilNode)
   
    #add a train
    self.addTrainToScene(scene, pos=scn.Vector3(-5, 20, -40))
        
    #add wooden blocks
    self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeA.jpg", position=scn.Vector3(-10, 15, 10))
    self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeB.jpg", position=scn.Vector3(-9, 10, 10))
    self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeC.jpg", position=scn.Vector3(20, 15, -11))
    self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeA.jpg", position=scn.Vector3(25, 5, -20))

    #add more block
    for _ in range(4):
      self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeA.jpg", position=scn.Vector3(random.randint(-30, 30), 20, random.randint(-20, 20)))
      self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeB.jpg", position=scn.Vector3(random.randint(-30, 30), 20, random.randint(-20, 20)))
      self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeC.jpg", position=scn.Vector3(random.randint(-30, 30), 20, random.randint(-20, 20)))
        
    #add cartoon book
    block = scn.Node()
    block.position = scn.Vector3(20, 10, -16)
    block.rotation = scn.Vector4(0, 1, 0, -M_PI_4)
    block.geometry = scn.Box(width=22, height=2., length=34, chamferRadius=0)
    frontMat = scn.Material()
    frontMat.locksAmbientWithDiffuse = True
    frontMat.diffuse.contents = "resources/book_front.jpg"
    frontMat.diffuse.mipFilter = scn.FilterMode.Linear
    backMat = scn.Material()
    backMat.locksAmbientWithDiffuse = True
    backMat.diffuse.contents = "resources/book_back.jpg"
    backMat.diffuse.mipFilter = scn.FilterMode.Linear
    edgeMat = scn.Material()
    edgeMat.locksAmbientWithDiffuse = True
    edgeMat.diffuse.contents = "resources/book_side_title.jpg"
    edgeMat.diffuse.mipFilter = scn.FilterMode.Linear
    edgeMatSide = scn.Material()
    edgeMatSide.locksAmbientWithDiffuse = True
    edgeMatSide.diffuse.contents = "resources/book_side.jpg"
    edgeMatSide.diffuse.mipFilter = scn.FilterMode.Linear
  
    block.geometry.materials = [edgeMatSide, edgeMatSide, edgeMatSide, edgeMat, frontMat, backMat]
    block.physicsBody = scn.PhysicsBody.dynamicBody()
    scene.rootNode.addChildNode(block)
        
    #add carpet
    path = ui.Path.rounded_rect(-50, -30, 100, 50, 2.5)
    rug_geometry = scn.Shape.shapeWithPath(path, extrusionDepth=0.05)
    rug = scn.Node.nodeWithGeometry(rug_geometry)
    rug.geometry.firstMaterial.locksAmbientWithDiffuse = True
    rug.geometry.firstMaterial.diffuse.contents = "resources/carpet.jpg"
    rug.position = scn.Vector3(0, 0.01, 0)
    rug.rotation = scn.Vector4(1, 0, 0, M_PI_2)
    scene.rootNode.addChildNode(rug)
        
    #add ball
    ball = scn.Node()
    ball.position = scn.Vector3(-5, 5, -18)
    ball.geometry = scn.Sphere(radius=5)
    ball.geometry.firstMaterial.locksAmbientWithDiffuse = True
    ball.geometry.firstMaterial.diffuse.contents = "resources/ball.jpg"
    scale = list(scn.Matrix4Identity)
    scale[0], scale[5], scale[10] = 2., 1., 1.
    ball.geometry.firstMaterial.diffuse.contentsTransform = scale
    ball.geometry.firstMaterial.diffuse.wrapS = scn.WrapMode.Mirror
    ball.physicsBody = scn.PhysicsBody.dynamicBody()
    ball.physicsBody.restitution = 0.9
    scene.rootNode.addChildNode(ball)