1

这是一个程序,它在鼠标点击的任何地方都有一个球落下和弹跳。有谁知道如何改变球在重力作用下下落的速度?我试图找出正确的解决方案......但我遇到了一些麻烦。所有帮助和/或输入将不胜感激。

  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;
   }
  }
 }
4

1 回答 1

5

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

于 2012-11-03T03:08:14.663 回答