|
| 1 | +class Circle(object): |
| 2 | + NumCollisions = 0 # Number of collisions in one frame |
| 3 | + NumConnections = 0 # Total number of collisions |
| 4 | + GravityAngle = 1 # Angle to gravity center in degrees |
| 5 | + GravityTheta = 1 # Angle to gravity center in radians |
| 6 | + TotalCircles = 0 |
| 7 | + Gravity = 0 |
| 8 | + CXV = 0 |
| 9 | + CYV = 0 |
| 10 | + |
| 11 | + def __init__(self, index, x, y, xv, yv): |
| 12 | + self.index = index |
| 13 | + self.x = x |
| 14 | + self.y = y |
| 15 | + self.radius = 2 |
| 16 | + self.xv = xv |
| 17 | + self.yv = yv |
| 18 | + self.mightCollide = [1] * Circle.TotalCircles |
| 19 | + self.hasCollided = [1] * Circle.TotalCircles |
| 20 | + self.distances = [1] * Circle.TotalCircles |
| 21 | + self.angles = [1] * Circle.TotalCircles |
| 22 | + self.thetas = [1] * Circle.TotalCircles |
| 23 | + |
| 24 | + def behave(self, circles, sketchXMid, sketchYMid): |
| 25 | + self.move() |
| 26 | + self.areWeClose(circles) |
| 27 | + self.areWeColliding(circles) |
| 28 | + self.areWeConnected(circles) |
| 29 | + self.applyGravity(sketchXMid, sketchYMid) |
| 30 | + self.render(circles) |
| 31 | + self.reset() |
| 32 | + |
| 33 | + def areWeClose(self, others): |
| 34 | + for other in others: |
| 35 | + if self.index != other.index: |
| 36 | + if (abs(self.x - other.x) < 50 |
| 37 | + and abs(self.y - other.y) < 50): |
| 38 | + self.mightCollide[other.index] = True |
| 39 | + else: |
| 40 | + self.mightCollide[other.index] = False |
| 41 | + |
| 42 | + def areWeColliding(self, others): |
| 43 | + for other in others: |
| 44 | + if (self.mightCollide[other.index] |
| 45 | + and self.index != other.index): |
| 46 | + self.distances[other.index] = dist(self.x, self.y, |
| 47 | + other.x, other.y) |
| 48 | + if (self.distances[other.index] |
| 49 | + < (self.radius + other.radius) * 1.1): |
| 50 | + self.hasCollided[other.index] = True |
| 51 | + other.hasCollided[self.index] = True |
| 52 | + self.angles[other.index] = Circle.findAngle(self.x, self.y, |
| 53 | + other.x, other.y) |
| 54 | + self.thetas[other.index] = (-(self.angles[other.index] * PI)) / 180.0 |
| 55 | + Circle.CXV += (cos(self.thetas[other.index]) |
| 56 | + * ((other.radius + self.radius) / 2.0)) |
| 57 | + Circle.CYV += (sin(self.thetas[other.index]) |
| 58 | + * ((other.radius + self.radius) / 2.0)) |
| 59 | + Circle.NumCollisions += 1 |
| 60 | + if Circle.NumCollisions > 0: |
| 61 | + self.xv = -Circle.CXV / Circle.NumCollisions |
| 62 | + self.yv = -Circle.CYV / Circle.NumCollisions |
| 63 | + Circle.CXV = 0.0 |
| 64 | + Circle.CYV = 0.0 |
| 65 | + |
| 66 | + def areWeConnected(self, others): |
| 67 | + maxDistance = 150 |
| 68 | + for other in others: |
| 69 | + if (self.hasCollided[other.index] |
| 70 | + and self.index != other.index): |
| 71 | + self.distances[other.index] = dist(self.x, self.y, |
| 72 | + other.x, other.y) |
| 73 | + if self.distances[other.index] < maxDistance: |
| 74 | + self.angles[other.index] = Circle.findAngle(self.x, self.y, |
| 75 | + other.x, other.y) |
| 76 | + self.thetas[other.index] = (-(self.angles[other.index] * PI)) / 180.0 |
| 77 | + Circle.CXV += cos(self.thetas[other.index]) * (self.radius / 8.0) |
| 78 | + Circle.CYV += sin(self.thetas[other.index]) * (self.radius / 8.0) |
| 79 | + Circle.NumConnections += 1 |
| 80 | + else: |
| 81 | + self.hasCollided[other.index] = False |
| 82 | + other.hasCollided[self.index] = False |
| 83 | + if Circle.NumConnections > 0: |
| 84 | + self.xv += (Circle.CXV / Circle.NumConnections) / 4.0 |
| 85 | + self.yv += (Circle.CYV / Circle.NumConnections) / 4.0 |
| 86 | + Circle.CXV = 0.0 |
| 87 | + Circle.CYV = 0.0 |
| 88 | + self.radius = Circle.NumConnections * .85 + 2 |
| 89 | + |
| 90 | + def applyGravity(self, sketchXMid, sketchYMid): |
| 91 | + Circle.GravityAngle = Circle.findAngle(self.x, self.y, |
| 92 | + sketchXMid, sketchYMid) |
| 93 | + Circle.GravityTheta = (-(Circle.GravityAngle * PI)) / 180 |
| 94 | + self.xv += cos(Circle.GravityTheta) * Circle.Gravity |
| 95 | + self.yv += sin(Circle.GravityTheta) * Circle.Gravity |
| 96 | + |
| 97 | + def move(self): |
| 98 | + self.x += self.xv |
| 99 | + self.y += self.yv |
| 100 | + |
| 101 | + def render(self, others): |
| 102 | + noStroke() |
| 103 | + fill(0, 25) |
| 104 | + ellipse(self.x, self.y, self.radius, self.radius) |
| 105 | + fill(0 + self.radius * 10, 50) |
| 106 | + ellipse(self.x, self.y, self.radius * 0.5, self.radius * 0.5) |
| 107 | + fill(0 + self.radius * 10) |
| 108 | + ellipse(self.x, self.y, self.radius * 0.3, self.radius * 0.3) |
| 109 | + if Circle.NumCollisions > 0: |
| 110 | + noStroke() |
| 111 | + fill(0, 25) |
| 112 | + ellipse(self.x, self.y, self.radius, self.radius) |
| 113 | + fill(0, 55) |
| 114 | + ellipse(self.x, self.y, self.radius * 0.85, self.radius * 0.85) |
| 115 | + fill(0) |
| 116 | + ellipse(self.x, self.y, self.radius * 0.7, self.radius * 0.7) |
| 117 | + for other in others: |
| 118 | + if (self.hasCollided[other.index] |
| 119 | + and self.index != other.index): |
| 120 | + with beginShape(LINE_LOOP): |
| 121 | + xdist = self.x - other.x |
| 122 | + ydist = self.y - other.y |
| 123 | + stroke(0, 150 - self.distances[other.index] * 2.0) |
| 124 | + vertex(self.x, self.y) |
| 125 | + vertex(self.x - xdist * 0.25 + random(-1.0, 1.0), |
| 126 | + self.y - ydist * 0.25 + random(-1.0, 1.0)) |
| 127 | + vertex(self.x - xdist * 0.5 + random(-3.0, 3.0), |
| 128 | + self.y - ydist * 0.5 + random(-3.0, 3.0)) |
| 129 | + vertex(self.x - xdist * 0.75 + random(-1.0, 1.0), |
| 130 | + self.y - ydist * 0.75 + random(-1.0, 1.0)) |
| 131 | + vertex(other.x, other.y) |
| 132 | + line (self.x, self.y, other.x, other.y); |
| 133 | + noStroke() |
| 134 | + |
| 135 | + @classmethod |
| 136 | + def reset(cls): |
| 137 | + Circle.NumCollisions = 0 |
| 138 | + Circle.NumConnections = 0 |
| 139 | + |
| 140 | + @classmethod |
| 141 | + def findAngle(cls, x1, y1, x2, y2): |
| 142 | + return 180 + (-(180 * (atan2(y1 - y2, x1 - x2))) / PI) |
0 commit comments