Advertisement
billysback

python motion engine (WIP)

Jan 30th, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.84 KB | None | 0 0
  1. import math
  2.  
  3. pi = math.pi
  4.  
  5. class Motion:
  6.     def __init__(self, velocity, acceleration):
  7.         self.velocity = velocity
  8.         self.acceleration = accel
  9.  
  10.     def getDistance(self, time):
  11.         s = (self.velocity*time) + (0.5*self.accel*(time*time))
  12.         return s
  13.  
  14.     def getVelocity(self, time)
  15.         v = (self.velocity + (self.accel*time))
  16.         return v
  17.  
  18.     def getTime(self, distance):
  19.         t = (distance-self.velocity)/self.accel
  20.         return t
  21.  
  22.     def addForce(self, force):
  23.         accel = (force/mass)
  24.         self.accel = self.accel + accel
  25.  
  26. def newMotion(mass, force):
  27.     accel = 0
  28.     if mass != 0:
  29.         accel = (force/mass)
  30.     return Motion(0, accel)
  31.  
  32. class Direction:
  33.     def __init__(self, motion, angle):
  34.         self.motion = motion
  35.         self.angle = angle
  36.  
  37.     def updateAngle(self):
  38.         while self.angle < 0:
  39.             self.angle = self.angle + (2*pi)
  40.         while self.angle > 2*pi:
  41.             self.angle = self.angle - (2*pi)
  42.  
  43.     def getMods(self):
  44.         self.updateAngle()
  45.         trueA, xmod, ymod = self.angle, 1, 1
  46.         if self.angle <= pi/2:
  47.             trueA, xmod, ymod = self.angle, 1, 1
  48.         elif self.angle <= pi:
  49.             trueA, xmod, ymod = pi - self.angle, 1, -1
  50.         elif self.angle <= (pi/2) + pi:
  51.             trueA, xmod, ymod = pi + self.angle, -1, -1
  52.         elif self.angle <= 2*pi:
  53.             trueA, xmod, ymod = (2*pi) - self.angle, -1, 1
  54.         return trueA, xmod, ymod
  55.  
  56.     def getMovement(self, time):
  57.         a, xmod, ymod = self.getMods()
  58.         dist = self.motion.getDistance(time)
  59.         x = (math.sin(a)*dist)*xmod
  60.         y = (math.cos(a)*dist)*ymod
  61.         return x, y
  62.  
  63.     def getForce(self, mass):
  64.         return self.motion.accel*mass
  65.  
  66. class Mass:
  67.     def __init__(self, mass, direction):
  68.         self.mass = mass
  69.         self.direction = direction
  70.  
  71.     def getForce(self):
  72.         return self.direction.getForce(self.mass)
  73.    
  74.     def push(self, direction):
  75.         direction.updateAngle()
  76.         self.direction.updateAngle()
  77.        
  78.         f1 = self.direction.getForce(self.mass)
  79.         a1, xmod1, ymod1 = self.direction.getMods()
  80.         f2 = direction.getForce(self.mass)
  81.         a2, xmod2, ymod2 = direction.getMods()
  82.  
  83.         x1, y1 = (math.sin(a1)*f1)*xmod1, (math.cos(a1)*f1)*ymod1
  84.         x2, y2 = (math.sin(a2)*f2)*xmod2, (math.cos(a2)*f2)*ymod2
  85.         nx, ny = x1+x2, y1+y2
  86.         nforce = math.sqrt( (nx*nx) + (ny*ny) )
  87.         na = 0
  88.        
  89.         if nx != 0 and ny != 0:
  90.             if nx > 0 and ny > 0:
  91.                 na = math.asin(nx/nforce)
  92.             elif nx > 0 and ny < 0:
  93.                 na = pi - math.asin(math.fabs(nx)/nforce)
  94.             elif nx < 0 and ny < 0:
  95.                 na = pi + math.asin(math.fabs(nx)/nforce)
  96.             elif nx < 0 and ny > 0:
  97.                 na = (2*pi) - math.asin(math.fabs(nx)/nforce)
  98.             else:
  99.                 raise Exception(" Impossible Coordinates: ", nx, ",", ny)
  100.         elif nx == 0 and ny == 0:
  101.             self.direction.motion = newMotion(self.mass, 0)
  102.             na = None
  103.         elif nx == 0:
  104.             if ny > 0:
  105.                 na = 0
  106.             else:
  107.                 na = pi
  108.         elif ny == 0:
  109.             if nx > 0:
  110.                 na = (pi/2)
  111.             else:
  112.                 na = pi + (pi/2)
  113.         if na != None:
  114.             nDir = Direction(newMotion(self.mass, nforce), na)
  115.  
  116. class Surface:
  117.     def __init__(self, strength):
  118.         self.strength = strength
  119.         self.broken = False
  120.  
  121.     def collide(self, mass, normalAngle):
  122.         if self.strength < mass.getForce():
  123.             self.broken = True
  124.         if self.absorb > 0:
  125.             mass.direction.motion.addForce(-self.absorb)
  126.         if self.bounce > 0:
  127.             mass.push(Direction(newMotion(1, self.bounce), normalAngle))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement