如何对弹跳球进行阻尼和摩擦?

时间:2019-04-24 23:38:13

标签: java processing

我刚刚用弹跳球编写了代码。它工作正常,但不会减慢速度。我想要的是放一个摩擦(20%),每次当球在边缘反弹时,它应该放慢速度(0.2%)直到停止并变黑。

我的代码:

float posX = 300;
float posY = 200;
float speedY= 2;
float speedX = 5;
float ballsize = 25;
float damping = 0.2;
color red = color(255,0,0);
color green = color(0,255,0);
color black = color(0);
color ballcolor; 


void setup() {
  size(600, 400);
  background(191);
  surface.setTitle("ball");
  surface.setLocation((displayWidth-width)>>1, (displayHeight-height)>>1);
}
void draw() {
background(191);
  posX = posX + speedX;
  posY = posY + speedY;

  if (posX > width) {
    speedX = speedX* -1;
    ballcolor = (ballcolor == red ? green : red);
  }

  if (posX < 0) {
    speedX = speedX* -1;
    ballcolor = (ballcolor == red ? green : red);
  }

  if (posY > height) {
    speedY= speedY* -1;
    ballcolor = (ballcolor == red ? green : red);
  }

  if (posY < 0) {
    speedY= speedY* -1;
    ballcolor = (ballcolor == red ? green : red);
  }

  if (abs(speedY) < 0.1 && abs(speedX) < 0.1) {
 speedX = 0;
 speedY = 0;
}

  fill(ballcolor);
  ellipse(posX, posY, ballsize, ballsize);
}

0 个答案:

没有答案