please format code with </> button * homework policy * asking questions
</>Mover movers = new Mover[100];
液体;
void setup(){
size(640,320);
smooth();
for(int i =0;i<movers.length;i++){
movers[i] = new Mover(random(0.1,5),0,0);
}
液体 = 新液体(0,高度,2,高度/2,0.1);
}
void draw(){
background(255);
liquid.display();
PVector wind=new PVector(0.01,0);
PVector gravity=new PVector(0,0.1);
for(int i =0;i<movers.length;i++){
if(movers[i].isInside(liquid)){
movers[i].drag(liquid);
}
浮点数 m =0.1*移动器[i].质量;
PVector gravity = new PVector(0,m);
浮点数 c =0.1;
PVector friction = movers[i].velocity.get();
friction.mult(-1);
friction.normalize();
friction.mult(c);
movers[i].applyForce(friction);
movers[i].applyForce(wind);
移动器[i].applyForce(gravity);
movers[i].update();
movers[i].display();
movers[i].checkEdges();
}
}
类移动器{
PVector 位置;
PVector 速度;
PVector 加速度;
浮子质量;
Mover(float m,float x,float y){
mass= m;
location = new PVector(x,y);
velocity = new PVector(0,0);
acceleration = new PVector(0,0);
}
void applyForce(PVector force){
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
void update(){
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display(){
stroke(0);
fill(174);
ellipse(location.x,location.y, mass16,mass16 );
}
void checkEdges(){
if(location.x>width){
location.x = width;
velocity.x*=-1;
}else if(location.x < 0){
velocity.x*=-1;
location.x = 0;
}
if(location.y>height){
velocity.y*=-1;
location.y = height;
}
}
}
类 液体{
浮子 x,y,w,h;
浮子 c;
PVector 加速度;
浮子质量;
PVector 位置;
PVector 速度;
Liquid(float x_,float y_,float w_,float h_,float c_){
x=x_;
y=y_;
w=w_;
h=h_;
c=c_;
}
void display(){
noStroke();
fill(174);
rect(x,y,w,h);
}
void applyForce(PVector force){
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
boolean isInside(Liquid l){
if(location.x>l.x && location.x<l.x+l.w && location.y>l.y && location.y<l.y+l.h){
return true;
}else{
返回 false;
}
}
void drag(Liquid l){
float speed = velocity.mag();
float dragMagnitude = l.c speedspeedspeed;
PVector drag = velocity.get();
drag.mult(-1);
drag.normalize();
drag.mult(dragMagnitude);
applyForce(drag);
}
}
</>