簡單的重力模擬:在Processing中做一個彈跳的球


效果演示:

Processing 代碼:

// simple emulation of gravity

float x;
float y;    // y(t)
float speed = 0;
float g = 0.1;  // gravity

void setup(){
  size(640, 480);
  x = width/2;
  y = height/2;  // start from half height
}

void draw(){
  // draw the ball
  background(255);
  fill(0);
  noStroke();
  ellipse(x,y,10,10);
  
  // move and accelerate
  y += speed;
  speed += g;
  
  // bounce back up
  if(y>height){
    speed *= -0.95;  // add resistance to each bounce
    y = height;      // make sure that the ball can bounce back up
  }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM