图形化模拟现实生活中的力
移动的物体类:
//移动的物体
class Mover{
PVector location; //物体的位置
PVector velocity; //物体的速度
PVector acceleration; //物体的加速度
float mass; //物体的质量
float G; //万有引力常量,对于常量用大小为 1 表示
Mover(float m_, float x_, float y_){
location = new PVector(x_, y_);
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
mass = m_;
G = 1;
}
Mover(){
location = new PVector(random(width), random(height));
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
mass = 1;
G = 1;
}
//对物体添加力的作用,改变物体的加速度
void applyForce(PVector force){
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
//更新速度 v_new = v_old + a(加速度),更新位置,更新加速度为 0
void update(){
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display(){
stroke(0);
fill(175);
ellipse(location.x, location.y, mass*16, mass*16);
}
//检测边界窗口边界
void checkEdges(){
if(location.x > width){
location.x = width;
velocity.x *= -1;
}else if(location.x < 0){
location.x = 0;
velocity.x *= -1;
}
if(location.y > height){
location.y =