def applyGravity(self, ship, deltaTime): distance = tupleDistance(ship.getPos(), vec3ToTuple(self.planet.getPos())) if distance > Game.GRAVITY_DISTANCE: return gravity = Game.GRAVITY / distance gravityVector = tupleNormalize( tupleSegment(ship.getPos(), vec3ToTuple(self.planet.getPos()))) gravityVector = scaleTuple(gravityVector, gravity * deltaTime) ship.applyForce(gravityVector)
def applyGravity(self, ship, deltaTime): distance = tupleDistance( ship.getPos(), vec3ToTuple( self.planet.getPos() ) ) if distance > Game.GRAVITY_DISTANCE: return gravity = Game.GRAVITY/distance gravityVector = tupleNormalize( tupleSegment( ship.getPos(), vec3ToTuple( self.planet.getPos() ) ) ) gravityVector = scaleTuple( gravityVector, gravity * deltaTime) ship.applyForce( gravityVector )
def updateCamera(self): ships = self.getShips() shipOne = ships[0] shipOnePos = shipOne.getPos() shipTwo = ships[1] shipTwoPos = shipTwo.getPos() # Calculate the distance between the ships distance = tupleDistance(shipOnePos, shipTwoPos) cameraDistance = distance * Game.CAMERA_DISTANCE_COEFFICIENT if cameraDistance > Game.CAMERA_DISTANCE_MAX: cameraDistance = Game.CAMERA_DISTANCE_MAX if cameraDistance < Game.CAMERA_DISTANCE_MIN: cameraDistance = Game.CAMERA_DISTANCE_MIN # Calculate the middle point in space between the ship's positions middle = tupleMiddle(shipOnePos, shipTwoPos) cameraPos = self.getCameraPos() self.lastCameraPos = cameraPos newCameraPos = (middle[0], middle[1], cameraDistance) self.setCameraPos(newCameraPos) self.updateStars(newCameraPos)
def testTupleDistance(self): tuple1 = (1, 2) tuple2 = (4, 6) distance = ( ( tuple2[0] - tuple1[0] )**2 + ( tuple2[1] - tuple1[1] )**2 )**(1/2.) self.failUnlessEqual( tupleDistance( tuple1, tuple2 ), distance )
def testTupleDistance(self): tuple1 = (1, 2) tuple2 = (4, 6) distance = ((tuple2[0] - tuple1[0])**2 + (tuple2[1] - tuple1[1])**2)**(1 / 2.) self.failUnlessEqual(tupleDistance(tuple1, tuple2), distance)