spring - attractor
This commit is contained in:
parent
e772172cbc
commit
effe06d9f5
18
day3/sketch_creature_attractor/CircleCreature.pde
Normal file
18
day3/sketch_creature_attractor/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
day3/sketch_creature_attractor/Creature.pde
Normal file
37
day3/sketch_creature_attractor/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
day3/sketch_creature_attractor/SquareCreature.pde
Normal file
22
day3/sketch_creature_attractor/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
day3/sketch_creature_attractor/TriangleCreature.pde
Normal file
36
day3/sketch_creature_attractor/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
day3/sketch_creature_attractor/sketch.properties
Normal file
2
day3/sketch_creature_attractor/sketch.properties
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
mode.id=processing.mode.java.JavaMode
|
||||||
|
mode=Java
|
||||||
81
day3/sketch_creature_attractor/sketch_creature_attractor.pde
Normal file
81
day3/sketch_creature_attractor/sketch_creature_attractor.pde
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
import teilchen.Physics;
|
||||||
|
import teilchen.force.Attractor;
|
||||||
|
import teilchen.force.ViscousDrag;
|
||||||
|
|
||||||
|
final int CANVAS_WIDTH = 500;
|
||||||
|
final int CANVAS_HEIGHT = 500;
|
||||||
|
|
||||||
|
final int NBR_CREATURES = 75;
|
||||||
|
|
||||||
|
Physics physics;
|
||||||
|
Attractor attractor;
|
||||||
|
|
||||||
|
ArrayList<Creature> creatures = new ArrayList<Creature>();
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
size(CANVAS_WIDTH, CANVAS_HEIGHT);
|
||||||
|
background(23, 68, 250);
|
||||||
|
frameRate(30);
|
||||||
|
|
||||||
|
physics = new Physics();
|
||||||
|
|
||||||
|
// create a global drag
|
||||||
|
ViscousDrag drag = new ViscousDrag();
|
||||||
|
drag.coefficient = 0.75f;
|
||||||
|
physics.add(drag);
|
||||||
|
|
||||||
|
// create attaractor
|
||||||
|
attractor = new Attractor();
|
||||||
|
attractor.radius(50);
|
||||||
|
attractor.strength(200);
|
||||||
|
physics.add(attractor);
|
||||||
|
|
||||||
|
// create all creatures
|
||||||
|
Creature c;
|
||||||
|
for(int i = 0; i < NBR_CREATURES; i++) {
|
||||||
|
if(i % 3 == 0) {
|
||||||
|
c = new CircleCreature((int)random(width), (int)random(height), 20);
|
||||||
|
} else if(i % 3 == 1) {
|
||||||
|
c = new TriangleCreature((int)random(width), (int)random(height), 20);
|
||||||
|
} else {
|
||||||
|
c = new SquareCreature((int)random(width), (int)random(height), 30, 30);
|
||||||
|
}
|
||||||
|
creatures.add(c);
|
||||||
|
physics.add(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw() {
|
||||||
|
|
||||||
|
attractor.position().set(mouseX, mouseY);
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(attractor.strength() < 0) {
|
||||||
|
stroke(255, 0, 0);
|
||||||
|
fill(255, 0, 0, 35);
|
||||||
|
} else {
|
||||||
|
stroke(0, 255, 0);
|
||||||
|
fill(0, 255, 0, 35);
|
||||||
|
}
|
||||||
|
|
||||||
|
ellipse(attractor.position().x, attractor.position().y,
|
||||||
|
attractor.radius(), attractor.radius());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void mousePressed() {
|
||||||
|
float inv = -1 * attractor.strength();
|
||||||
|
attractor.strength(inv);
|
||||||
|
}
|
||||||
|
|
||||||
18
day3/sketch_creature_spring/CircleCreature.pde
Normal file
18
day3/sketch_creature_spring/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
day3/sketch_creature_spring/Creature.pde
Normal file
37
day3/sketch_creature_spring/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
day3/sketch_creature_spring/SquareCreature.pde
Normal file
22
day3/sketch_creature_spring/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
day3/sketch_creature_spring/TriangleCreature.pde
Normal file
36
day3/sketch_creature_spring/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
day3/sketch_creature_spring/sketch.properties
Normal file
2
day3/sketch_creature_spring/sketch.properties
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
mode.id=processing.mode.java.JavaMode
|
||||||
|
mode=Java
|
||||||
59
day3/sketch_creature_spring/sketch_creature_spring.pde
Normal file
59
day3/sketch_creature_spring/sketch_creature_spring.pde
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import teilchen.Physics;
|
||||||
|
import teilchen.force.Spring;
|
||||||
|
import teilchen.force.ViscousDrag;
|
||||||
|
|
||||||
|
final int CANVAS_WIDTH = 500;
|
||||||
|
final int CANVAS_HEIGHT = 500;
|
||||||
|
|
||||||
|
final int NBR_CREATURES = 75;
|
||||||
|
|
||||||
|
Physics physics;
|
||||||
|
Spring spring;
|
||||||
|
|
||||||
|
Creature t;
|
||||||
|
Creature c;
|
||||||
|
|
||||||
|
ArrayList<Creature> creatures = new ArrayList<Creature>();
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
size(CANVAS_WIDTH, CANVAS_HEIGHT);
|
||||||
|
background(23, 68, 250);
|
||||||
|
frameRate(30);
|
||||||
|
|
||||||
|
physics = new Physics();
|
||||||
|
|
||||||
|
// create a global drag
|
||||||
|
ViscousDrag drag = new ViscousDrag();
|
||||||
|
drag.coefficient = 0.25f;
|
||||||
|
physics.add(drag);
|
||||||
|
|
||||||
|
t = new TriangleCreature((int)random(width), (int)random(height), 20);
|
||||||
|
c = new CircleCreature((int)random(width), (int)random(height), 20);
|
||||||
|
|
||||||
|
physics.add(t); physics.add(c);
|
||||||
|
|
||||||
|
spring = physics.makeSpring(t, c);
|
||||||
|
spring.restlength(20);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw() {
|
||||||
|
|
||||||
|
physics.step(1.0 / frameRate);
|
||||||
|
|
||||||
|
background(23, 68, 250);
|
||||||
|
stroke(255);
|
||||||
|
noFill();
|
||||||
|
|
||||||
|
c.display();
|
||||||
|
t.display();
|
||||||
|
|
||||||
|
stroke(255, 0, 0);
|
||||||
|
line(c.position().x, c.position().y, t.position().x, t.position().y);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouseDragged() {
|
||||||
|
spring.b().position().set(mouseX, mouseY);
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user