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 limitVelocity(self): shipVel = self.getVel() newVelScalar = tupleLength(shipVel) if newVelScalar > Ship.SPEED_MAX: newVelScale = Ship.SPEED_MAX / newVelScalar newVel = scaleTuple(shipVel, newVelScale) self.setVel(newVel)
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 testScaleTuple(self): self.failUnlessEqual( scaleTuple( (1, -4), 3 ), (3, -12) )
def testScaleTuple(self): self.failUnlessEqual(scaleTuple((1, -4), 3), (3, -12))