day4
This commit is contained in:
parent
9bb85c8bfa
commit
ba6914d33b
18
day4/sketch_creature_behavior_colony/CircleCreature.pde
Normal file
18
day4/sketch_creature_behavior_colony/CircleCreature.pde
Normal file
@ -0,0 +1,18 @@
|
||||
class CircleCreature extends Creature {
|
||||
|
||||
public CircleCreature(int x, int y, int r) {
|
||||
super(x, y, r);
|
||||
ellipseMode(RADIUS);
|
||||
}
|
||||
|
||||
public void draw_shape(){
|
||||
ellipse(0, 0, radius(), radius());
|
||||
line(0, 0, radius(), 0);
|
||||
}
|
||||
|
||||
public boolean inside(int mx, int my) {
|
||||
if(dist(mx, my, position().x, position().y) < radius()) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
52
day4/sketch_creature_behavior_colony/Colony.pde
Normal file
52
day4/sketch_creature_behavior_colony/Colony.pde
Normal file
@ -0,0 +1,52 @@
|
||||
import teilchen.behavior.Arrival;
|
||||
|
||||
class Colony {
|
||||
|
||||
ArrayList<Colon> _colons = new ArrayList<Colon>();
|
||||
Arrival _leader;
|
||||
CollisionManager _collision;
|
||||
|
||||
Physics _physics;
|
||||
|
||||
public Colony(Physics p, CollisionManager c) {
|
||||
_colons = new ArrayList<Colon>();
|
||||
_leader = new Arrival();
|
||||
_physics = p;
|
||||
_collision = c;
|
||||
}
|
||||
|
||||
public void add(Creature c) {
|
||||
|
||||
_physics.add(c);
|
||||
_collision.collision().add(c);
|
||||
|
||||
Colon colon = new Colon(c);
|
||||
if(_colons.size() >= 1) {
|
||||
colon.a.setPositionRef(_colons.get(_colons.size()-1).creature.position());
|
||||
} else if(_colons.isEmpty()) {
|
||||
colon.a.setPositionRef(_leader.position());
|
||||
}
|
||||
_colons.add(colon);
|
||||
}
|
||||
|
||||
public void migrate_to(int x, int y) {
|
||||
_leader.position().set(x, y);
|
||||
}
|
||||
|
||||
public void remove(Creature c) {
|
||||
// todo
|
||||
}
|
||||
|
||||
class Colon {
|
||||
Creature creature;
|
||||
Arrival a;
|
||||
public Colon(Creature colon) {
|
||||
creature = colon;
|
||||
a = new Arrival();
|
||||
a.breakforce(creature.radius() * 5);
|
||||
a.breakradius(creature.radius() * 5);
|
||||
creature.behaviors().add(a);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
37
day4/sketch_creature_behavior_colony/Creature.pde
Normal file
37
day4/sketch_creature_behavior_colony/Creature.pde
Normal file
@ -0,0 +1,37 @@
|
||||
import teilchen.BehaviorParticle;
|
||||
import mathematik.Vector3f;
|
||||
|
||||
class Creature extends BehaviorParticle {
|
||||
|
||||
float _scale;
|
||||
|
||||
public Creature(int x, int y, int r) {
|
||||
super();
|
||||
position().set(x, y);
|
||||
maximumInnerForce(100);
|
||||
radius(r);
|
||||
_scale = 1.0;
|
||||
}
|
||||
|
||||
public void display() {
|
||||
pushMatrix();
|
||||
translate(position().x, position().y);
|
||||
rotate(getRotation());
|
||||
scale(_scale);
|
||||
draw_shape();
|
||||
popMatrix();
|
||||
|
||||
}
|
||||
|
||||
private float getRotation() {
|
||||
if(velocity().isNaN() || velocity().magnitude() == 0) return 0;
|
||||
Vector3f v = new Vector3f(-1, 0, 0);
|
||||
return velocity().angle(v);
|
||||
}
|
||||
|
||||
public void draw_shape() {}
|
||||
|
||||
public boolean inside(int mx, int my){return false;}
|
||||
|
||||
};
|
||||
|
||||
22
day4/sketch_creature_behavior_colony/SquareCreature.pde
Normal file
22
day4/sketch_creature_behavior_colony/SquareCreature.pde
Normal file
@ -0,0 +1,22 @@
|
||||
class SquareCreature extends Creature {
|
||||
|
||||
int _width;
|
||||
int _height;
|
||||
|
||||
public SquareCreature(int x, int y, int w, int h) {
|
||||
super(x, y, (int)sqrt(pow(w / 2, 2) + pow(h / 2, 2)));
|
||||
_width = w;
|
||||
_height = h;
|
||||
rectMode(CENTER);
|
||||
}
|
||||
|
||||
public void draw_shape(){
|
||||
rect(0, 0, _width, _height);
|
||||
}
|
||||
|
||||
public boolean inside(int mx, int my) {
|
||||
if(dist(mx, my, position().x, position().y) < radius()) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
36
day4/sketch_creature_behavior_colony/TriangleCreature.pde
Normal file
36
day4/sketch_creature_behavior_colony/TriangleCreature.pde
Normal file
@ -0,0 +1,36 @@
|
||||
class TriangleCreature extends Creature {
|
||||
|
||||
PVector A, B, C;
|
||||
|
||||
public TriangleCreature(int x, int y, int r) {
|
||||
super(x, y, r);
|
||||
|
||||
A = new PVector();
|
||||
B = new PVector();
|
||||
C = new PVector();
|
||||
|
||||
float theta = TWO_PI/3;
|
||||
|
||||
A.x = cos(0 * theta) * r;
|
||||
A.y = sin(0 * theta) * r;
|
||||
|
||||
B.x = cos(1 * theta) * r;
|
||||
B.y = sin(1 * theta) * r;
|
||||
|
||||
C.x = cos(2 * theta) * r;
|
||||
C.y = sin(2 * theta) * r;
|
||||
|
||||
}
|
||||
|
||||
public void draw_shape(){
|
||||
triangle(A.x, A.y, B.x, B.y, C.x, C.y);
|
||||
line(0, 0, A.x, 0);
|
||||
}
|
||||
|
||||
public boolean inside(int mx, int my) {
|
||||
if(dist(mx, my, position().x, position().y) < radius()) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
2
day4/sketch_creature_behavior_colony/sketch.properties
Normal file
2
day4/sketch_creature_behavior_colony/sketch.properties
Normal file
@ -0,0 +1,2 @@
|
||||
mode.id=processing.mode.java.JavaMode
|
||||
mode=Java
|
||||
@ -0,0 +1,60 @@
|
||||
import teilchen.Physics;
|
||||
import teilchen.util.CollisionManager;
|
||||
|
||||
final int CANVAS_WIDTH = 500;
|
||||
final int CANVAS_HEIGHT = 500;
|
||||
|
||||
Physics physics;
|
||||
|
||||
CollisionManager collision;
|
||||
|
||||
Colony colony;
|
||||
|
||||
ArrayList<Creature> creatures = new ArrayList<Creature>();
|
||||
|
||||
void setup() {
|
||||
size(CANVAS_WIDTH, CANVAS_HEIGHT);
|
||||
background(23, 68, 250);
|
||||
frameRate(30);
|
||||
|
||||
physics = new Physics();
|
||||
|
||||
collision = new CollisionManager();
|
||||
collision.minimumDistance(25);
|
||||
|
||||
colony = new Colony(physics, collision);
|
||||
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
colony.migrate_to(mouseX, mouseY);
|
||||
|
||||
collision.createCollisionResolvers();
|
||||
collision.loop(1.0 / frameRate);
|
||||
|
||||
physics.step(1.0 / frameRate);
|
||||
|
||||
background(23, 68, 250);
|
||||
stroke(255);
|
||||
noFill();
|
||||
|
||||
for(int i = 0; i < creatures.size(); i++) {
|
||||
Creature c = creatures.get(i);
|
||||
c.display();
|
||||
}
|
||||
|
||||
collision.removeCollisionResolver();
|
||||
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
|
||||
Creature c = new CircleCreature(mouseX, mouseY, 10);
|
||||
|
||||
creatures.add(c);
|
||||
|
||||
colony.add(c);
|
||||
|
||||
}
|
||||
|
||||
18
day4/sketch_creature_behavior_herd/CircleCreature.pde
Normal file
18
day4/sketch_creature_behavior_herd/CircleCreature.pde
Normal file
@ -0,0 +1,18 @@
|
||||
class CircleCreature extends Creature {
|
||||
|
||||
public CircleCreature(int x, int y, int r) {
|
||||
super(x, y, r);
|
||||
ellipseMode(RADIUS);
|
||||
}
|
||||
|
||||
public void draw_shape(){
|
||||
ellipse(0, 0, radius(), radius());
|
||||
line(0, 0, radius(), 0);
|
||||
}
|
||||
|
||||
public boolean inside(int mx, int my) {
|
||||
if(dist(mx, my, position().x, position().y) < radius()) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
37
day4/sketch_creature_behavior_herd/Creature.pde
Normal file
37
day4/sketch_creature_behavior_herd/Creature.pde
Normal file
@ -0,0 +1,37 @@
|
||||
import teilchen.BehaviorParticle;
|
||||
import mathematik.Vector3f;
|
||||
|
||||
class Creature extends BehaviorParticle {
|
||||
|
||||
float _scale;
|
||||
|
||||
public Creature(int x, int y, int r) {
|
||||
super();
|
||||
position().set(x, y);
|
||||
maximumInnerForce(100);
|
||||
radius(r);
|
||||
_scale = 1.0;
|
||||
}
|
||||
|
||||
public void display() {
|
||||
pushMatrix();
|
||||
translate(position().x, position().y);
|
||||
rotate(getRotation());
|
||||
scale(_scale);
|
||||
draw_shape();
|
||||
popMatrix();
|
||||
|
||||
}
|
||||
|
||||
private float getRotation() {
|
||||
if(velocity().isNaN() || velocity().magnitude() == 0) return 0;
|
||||
Vector3f v = new Vector3f(-1, 0, 0);
|
||||
return velocity().angle(v);
|
||||
}
|
||||
|
||||
public void draw_shape() {}
|
||||
|
||||
public boolean inside(int mx, int my){return false;}
|
||||
|
||||
};
|
||||
|
||||
85
day4/sketch_creature_behavior_herd/Herd.pde
Normal file
85
day4/sketch_creature_behavior_herd/Herd.pde
Normal file
@ -0,0 +1,85 @@
|
||||
import teilchen.Physics;
|
||||
import teilchen.behavior.Alignment;
|
||||
import teilchen.behavior.Cohesion;
|
||||
import teilchen.behavior.Motor;
|
||||
import teilchen.behavior.Separation;
|
||||
import teilchen.behavior.Wander;
|
||||
import java.util.Vector;
|
||||
import teilchen.IBehaviorParticle;
|
||||
|
||||
class Herd {
|
||||
|
||||
ArrayList<Herderer> _herderers = new ArrayList<Herderer>();
|
||||
Vector<Creature> _creatures = new Vector<Creature>();
|
||||
CollisionManager _collision;
|
||||
|
||||
Physics _physics;
|
||||
|
||||
public Herd(Physics p, CollisionManager c) {
|
||||
_herderers = new ArrayList<Herderer>();
|
||||
_physics = p;
|
||||
_collision = c;
|
||||
}
|
||||
|
||||
public void add(Creature c) {
|
||||
|
||||
_physics.add(c);
|
||||
_collision.collision().add(c);
|
||||
_creatures.add(c);
|
||||
|
||||
Herderer h = new Herderer(c);
|
||||
_herderers.add(h);
|
||||
}
|
||||
|
||||
public void remove(Creature c) {
|
||||
// todo
|
||||
}
|
||||
|
||||
public void update() {
|
||||
for(int i = 0; i < _herderers.size(); i++) {
|
||||
_herderers.get(i).update();
|
||||
}
|
||||
}
|
||||
|
||||
class Herderer {
|
||||
Creature creature;
|
||||
Separation separation;
|
||||
Alignment alignment;
|
||||
Cohesion cohesion;
|
||||
Wander wander;
|
||||
Motor motor;
|
||||
|
||||
public Herderer(Creature c) {
|
||||
|
||||
creature = c;
|
||||
|
||||
separation = new Separation();
|
||||
separation.proximity(10);
|
||||
separation.weight(10.0f);
|
||||
creature.behaviors().add(separation);
|
||||
|
||||
cohesion = new Cohesion();
|
||||
cohesion.proximity(500);
|
||||
cohesion.weight(5.0f);
|
||||
creature.behaviors().add(cohesion);
|
||||
|
||||
wander = new Wander();
|
||||
creature.behaviors().add(wander);
|
||||
|
||||
/*
|
||||
motor = new Motor();
|
||||
motor.auto_update_direction(true);
|
||||
motor.strength(0.01f);
|
||||
creature.behaviors().add(motor);
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
public void update() {
|
||||
separation.neighbors(_creatures);
|
||||
cohesion.neighbors(_creatures);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
22
day4/sketch_creature_behavior_herd/SquareCreature.pde
Normal file
22
day4/sketch_creature_behavior_herd/SquareCreature.pde
Normal file
@ -0,0 +1,22 @@
|
||||
class SquareCreature extends Creature {
|
||||
|
||||
int _width;
|
||||
int _height;
|
||||
|
||||
public SquareCreature(int x, int y, int w, int h) {
|
||||
super(x, y, (int)sqrt(pow(w / 2, 2) + pow(h / 2, 2)));
|
||||
_width = w;
|
||||
_height = h;
|
||||
rectMode(CENTER);
|
||||
}
|
||||
|
||||
public void draw_shape(){
|
||||
rect(0, 0, _width, _height);
|
||||
}
|
||||
|
||||
public boolean inside(int mx, int my) {
|
||||
if(dist(mx, my, position().x, position().y) < radius()) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
36
day4/sketch_creature_behavior_herd/TriangleCreature.pde
Normal file
36
day4/sketch_creature_behavior_herd/TriangleCreature.pde
Normal file
@ -0,0 +1,36 @@
|
||||
class TriangleCreature extends Creature {
|
||||
|
||||
PVector A, B, C;
|
||||
|
||||
public TriangleCreature(int x, int y, int r) {
|
||||
super(x, y, r);
|
||||
|
||||
A = new PVector();
|
||||
B = new PVector();
|
||||
C = new PVector();
|
||||
|
||||
float theta = TWO_PI/3;
|
||||
|
||||
A.x = cos(0 * theta) * r;
|
||||
A.y = sin(0 * theta) * r;
|
||||
|
||||
B.x = cos(1 * theta) * r;
|
||||
B.y = sin(1 * theta) * r;
|
||||
|
||||
C.x = cos(2 * theta) * r;
|
||||
C.y = sin(2 * theta) * r;
|
||||
|
||||
}
|
||||
|
||||
public void draw_shape(){
|
||||
triangle(A.x, A.y, B.x, B.y, C.x, C.y);
|
||||
line(0, 0, A.x, 0);
|
||||
}
|
||||
|
||||
public boolean inside(int mx, int my) {
|
||||
if(dist(mx, my, position().x, position().y) < radius()) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
2
day4/sketch_creature_behavior_herd/sketch.properties
Normal file
2
day4/sketch_creature_behavior_herd/sketch.properties
Normal file
@ -0,0 +1,2 @@
|
||||
mode.id=processing.mode.java.JavaMode
|
||||
mode=Java
|
||||
@ -0,0 +1,60 @@
|
||||
import teilchen.Physics;
|
||||
import teilchen.util.CollisionManager;
|
||||
|
||||
final int CANVAS_WIDTH = 500;
|
||||
final int CANVAS_HEIGHT = 500;
|
||||
|
||||
Physics physics;
|
||||
|
||||
CollisionManager collision;
|
||||
|
||||
Herd herd;
|
||||
|
||||
ArrayList<Creature> creatures = new ArrayList<Creature>();
|
||||
|
||||
void setup() {
|
||||
size(CANVAS_WIDTH, CANVAS_HEIGHT);
|
||||
background(23, 68, 250);
|
||||
frameRate(30);
|
||||
|
||||
physics = new Physics();
|
||||
|
||||
collision = new CollisionManager();
|
||||
collision.minimumDistance(25);
|
||||
|
||||
herd = new Herd(physics, collision);
|
||||
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
||||
collision.createCollisionResolvers();
|
||||
collision.loop(1.0 / frameRate);
|
||||
|
||||
physics.step(1.0 / frameRate);
|
||||
|
||||
herd.update();
|
||||
|
||||
background(23, 68, 250);
|
||||
stroke(255);
|
||||
noFill();
|
||||
|
||||
for(int i = 0; i < creatures.size(); i++) {
|
||||
Creature c = creatures.get(i);
|
||||
c.display();
|
||||
}
|
||||
|
||||
collision.removeCollisionResolver();
|
||||
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
|
||||
Creature c = new CircleCreature(mouseX, mouseY, 10);
|
||||
|
||||
creatures.add(c);
|
||||
|
||||
herd.add(c);
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user