def Rope(verlet, startX, startY, links, linkLength, elasticity, pinFirst): lastParticle = None for i in range(0, links): pin = Vector2D( startX, startY) if lastParticle == None and pinFirst == True else None particle = Particle({ 'vector': Vector2D(startX, startY), 'pinnedTo': pin }) verlet.addParticle(particle) if (lastParticle): verlet.addConstraint( Constraint({ 'startParticle': particle, 'endParticle': lastParticle, 'stiffness': elasticity, 'objectID': random.randint(0, 100000), 'data': { 'drawn': True } })) startX += linkLength lastParticle = particle
def Triangle(verlet, startX, startY, size, elasticity, objectIndex): particle1 = Particle({'vector': Vector2D(startX, startY)}) particle2 = Particle({'vector': Vector2D(startX + size, startY)}) particle3 = Particle({'vector': Vector2D(startX, startY + size)}) verlet.addParticle(particle1) verlet.addParticle(particle2) verlet.addParticle(particle3) edge1 = Constraint({ 'startParticle': particle1, 'endParticle': particle2, 'stiffness': 1, 'collides': True, 'objectID': objectIndex, 'data': { 'drawn': True } }) edge2 = Constraint({ 'startParticle': particle2, 'endParticle': particle3, 'stiffness': 1, 'collides': True, 'objectID': objectIndex, 'data': { 'drawn': True } }) edge3 = Constraint({ 'startParticle': particle3, 'endParticle': particle1, 'vstiffness': 1, 'collides': True, 'objectID': objectIndex, 'data': { 'drawn': True } }) verlet.addConstraint(edge1) verlet.addConstraint(edge2) verlet.addConstraint(edge3) verlet.addBody(Body({ 'edges': [edge1, edge2, edge3], 'collides': True }))
def __init__(self, options): self.vector = options['vector'] if 'vector' in options else Vector2D() self.lastVector = self.vector.copy() self.mass = options['mass'] if 'mass' in options else 1 self.pinnedTo = options['pinnedTo'] if 'pinnedTo' in options else None self.collides = options['collides'] if 'collides' in options else False self.objectID = options['objectID'] if 'objectID' in options else None self.data = options['data'] if 'data' in options else {} self.radius = options['radius'] if 'radius' in options else 0
def getCenter(self): centerX = 0 centerY = 0 for i in range(0, len(self.uniqueVectors)): centerX += self.uniqueVectors[i].x centerY += self.uniqueVectors[i].y return Vector2D(centerX / (len(self.uniqueVectors)), centerY / (len(self.uniqueVectors)))
def __init__(self, options): self.iterations = 2 self.constraintSnapCallback = None self.useMass = False self.particles = [] self.paused = False self.gravity = Vector2D(0, 0.005) self.constraints = [] self.bodies = [] self.stageMinVect = Vector2D(10, 10) self.stageMaxVect = Vector2D(790, 490) self.speedLimitMinVect = Vector2D(-4, -4) self.speedLimitMaxVect = Vector2D(4, 4) self.stageFriction = 0.001 if (options is not None): if 'iterations' in options: self.iterations = options['iterations'] if 'gravity' in options: self.gravity = options['gravity'] if 'stageMinVect' in options: self.stageMinVect = options['stageMinVect'] if 'stageMaxVect' in options: self.stageMaxVect = options['stageMaxVect'] if 'speedLimitMinVect' in options: self.speedLimitMinVect = options['speedLimitMinVect'] if 'speedLimitMaxVect' in options: self.speedLimitMaxVect = options['speedLimitMaxVect'] if 'constraintSnapCallback' in options: self.constraintSnapCallback = options['constraintSnapCallback'] if 'useMass' in options: self.useMass = options['useMass'] if 'stageFriction' in options: self.stageFriction = options['stageFriction']
def __init__(self, vect2DA=Vector2D(), vect2DB=Vector2D()): self.ends = lambda: None self.ends.startVect2D = vect2DA self.ends.endVect2D = vect2DB
def rightNormal(self): dx = self.ends.endVect2D.x - self.ends.startVect2D.x dy = self.ends.endVect2D.y - self.ends.startVect2D.y return Vector2D(dy, dx * -1)
def leftNormal(self): dx = self.ends.endVect2D.x - self.ends.startVect2D.x dy = self.ends.endVect2D.y - self.ends.startVect2D.y return Vector2D(dy * -1, dx)
options = RGBMatrixOptions() options.rows = 32 options.cols = 64 # options.brightness = self.args.led_brightness matrix = RGBMatrix(options=options) # init mpu9250 # https://github.com/FaBoPlatform/FaBo9AXIS-MPU9250-Python/blob/master/example/read9axis.py mpu9250 = FaBo9Axis_MPU9250.MPU9250() # init verlet verlet = Integration({ 'stageMinVect': Vector2D(2, 2), 'stageMaxVect': Vector2D(CANVAS_WIDTH - 2, CANVAS_HEIGHT - 2), 'gravity': Vector2D(0, 0.05) }) objectID = 0 for x in range(0, 2): for y in range(0, 1): Prefabs.Box(verlet, random.randint(0, CANVAS_WIDTH - 10), random.randint(0, CANVAS_HEIGHT - 10), 10, 10, random.randint(0, 360), True, 0.55, objectID) objectID += 1
def getCollision(self, body): # initialize the length of the collision vector to a relatively large value minDistance = 10000 collisionAxis = None collisionEdge = None collisionVector = None collisionEdgeBody = None primaryBody = None secondaryBody = None # just a fancy way of iterating through all of the edges of both bodies at once for i in range(0, len(self.edges) + len(body.edges)): edge = None if (i < len(self.edges)): edge = self.edges[i].ends else: edge = body.edges[i - len(self.edges)].ends # calculate the axis perpendicular to this edge and normalize it axisVect = Vector2D( edge.startParticle.vector.y - edge.endParticle.vector.y, edge.endParticle.vector.x - edge.startParticle.vector.x) axisVect.normalize() projection1 = self.projectToAxis(axisVect) projection2 = body.projectToAxis(axisVect) # calculate the distance between the two intervals - see below distance = projection2[0] - projection1[1] if projection1[ 0] < projection2[0] else projection1[0] - projection2[1] if (distance > 0): # if the intervals don't overlap, return, since there is no collision return None elif (abs(distance) < minDistance): # if they do and it's the shortest distance so far, save this info for response this = self minDistance = abs(distance) collisionAxis = axisVect collisionEdge = edge collisionEdgeBody = this if i < len(self.edges) else body primaryBody = this secondaryBody = body if (this == collisionEdgeBody): primaryBody = body secondaryBody = this # this is needed to make sure that the collision normal is pointing at B1 sign = collisionAxis.dotProduct( secondaryBody.getCenter().getSubtractedFromVector( primaryBody.getCenter())) > 0 # remember that the line equation is N*( R - R0 ). We choose B2->Center # as R0 the normal N is given by the collision normal if (sign == True): collisionAxis.reverse( ) # reverse the collision normal if it points away from B1 smallestDistance = 10000 for i in range(0, len(primaryBody.uniqueVectors)): # measure the distance of the vertex from the line using the line equation vertexDistance = collisionAxis.dotProduct( primaryBody.uniqueVectors[i].getSubtractedFromVector( secondaryBody.getCenter())) # if the measured distance is smaller than the smallest distance reported # so far, set the smallest distance and the collision vertex if (vertexDistance < smallestDistance): smallestDistance = vertexDistance collisionVector = primaryBody.uniqueVectors[i] return { 'distance': minDistance, 'collisionVector': collisionAxis, 'edge': collisionEdge, 'edgeBody': collisionEdgeBody, 'vector': collisionVector, 'vectorBody': primaryBody }
options.cols = 64 # options.brightness = self.args.led_brightness matrix = RGBMatrix(options=options) # init mpu9250 # https://github.com/FaBoPlatform/FaBo9AXIS-MPU9250-Python/blob/master/example/read9axis.py mpu9250 = FaBo9Axis_MPU9250.MPU9250() # init verlet verlet = Integration({ 'iterations': 1, 'stageMinVect': Vector2D(2, 2), 'stageMaxVect': Vector2D(CANVAS_WIDTH - 2, CANVAS_HEIGHT - 2), 'gravity': Vector2D(0, 0.05) }) for i in range(0, 25): verlet.addParticle( Particle({ 'vector': Vector2D(random.randint(0, CANVAS_WIDTH), random.randint(0, CANVAS_HEIGHT)), 'radius': 1 + random.randint(0, 3), 'collides':
def Box(verlet, x, y, width, height, rotationDegrees, collides, stiffness, objectID): from VerletIntegration import Utilities particle1 = Particle({'vector': Vector2D(x, y), 'objectID': objectID}) rotatedPoint1 = Utilities.RotatePoint(x, y, x + width, y, rotationDegrees) particle2 = Particle({ 'vector': Vector2D(rotatedPoint1[0], rotatedPoint1[1]), 'objectID': objectID }) rotatedPoint2 = Utilities.RotatePoint(x, y, x + width, y + height, rotationDegrees) particle3 = Particle({ 'vector': Vector2D(rotatedPoint2[0], rotatedPoint2[1]), 'objectID': objectID }) rotatedPoint3 = Utilities.RotatePoint(x, y, x, y + height, rotationDegrees) particle4 = Particle({ 'vector': Vector2D(rotatedPoint3[0], rotatedPoint3[1]), 'objectID': objectID }) verlet.addParticle(particle1) verlet.addParticle(particle2) verlet.addParticle(particle3) verlet.addParticle(particle4) constraint1 = Constraint({ 'startParticle': particle1, 'endParticle': particle2, 'stiffness': stiffness, 'objectID': objectID, 'collides': collides, 'data': { 'drawn': True } }) verlet.addConstraint(constraint1) constraint2 = Constraint({ 'startParticle': particle2, 'endParticle': particle3, 'stiffness': stiffness, 'objectID': objectID, 'collides': collides, 'data': { 'drawn': True } }) verlet.addConstraint(constraint2) constraint3 = Constraint({ 'startParticle': particle3, 'endParticle': particle4, 'stiffness': stiffness, 'objectID': objectID, 'collides': collides, 'data': { 'drawn': True } }) verlet.addConstraint(constraint3) constraint4 = Constraint({ 'startParticle': particle4, 'endParticle': particle1, 'stiffness': stiffness, 'objectID': objectID, 'collides': collides, 'data': { 'drawn': True } }) verlet.addConstraint(constraint4) verlet.addBody( Body({ 'edges': [constraint1, constraint2, constraint3, constraint4], 'collides': True })) verlet.addConstraint( Constraint({ 'startParticle': particle1, 'endParticle': particle3, 'stiffness': stiffness, 'objectID': objectID, 'collides': False })) verlet.addConstraint( Constraint({ 'startParticle': particle2, 'endParticle': particle4, 'stiffness': stiffness, 'objectID': objectID, 'collides': False }))
def Cloth(verlet, startX, startY, links, linkLength, elasticity, tolerance, pinCorners): particleArray = {} xlinks = links * 2 for i in range(0, xlinks): for o in range(0, links): pin = Vector2D(startX + (i * linkLength), startY + (o * linkLength)) if ( o == 0 and (i == 0 or i == (xlinks - 1))) else None particle = Particle({ 'vector': Vector2D(startX + (i * linkLength), startY + (o * linkLength)), 'pinnedTo': pin }) particleArray[str(i) + "_" + str(o)] = particle verlet.addParticle(particle) if (i > 0): verlet.addConstraint( Constraint({ 'startParticle': particle, 'endParticle': particleArray[str(i - 1) + "_" + str(o)], 'stiffness': elasticity, 'tolerance': tolerance, 'collides': True, 'objectID': 0, 'data': { 'drawn': True if (o % 4 == 0) or ((o == 0 or o == (links - 1))) else False } })) if (o > 0): verlet.addConstraint( Constraint({ 'startParticle': particle, 'endParticle': particleArray[str(i) + "_" + str((o - 1))], 'stiffness': elasticity, 'tolerance': tolerance, 'collides': True, 'objectID': 0, 'data': { 'drawn': True if (i % 4 == 0) or ((i == 0 or i == (xlinks - 1))) else False } }))
options = RGBMatrixOptions() options.rows = 32 options.cols = 64 # options.brightness = self.args.led_brightness matrix = RGBMatrix(options = options) # init mpu9250 # https://github.com/FaBoPlatform/FaBo9AXIS-MPU9250-Python/blob/master/example/read9axis.py mpu9250 = FaBo9Axis_MPU9250.MPU9250() # init verlet verlet = Integration({ 'stageMinVect': Vector2D(2, 2), 'stageMaxVect': Vector2D(CANVAS_WIDTH - 2, CANVAS_HEIGHT - 2), 'gravity': Vector2D(0, 0.05) }) for i in range(0, 400): Prefabs.Rope(verlet, random.randint(0, 64), 0, random.randint(0, 3) + 1, random.randint(0, 3) + 5, 1, True) highestMX = 0 lowestMX = 0
def isPointInside(self, x, y): return self.vector.distanceTo(Vector2D(x, y)) < self.radius
def __init__(self, vect2D=Vector2D(), rad=0): self.vector = vect2D self.radius = rad