改变落球速度......在加工中

时间:2012-11-03 02:56:28

标签: java processing

这是一个程序,只要点击鼠标就会有球掉落和反弹。有谁知道如何改变球落入重力的速度? 我试图找出适当的解决方案...但我有点麻烦。非常感谢所有帮助和/或输入。

  float x;
  float y;
  float yspeed = 0;
  float xspeed = 0;
  float balldiameter = 10;
  float ballradius = balldiameter/2;

  void setup() {
     size (400,400);
     background (255);
     fill (0);
     ellipseMode(CENTER);
     smooth();
     noStroke();
     x = width/2;
     y = height/2;
   }

   void draw() {
   mouseChecks();
   boundaryChecks();
   ballFunctions();
   keyFunctions();
   }

   void mouseChecks() {
      if (mousePressed == true) {
      x = mouseX;
      y = mouseY;
      yspeed = mouseY - pmouseY;
      xspeed = mouseX - pmouseX;
      }
    }

  void boundaryChecks() {
     if (y >= height - ballradius) {
     y = height - ballradius;
     yspeed = -yspeed/1.15;
   }
   if (y <= ballradius) {
   y = ballradius;
   yspeed = -yspeed/1.35;
   }
   if (x >= width -ballradius) {
     x = width -ballradius;
     xspeed = -xspeed/1.10;
   }
   if (x <= ballradius) {
     x = ballradius;
     xspeed = -xspeed/1.10;
     }
    }

 void ballFunctions() {
    if (balldiameter < 2) {
     balldiameter = 2;
    }
   if (balldiameter > 400) {
     balldiameter = 400;
    }
   ballradius = balldiameter/2;
   background(255); //should this be in here?
   ellipse (x,y,balldiameter,balldiameter);
   yspeed = yspeed += 0.2;
   xspeed = xspeed/1.005;
   y = y + yspeed;
   x = x + xspeed;
  }
 void keyFunctions() {
    if (keyPressed) {
    if(keyCode == UP) {
    balldiameter +=1;
   }
  if (keyCode == DOWN) {
   balldiameter -=1;
   }
  }
 }

1 个答案:

答案 0 :(得分:5)

地球上的重力加速度为9.81 m / s ^ 2。因此,如果点击鼠标时球的速度为0,则最终速度将是与时间相关的加速度。这将是(9.81 * t)/ 2.其中t以秒为单位。得到的单位为m / sec。您必须将米转换为某个屏幕空间单位才能进行绘图。