简单的重力模拟:在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