“Mover class: Flocking” by Diana Lange (Teachings)
https://openprocessing.org/sketch/156580
License CreativeCommons Attribution ShareAlike
https://creativecommons.org/licenses/by-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
CC Attribution ShareAlike
Mover class: Flocking
Lange (Teachings)
xxxxxxxxxx
ArrayList <Mover> bouncers;
int bewegungsModus = 5;
boolean transparentBG = true;
void setup ()
{
size (600, 200);
smooth();
bouncers = new ArrayList();
for (int i = 0; i < 200; i++)
{
Mover m = new Mover();
bouncers.add (m);
}
background (#57385c);
frameRate (30);
}
void draw ()
{
if (transparentBG)
{
fill (#57385c, 40);
noStroke();
rect (0, 0, width, height);
}
else background (#57385c);
int i = 0;
while (i < bouncers.size () )
{
Mover m = bouncers.get(i);
if (bewegungsModus != 5) m.update (bewegungsModus);
else
{
m.flock (bouncers);
m.move();
m.checkEdges();
m.display();
}
i = i + 1;
}
}
class Mover
{
PVector direction;
PVector location;
float speed;
float SPEED;
float noiseScale;
float noiseStrength;
float forceStrength;
float ellipseSize;
color c;
Mover () // Konstruktor = setup der Mover Klasse
{
setRandomValues();
}
Mover (float x, float y) // Konstruktor = setup der Mover Klasse
{
setRandomValues ();
}
// SET ---------------------------
void setRandomValues ()
{
location = new PVector (random (width), random (height));
ellipseSize = random (4, 15);
float angle = random (TWO_PI);
direction = new PVector (cos (angle), sin (angle));
speed = random (4, 7);
SPEED = speed;
noiseScale = 80;
noiseStrength = 1;
forceStrength = random (0.1, 0.2);
setRandomColor();
}
void setRandomColor ()
{
int colorDice = (int) random (4);
if (colorDice == 0) c = #ffedbc;
else if (colorDice == 1) c = #A75265;
else if (colorDice == 2) c = #ec7263;
else c = #febe7e;
}
// GENEREL ------------------------------
void update ()
{
update (0);
}
void update (int mode)
{
if (mode == 0) // bouncing ball
{
speed = SPEED * 0.7;
move();
checkEdgesAndBounce();
}
else if (mode == 1) // noise
{
speed = SPEED * 0.7;
addNoise ();
move();
checkEdgesAndRelocate ();
}
else if (mode == 2) // steer
{
steer (mouseX, mouseY);
move();
}
else if (mode == 3) // seek
{
speed = SPEED * 0.7;
seek (mouseX, mouseY);
move();
}
else // radial
{
speed = SPEED * 0.7;
addRadial ();
move();
checkEdges();
}
display();
}
// FLOCK ------------------------------
void flock (ArrayList <Mover> boids)
{
PVector other;
float otherSize ;
PVector cohesionSum = new PVector (0, 0);
float cohesionCount = 0;
PVector seperationSum = new PVector (0, 0);
float seperationCount = 0;
PVector alignSum = new PVector (0, 0);
float speedSum = 0;
float alignCount = 0;
for (int i = 0; i < boids.size(); i++)
{
other = boids.get(i).location;
otherSize = boids.get(i).ellipseSize;
float distance = PVector.dist (other, location);
if (distance > 0 && distance <70) //align + cohesion
{
cohesionSum.add (other);
cohesionCount++;
alignSum.add (boids.get(i).direction);
speedSum += boids.get(i).speed;
alignCount++;
}
if (distance > 0 && distance < (ellipseSize+otherSize)*1.2) // seperate bei collision
{
float angle = atan2 (location.y-other.y, location.x-other.x);
seperationSum.add (cos (angle), sin (angle), 0);
seperationCount++;
}
if (alignCount > 8 && seperationCount > 12) break;
}
// cohesion: bewege dich in die Mitte deiner Nachbarn
// seperation: renne nicht in andere hinein
// align: bewege dich in die Richtung deiner Nachbarn
if (cohesionCount > 0)
{
cohesionSum.div (cohesionCount);
cohesion (cohesionSum, 1);
}
if (alignCount > 0)
{
speedSum /= alignCount;
alignSum.div (alignCount);
align (alignSum, speedSum, 1.3);
}
if (seperationCount > 0)
{
seperationSum.div (seperationCount);
seperation (seperationSum, 2);
}
}
void cohesion (PVector force, float strength)
{
steer (force.x, force.y, strength);
}
void seperation (PVector force, float strength)
{
force.limit (strength*forceStrength);
direction.add (force);
direction.normalize();
speed *= 1.1;
speed = constrain (speed, 0, SPEED * 1.5);
}
void align (PVector force, float forceSpeed, float strength)
{
speed = lerp (speed, forceSpeed, strength*forceStrength);
force.normalize();
force.mult (strength*forceStrength);
direction.add (force);
direction.normalize();
}
// HOW TO MOVE ----------------------------
void steer (float x, float y)
{
steer (x, y, 1);
}
void steer (float x, float y, float strength)
{
float angle = atan2 (y-location.y, x -location.x);
PVector force = new PVector (cos (angle), sin (angle));
force.mult (forceStrength * strength);
direction.add (force);
direction.normalize();
float currentDistance = dist (x, y, location.x, location.y);
if (currentDistance < 70)
{
speed = map (currentDistance, 0, 70, 0, SPEED);
}
else speed = SPEED;
}
void seek (float x, float y)
{
seek (x, y, 1);
}
void seek (float x, float y, float strength)
{
float angle = atan2 (y-location.y, x -location.x);
PVector force = new PVector (cos (angle), sin (angle));
force.mult (forceStrength * strength);
direction.add (force);
direction.normalize();
}
void addRadial ()
{
float m = noise (frameCount / (2*noiseScale));
m = map (m, 0, 1, - 1.2, 1.2);
float maxDistance = m * dist (0, 0, width/2, height/2);
float distance = dist (location.x, location.y, width/2, height/2);
float angle = map (distance, 0, maxDistance, 0, TWO_PI);
PVector force = new PVector (cos (angle), sin (angle));
force.mult (forceStrength);
direction.add (force);
direction.normalize();
}
void addNoise ()
{
float noiseValue = noise (location.x /noiseScale, location.y / noiseScale, frameCount / noiseScale);
noiseValue*= TWO_PI * noiseStrength;
PVector force = new PVector (cos (noiseValue), sin (noiseValue));
//Processing 2.0:
//PVector force = PVector.fromAngle (noiseValue);
force.mult (forceStrength);
direction.add (force);
direction.normalize();
}
// MOVE -----------------------------------------
void move ()
{
PVector velocity = direction.get();
velocity.mult (speed);
location.add (velocity);
}
// CHECK --------------------------------------------------------
void checkEdgesAndRelocate ()
{
float diameter = ellipseSize;
if (location.x < -diameter/2)
{
location.x = random (-diameter/2, width+diameter/2);
location.y = random (-diameter/2, height+diameter/2);
}
else if (location.x > width+diameter/2)
{
location.x = random (-diameter/2, width+diameter/2);
location.y = random (-diameter/2, height+diameter/2);
}
if (location.y < -diameter/2)
{
location.x = random (-diameter/2, width+diameter/2);
location.y = random (-diameter/2, height+diameter/2);
}
else if (location.y > height + diameter/2)
{
location.x = random (-diameter/2, width+diameter/2);
location.y = random (-diameter/2, height+diameter/2);
}
}
void checkEdges ()
{
float diameter = ellipseSize;
if (location.x < -diameter / 2)
{
location.x = width+diameter /2;
}
else if (location.x > width+diameter /2)
{
location.x = -diameter /2;
}
if (location.y < -diameter /2)
{
location.y = height+diameter /2;
}
else if (location.y > height+diameter /2)
{
location.y = -diameter /2;
}
}
void checkEdgesAndBounce ()
{
float radius = ellipseSize / 2;
if (location.x < radius )
{
location.x = radius ;
direction.x = direction.x * -1;
}
else if (location.x > width-radius )
{
location.x = width-radius ;
direction.x *= -1;
}
if (location.y < radius )
{
location.y = radius ;
direction.y *= -1;
}
else if (location.y > height-radius )
{
location.y = height-radius ;
direction.y *= -1;
}
}
// DISPLAY ---------------------------------------------------------------
void display ()
{
noStroke();
fill (c);
ellipse (location.x, location.y, ellipseSize, ellipseSize);
}
}
void keyPressed ()
{
if (key == ' ') transparentBG = !transparentBG;
if (key == 'n')
{
float noiseScale = random (5, 400);
float noiseStrength = random (0.5, 6);
float forceStrength = random (0.5, 4);
for (int i = 0; i < bouncers.size(); i++)
{
Mover currentMover = bouncers.get(i);
currentMover.noiseScale = noiseScale;
currentMover.noiseStrength = noiseStrength;
currentMover.forceStrength = forceStrength;
}
}
}
void mousePressed ()
{
if (mouseButton == LEFT)
{
bewegungsModus++;
if (bewegungsModus > 5)
{
bewegungsModus = 0;
}
}
}
See More Shortcuts