首页 > Sktech > 发放第三期BOSS的源代码
2017
08-04

发放第三期BOSS的源代码

FlowField flow;
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);
}
}

@虎牙



最后编辑:
作者:虎牙拯救世界
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。