| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,10 +9,17 @@ class Boid { | |||
| 9 | 9 | float maxforce; // Maximum steering force | |
| 10 | 10 | float maxspeed; // Maximum speed | |
| 11 | 11 | ||
| 12 | - Boid(float x, float y) { | ||
| 13 | - acceleration = new PVector(0,0); | ||
| 14 | - velocity = PVector.random2D(); | ||
| 15 | - location = new PVector(x,y); | ||
| 12 | + Boid(float x, float y) { | ||
| 13 | + acceleration = new PVector(0, 0); | ||
| 14 | + | ||
| 15 | + // This is a new PVector method not yet implemented in JS | ||
| 16 | + // velocity = PVector.random2D(); | ||
| 17 | + | ||
| 18 | + // Leaving the code temporarily this way so that this example runs in JS | ||
| 19 | + float angle = random(TWO_PI); | ||
| 20 | + velocity = new PVector(cos(angle), sin(angle)); | ||
| 21 | + | ||
| 22 | + location = new PVector(x, y); | ||
| 16 | 23 | r = 2.0; | |
| 17 | 24 | maxspeed = 2; | |
| 18 | 25 | maxforce = 0.03; | |
@@ -59,22 +66,30 @@ class Boid { | |||
| 59 | 66 | // A method that calculates and applies a steering force towards a target | |
| 60 | 67 | // STEER = DESIRED MINUS VELOCITY | |
| 61 | 68 | PVector seek(PVector target) { | |
| 62 | - PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target | ||
| 69 | + PVector desired = PVector.sub(target, location); // A vector pointing from the location to the target | ||
| 63 | 70 | // Scale to maximum speed | |
| 64 | - desired.setMag(maxspeed); | ||
| 71 | + desired.normalize(); | ||
| 72 | + desired.mult(maxspeed); | ||
| 73 | + | ||
| 74 | + // Above two lines of code below could be condensed with new PVector setMag() method | ||
| 75 | + // Not using this method until Processing.js catches up | ||
| 76 | + // desired.setMag(maxspeed); | ||
| 77 | + | ||
| 65 | 78 | // Steering = Desired minus Velocity | |
| 66 | - PVector steer = PVector.sub(desired,velocity); | ||
| 79 | + PVector steer = PVector.sub(desired, velocity); | ||
| 67 | 80 | steer.limit(maxforce); // Limit to maximum steering force | |
| 68 | 81 | return steer; | |
| 69 | 82 | } | |
| 70 | 83 | ||
| 71 | 84 | void render() { | |
| 72 | 85 | // Draw a triangle rotated in the direction of velocity | |
| 73 | - float theta = velocity.heading() + radians(90); | ||
| 74 | - fill(200,100); | ||
| 86 | + float theta = velocity.heading2D() + radians(90); | ||
| 87 | + // heading2D() above is now heading() but leaving old syntax until Processing.js catches up | ||
| 88 | + | ||
| 89 | + fill(200, 100); | ||
| 75 | 90 | stroke(255); | |
| 76 | 91 | pushMatrix(); | |
| 77 | - translate(location.x,location.y); | ||
| 92 | + translate(location.x, location.y); | ||
| 78 | 93 | rotate(theta); | |
| 79 | 94 | beginShape(TRIANGLES); | |
| 80 | 95 | vertex(0, -r*2); | |
@@ -96,15 +111,15 @@ class Boid { | |||
| 96 | 111 | // Method checks for nearby boids and steers away | |
| 97 | 112 | PVector separate (ArrayList<Boid> boids) { | |
| 98 | 113 | float desiredseparation = 25.0f; | |
| 99 | - PVector steer = new PVector(0,0,0); | ||
| 114 | + PVector steer = new PVector(0, 0, 0); | ||
| 100 | 115 | int count = 0; | |
| 101 | 116 | // For every boid in the system, check if it's too close | |
| 102 | 117 | for (Boid other : boids) { | |
| 103 | - float d = PVector.dist(location,other.location); | ||
| 118 | + float d = PVector.dist(location, other.location); | ||
| 104 | 119 | // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself) | |
| 105 | 120 | if ((d > 0) && (d < desiredseparation)) { | |
| 106 | 121 | // Calculate vector pointing away from neighbor | |
| 107 | - PVector diff = PVector.sub(location,other.location); | ||
| 122 | + PVector diff = PVector.sub(location, other.location); | ||
| 108 | 123 | diff.normalize(); | |
| 109 | 124 | diff.div(d); // Weight by distance | |
| 110 | 125 | steer.add(diff); | |
@@ -118,8 +133,13 @@ class Boid { | |||
| 118 | 133 | ||
| 119 | 134 | // As long as the vector is greater than 0 | |
| 120 | 135 | if (steer.mag() > 0) { | |
| 136 | + // First two lines of code below could be condensed with new PVector setMag() method | ||
| 137 | + // Not using this method until Processing.js catches up | ||
| 138 | + // steer.setMag(maxspeed); | ||
| 139 | + | ||
| 121 | 140 | // Implement Reynolds: Steering = Desired - Velocity | |
| 122 | - steer.setMag(maxspeed); | ||
| 141 | + steer.normalize(); | ||
| 142 | + steer.mult(maxspeed); | ||
| 123 | 143 | steer.sub(velocity); | |
| 124 | 144 | steer.limit(maxforce); | |
| 125 | 145 | } | |
@@ -130,34 +150,41 @@ class Boid { | |||
| 130 | 150 | // For every nearby boid in the system, calculate the average velocity | |
| 131 | 151 | PVector align (ArrayList<Boid> boids) { | |
| 132 | 152 | float neighbordist = 50; | |
| 133 | - PVector sum = new PVector(0,0); | ||
| 153 | + PVector sum = new PVector(0, 0); | ||
| 134 | 154 | int count = 0; | |
| 135 | 155 | for (Boid other : boids) { | |
| 136 | - float d = PVector.dist(location,other.location); | ||
| 156 | + float d = PVector.dist(location, other.location); | ||
| 137 | 157 | if ((d > 0) && (d < neighbordist)) { | |
| 138 | 158 | sum.add(other.velocity); | |
| 139 | 159 | count++; | |
| 140 | 160 | } | |
| 141 | 161 | } | |
| 142 | 162 | if (count > 0) { | |
| 143 | 163 | sum.div((float)count); | |
| 144 | - sum.setMag(maxspeed); | ||
| 145 | - PVector steer = PVector.sub(sum,velocity); | ||
| 164 | + // First two lines of code below could be condensed with new PVector setMag() method | ||
| 165 | + // Not using this method until Processing.js catches up | ||
| 166 | + // sum.setMag(maxspeed); | ||
| 167 | + | ||
| 168 | + // Implement Reynolds: Steering = Desired - Velocity | ||
| 169 | + sum.normalize(); | ||
| 170 | + sum.mult(maxspeed); | ||
| 171 | + PVector steer = PVector.sub(sum, velocity); | ||
| 146 | 172 | steer.limit(maxforce); | |
| 147 | 173 | return steer; | |
| 148 | - } else { | ||
| 149 | - return new PVector(0,0); | ||
| 174 | + } | ||
| 175 | + else { | ||
| 176 | + return new PVector(0, 0); | ||
| 150 | 177 | } | |
| 151 | 178 | } | |
| 152 | 179 | ||
| 153 | 180 | // Cohesion | |
| 154 | 181 | // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location | |
| 155 | 182 | PVector cohesion (ArrayList<Boid> boids) { | |
| 156 | 183 | float neighbordist = 50; | |
| 157 | - PVector sum = new PVector(0,0); // Start with empty vector to accumulate all locations | ||
| 184 | + PVector sum = new PVector(0, 0); // Start with empty vector to accumulate all locations | ||
| 158 | 185 | int count = 0; | |
| 159 | 186 | for (Boid other : boids) { | |
| 160 | - float d = PVector.dist(location,other.location); | ||
| 187 | + float d = PVector.dist(location, other.location); | ||
| 161 | 188 | if ((d > 0) && (d < neighbordist)) { | |
| 162 | 189 | sum.add(other.location); // Add location | |
| 163 | 190 | count++; | |
@@ -166,10 +193,10 @@ class Boid { | |||
| 166 | 193 | if (count > 0) { | |
| 167 | 194 | sum.div(count); | |
| 168 | 195 | return seek(sum); // Steer towards the location | |
| 169 | - } else { | ||
| 170 | - return new PVector(0,0); | ||
| 196 | + } | ||
| 197 | + else { | ||
| 198 | + return new PVector(0, 0); | ||
| 171 | 199 | } | |
| 172 | 200 | } | |
| 173 | 201 | } | |
| 174 | 202 | ||
| 175 | - | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments