Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- pi = math.pi
- class Motion:
- def __init__(self, velocity, acceleration):
- self.velocity = velocity
- self.acceleration = accel
- def getDistance(self, time):
- s = (self.velocity*time) + (0.5*self.accel*(time*time))
- return s
- def getVelocity(self, time)
- v = (self.velocity + (self.accel*time))
- return v
- def getTime(self, distance):
- t = (distance-self.velocity)/self.accel
- return t
- def addForce(self, force):
- accel = (force/mass)
- self.accel = self.accel + accel
- def newMotion(mass, force):
- accel = 0
- if mass != 0:
- accel = (force/mass)
- return Motion(0, accel)
- class Direction:
- def __init__(self, motion, angle):
- self.motion = motion
- self.angle = angle
- def updateAngle(self):
- while self.angle < 0:
- self.angle = self.angle + (2*pi)
- while self.angle > 2*pi:
- self.angle = self.angle - (2*pi)
- def getMods(self):
- self.updateAngle()
- trueA, xmod, ymod = self.angle, 1, 1
- if self.angle <= pi/2:
- trueA, xmod, ymod = self.angle, 1, 1
- elif self.angle <= pi:
- trueA, xmod, ymod = pi - self.angle, 1, -1
- elif self.angle <= (pi/2) + pi:
- trueA, xmod, ymod = pi + self.angle, -1, -1
- elif self.angle <= 2*pi:
- trueA, xmod, ymod = (2*pi) - self.angle, -1, 1
- return trueA, xmod, ymod
- def getMovement(self, time):
- a, xmod, ymod = self.getMods()
- dist = self.motion.getDistance(time)
- x = (math.sin(a)*dist)*xmod
- y = (math.cos(a)*dist)*ymod
- return x, y
- def getForce(self, mass):
- return self.motion.accel*mass
- class Mass:
- def __init__(self, mass, direction):
- self.mass = mass
- self.direction = direction
- def getForce(self):
- return self.direction.getForce(self.mass)
- def push(self, direction):
- direction.updateAngle()
- self.direction.updateAngle()
- f1 = self.direction.getForce(self.mass)
- a1, xmod1, ymod1 = self.direction.getMods()
- f2 = direction.getForce(self.mass)
- a2, xmod2, ymod2 = direction.getMods()
- x1, y1 = (math.sin(a1)*f1)*xmod1, (math.cos(a1)*f1)*ymod1
- x2, y2 = (math.sin(a2)*f2)*xmod2, (math.cos(a2)*f2)*ymod2
- nx, ny = x1+x2, y1+y2
- nforce = math.sqrt( (nx*nx) + (ny*ny) )
- na = 0
- if nx != 0 and ny != 0:
- if nx > 0 and ny > 0:
- na = math.asin(nx/nforce)
- elif nx > 0 and ny < 0:
- na = pi - math.asin(math.fabs(nx)/nforce)
- elif nx < 0 and ny < 0:
- na = pi + math.asin(math.fabs(nx)/nforce)
- elif nx < 0 and ny > 0:
- na = (2*pi) - math.asin(math.fabs(nx)/nforce)
- else:
- raise Exception(" Impossible Coordinates: ", nx, ",", ny)
- elif nx == 0 and ny == 0:
- self.direction.motion = newMotion(self.mass, 0)
- na = None
- elif nx == 0:
- if ny > 0:
- na = 0
- else:
- na = pi
- elif ny == 0:
- if nx > 0:
- na = (pi/2)
- else:
- na = pi + (pi/2)
- if na != None:
- nDir = Direction(newMotion(self.mass, nforce), na)
- class Surface:
- def __init__(self, strength):
- self.strength = strength
- self.broken = False
- def collide(self, mass, normalAngle):
- if self.strength < mass.getForce():
- self.broken = True
- if self.absorb > 0:
- mass.direction.motion.addForce(-self.absorb)
- if self.bounce > 0:
- mass.push(Direction(newMotion(1, self.bounce), normalAngle))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement