creature creature exmaple

This commit is contained in:
gauthiier 2015-03-05 14:53:49 +01:00
parent f389f9473c
commit cb9ac6e110
7 changed files with 285 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import mathematik.Vector3f;
class Creature extends BehaviorParticle {
float _scale;
float _rotation;
public Creature(int x, int y, int r) {
super();
@ -11,25 +12,61 @@ class Creature extends BehaviorParticle {
maximumInnerForce(100);
radius(r);
_scale = 1.0;
_rotation = 0;
}
///////////////////////////////////
// display method
// + applies transformations
// + draws shape of the subclass
public void display() {
pushMatrix();
translate(position().x, position().y);
if(_rotation != 0)
rotate(_rotation);
else
rotate(getRotation());
scale(_scale);
draw_shape();
popMatrix();
}
/////////////////////////////////
// (absolute) transforms
public void rotate_to(float r) {
_rotation = r;
}
public void scale_to(float s) {
_scale = s;
}
///////////////////////////////////
// (incremental) transforms
public void rotate_increment(float r) {
_rotation += r;
}
public void scale_increment(float s) {
_scale += s;
}
private float getRotation() {
if(velocity().isNaN() || velocity().magnitude() == 0) return 0;
Vector3f v = new Vector3f(-1, 0, 0);
return velocity().angle(v);
}
///////////////////////////////////
// ** METHODS FOR SUBCLASSES **
// draws a (subclass) shape (in own coordinates)
public void draw_shape() {}
// evaluates if mx and my are inside the (subclass) shape
public boolean inside(int mx, int my){return false;}
};

View File

@ -0,0 +1,59 @@
import teilchen.Physics;
import teilchen.util.CollisionManager;
import teilchen.behavior.Arrival;
class ColonyBehavior implements CreatureBehavior {
ArrayList<Colon> _colons = new ArrayList<Colon>();
Arrival _leader;
CollisionManager _collision;
Physics _physics;
public ColonyBehavior(Physics p, CollisionManager c) {
_colons = new ArrayList<Colon>();
_leader = new Arrival();
_leader.position().set(random(width), random(height));
_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 event(int x, int y) {
_leader.position().set(x, y);
}
public void event(int c) {
if(c == 'q') {
_leader.position().set(random(width), random(height));
}
}
public void update(float dt) {;}
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);
}
}
}

View File

@ -0,0 +1,73 @@
import teilchen.BehaviorParticle;
import mathematik.Vector3f;
class Creature extends BehaviorParticle {
float _scale;
float _rotation;
public Creature(int x, int y, int r) {
super();
position().set(x, y);
maximumInnerForce(100);
radius(r);
_scale = 1.0;
_rotation = 0;
}
///////////////////////////////////
// display method
// + applies transformations
// + draws shape of the subclass
public void display() {
pushMatrix();
translate(position().x, position().y);
if(_rotation != 0)
rotate(_rotation);
else
rotate(getRotation());
scale(_scale);
draw_shape();
popMatrix();
}
/////////////////////////////////
// (absolute) transforms
public void rotate_to(float r) {
_rotation = r;
}
public void scale_to(float s) {
_scale = s;
}
///////////////////////////////////
// (incremental) transforms
public void rotate_increment(float r) {
_rotation += r;
}
public void scale_increment(float s) {
_scale += s;
}
private float getRotation() {
if(velocity().isNaN() || velocity().magnitude() == 0) return 0;
Vector3f v = new Vector3f(-1, 0, 0);
return velocity().angle(v);
}
///////////////////////////////////
// ** METHODS FOR SUBCLASSES **
// draws a (subclass) shape (in own coordinates)
public void draw_shape() {}
// evaluates if mx and my are inside the (subclass) shape
public boolean inside(int mx, int my){return false;}
};

View File

@ -0,0 +1,11 @@
interface CreatureBehavior {
public void event(int x, int y);
public void event(int c);
public void update(float dt);
public void add(Creature c);
}

View 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;
}
};

View File

@ -0,0 +1,65 @@
import teilchen.Physics;
import teilchen.util.CollisionManager;
final int CANVAS_WIDTH = 500;
final int CANVAS_HEIGHT = 500;
Physics physics;
CollisionManager collision;
CreatureBehavior 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 ColonyBehavior(physics, collision);
}
void draw() {
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 TriangleCreature(mouseX, mouseY, 10);
creatures.add(c);
colony.add(c);
}
void keyPressed() {
// Inform the colony of the KEY pressed
colony.event(key);
}

View File

@ -0,0 +1,2 @@
mode.id=processing.mode.java.JavaMode
mode=Java