from OpenGL.GL import * from dmsgl import * class PointMass: def __init__(self, position=Vector([0,0,0]), velocity=Vector([0,0,0]), mass=1.0): self.position = position self.velocity = velocity self.mass = mass self.force = Vector([0, 0, 0]) self.mobile = True def clearForce(self): self.force = Vector([0,0,0]) def addForce(self, force): self.force += force def update(self, dt): if self.mobile: acc = self.force / self.mass self.velocity += acc * dt self.position += self.velocity * dt class Spring: def __init__(self, point1, point2, restLength=-1, k=1): self.point1 = point1 self.point2 = point2 if restLength >= 0: self.restLength = restLength else: self.restLength = point1.position.distance(point2.position) self.k = k def calculateForce(self): v = self.point2.position - self.point1.position displacement = v.length() - self.restLength v.normalize() force = self.k * displacement * v self.point1.addForce(force) self.point2.addForce(-force) def draw(self): glBegin(GL_LINES) glVertex3fv(self.point1.position) glVertex3fv(self.point2.position) glEnd()