def testTupleSegment(self): tuple1 = (1, 2) tuple2 = (-2, 3) segment12 = (-3, 1) segment21 = (3, -1) self.failUnlessEqual( tupleSegment( tuple1, tuple2 ), segment12 ) self.failUnlessEqual( tupleSegment( tuple2, tuple1 ), segment21 )
def testTupleSegment(self): tuple1 = (1, 2) tuple2 = (-2, 3) segment12 = (-3, 1) segment21 = (3, -1) self.failUnlessEqual(tupleSegment(tuple1, tuple2), segment12) self.failUnlessEqual(tupleSegment(tuple2, tuple1), segment21)
def warpShips(self, warpAxis): shipOne = self.ships[0] shipTwo = self.ships[1] shipOnePos = shipOne.getPos() shipTwoPos = shipTwo.getPos() furtherShip = None closerShip = None if shipOnePos == tupleFurthestDistance( vec3ToTuple(self.planet.getPos()), [shipOnePos, shipTwoPos]): furtherShip = shipOne closerShip = shipTwo else: closerShip = shipOne furtherShip = shipTwo furtherToCloser = tupleSegment(furtherShip.getPos(), closerShip.getPos()) if warpAxis == 'x': furtherShip.setPos( (furtherShip.getPos()[0] + furtherToCloser[0] * 2, furtherShip.getPos()[1])) elif warpAxis == 'y': furtherShip.setPos( (furtherShip.getPos()[0], furtherShip.getPos()[1] + furtherToCloser[1] * 2))
def warpPlanet(self, warpAxis): planetPos = vec3ToTuple( self.planet.getPos() ) planetToCamera = tupleSegment( planetPos, self.getCameraPos() ) if warpAxis == 'x': self.planet.setPos( planetPos[0] + planetToCamera[0]*2, planetPos[1], 0 ) self.planetCollGeom.setPosition( planetPos[0] + planetToCamera[0]*2, planetPos[1], 0 ) elif warpAxis == 'y': self.planet.setPos( planetPos[0], planetPos[1] + planetToCamera[1]*2, 0 ) self.planetCollGeom.setPosition( planetPos[0], planetPos[1] + planetToCamera[1]*2, 0 )
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 updateStars(self, newCameraPos): # TODO: Add unit tests! self.stars.setPos(newCameraPos) cameraDeltaPos = tupleSegment(self.lastCameraPos, vec3ToTuple(newCameraPos)) xRotation = Quat() xRotation.setFromAxisAngle(cameraDeltaPos[0] * .1, Vec3(0, 1, 0)) yRotation = Quat() yRotation.setFromAxisAngle(-cameraDeltaPos[1] * .1, Vec3(1, 0, 0)) newRotation = xRotation.multiply(yRotation) self.starsRotation *= newRotation self.stars.setQuat(self.starsRotation)
def colHandler(ship, contact): pos = ship.getPos() elasticity = 2.5 #mass1 = ship.getMass() #mass2 = collider.getMass() vel1 = ship.getVel() normal = tupleNormalize(tupleSegment(pos, contact)) force = (scaleTuple(normal, -elasticity * (1 / 2.) * dotProduct(vel1, normal))) ship.applyForce(force) ship.setPos(addTuples(ship.getPos(), scaleTuple(normal, -.1))) ship.getCollisions().remove(contact)
def warpPlanet(self, warpAxis): planetPos = vec3ToTuple(self.planet.getPos()) planetToCamera = tupleSegment(planetPos, self.getCameraPos()) if warpAxis == 'x': self.planet.setPos(planetPos[0] + planetToCamera[0] * 2, planetPos[1], 0) self.planetCollGeom.setPosition( planetPos[0] + planetToCamera[0] * 2, planetPos[1], 0) elif warpAxis == 'y': self.planet.setPos(planetPos[0], planetPos[1] + planetToCamera[1] * 2, 0) self.planetCollGeom.setPosition( planetPos[0], planetPos[1] + planetToCamera[1] * 2, 0)
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 updateStars(self, newCameraPos): # TODO: Add unit tests! self.stars.setPos(newCameraPos) cameraDeltaPos = tupleSegment( self.lastCameraPos, vec3ToTuple( newCameraPos ) ) xRotation = Quat() xRotation.setFromAxisAngle( cameraDeltaPos[0] * .1, Vec3( 0, 1, 0 ) ) yRotation = Quat() yRotation.setFromAxisAngle( -cameraDeltaPos[1] * .1, Vec3( 1, 0, 0 ) ) newRotation = xRotation.multiply( yRotation ) self.starsRotation *= newRotation self.stars.setQuat( self.starsRotation )
def warpShips(self, warpAxis): shipOne = self.ships[0] shipTwo = self.ships[1] shipOnePos = shipOne.getPos() shipTwoPos = shipTwo.getPos() furtherShip = None closerShip = None if shipOnePos == tupleFurthestDistance( vec3ToTuple( self.planet.getPos() ), [shipOnePos, shipTwoPos] ): furtherShip = shipOne closerShip = shipTwo else: closerShip = shipOne furtherShip = shipTwo furtherToCloser = tupleSegment( furtherShip.getPos(), closerShip.getPos() ) if warpAxis == 'x': furtherShip.setPos( ( furtherShip.getPos()[0] + furtherToCloser[0]*2, furtherShip.getPos()[1] ) ) elif warpAxis == 'y': furtherShip.setPos( ( furtherShip.getPos()[0], furtherShip.getPos()[1] + furtherToCloser[1]*2 ) )