
void setup(){
size(300,300);
}
float x = 200 , y = 150;
float vx = 0 , vy = 0;
void draw(){
background(#002966);
stroke(#b7efff);
line(100,150 , x,y);
fill(#b7efff);
noStroke(); //圖案變成無外框
ellipse(100,150 , 13,13);
ellipse(x,y , 13,13);
// <物理力學>
if(!mousePressed){ //滑鼠沒按
float dx = x-100, dy = y-150;
float len = sqrt(dx*dx + dy*dy);
// <虎克定律(Hooke's law)>
vx -= dx*(len-100)/len*0.01; // *彈性係數
vy -= dy*(len-100)/len*0.01; // 越大彈簧越快
x += vx;
y += vy;
// </虎克定律(Hooke's law)>
}
// </物理力學>
}
void mouseDragged(){ //滑鼠拖曳
x = mouseX;
y = mouseY;
vx = 0;
vy = 0;
}
沒有留言:
張貼留言