ArrayList<Vehicle> vlist;
PVector center;
void setup() {
size(1200, 600);
flow = new FlowField();
vlist = new ArrayList<Vehicle>();
for (int i = 0; i<70; i++) {
vlist.add(new Vehicle(random(width), random(height)));
}
center = new PVector(width/2,height/2);
}
void draw() {
background(175);
for (Vehicle v : vlist) {
v.follow(flow);
v.run(center,flow);
}
}
void mousePressed(){
vlist.add(new Vehicle(mouseX,mouseY));
}
class FlowField {
PVector [][] field;
int cols, rows;
int sulotion;
FlowField() {
sulotion = 10;
cols = width/sulotion;
rows = height/sulotion;
field = new PVector [cols][rows];
init();
}
PVector lookUp(PVector lookup){
int xrow = int(constrain(lookup.x/sulotion,0,cols-1));
int ycol = int(constrain(lookup.y/sulotion,0,rows-1));
return field[xrow][ycol].get();
}
public void init() {
for (int i=0; i<cols; i++) {
for (int j = 0; j<rows; j++) {
field[i][j] = new PVector(random(cos(0)),random(sin(1)));
}
}
}
}
class Vehicle {
PVector location;
PVector velocity;
PVector acceleration;
float maxSpeed;
float maxforce;
float mass;
float r;
float w;
float xoffset;
float yoffset;
color c;
Vehicle(float x, float y) {
location = new PVector(x, y);
acceleration = new PVector(0, 0);
velocity = new PVector(0, 0);
r = 7.0;
maxSpeed = 4;
maxforce = 0.1;
mass = 2;
xoffset = random(width);
yoffset = random(height);
c = color(random(255),random(255),random(255));
w = random(1,2);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
velocity.limit(maxSpeed);
acceleration.mult(0);
}
void applyForce(PVector f) {
PVector force= f.get();
force.div(mass);
acceleration.add(force);
}
void follow(FlowField f) {
PVector desire =f.lookUp(location);
desire.mult(maxSpeed);
PVector steer = PVector.sub(desire, velocity);
steer.limit(maxforce);
applyForce(steer);
}
public void display(PVector center, FlowField f) {
PVector temp;
temp = f.lookUp(new PVector(random(width),random(height)));
beginShape();
noFill();
stroke(c);
strokeWeight(w);
vertex(center.x, center.y);
bezierVertex(temp.x,temp.y,temp.x+xoffset,temp.y+yoffset, this.location.x, this.location.y);
endShape(CLOSE);
}
void run(PVector center, FlowField f) {
update();
display(center, f);
}
}
@虎牙
- 本文固定链接: http://iprocessing.cn/2017/08/04/发放第三期boss的源代码/
- 转载请注明: 虎牙拯救世界 于 Processing编程艺术 发表