球在处理中远离光标项目

balls moving away from cursor project in processing

我正在尝试制作一个程序,该程序使用一个数组来显示 100 个球和矢量,使这些球远离光标。由于知识匮乏,我把向量注释掉了,但是当我使用处理ide时,仍然所有100个球都卡在左上角。同样的代码(没有向量)在 repl.it 上使用处理。

{
  float x;
  float y;
  float r1;
  float b1;
  float g1;
  float d;
  float xSpeed;
  float ySpeed;
  /*PVector mouseLocation;
  PVector ballLocation;
  PVector ballVelocity;
  PVector repulsionForce;
  float distanceFromMouse = repulsionForce.mag();*/
  
  ball(float x,float y)
  {
   
    d = random(10,30);
    r1= random(0,255);
    b1= random(0,255);
    g1= random(0,255);
    xSpeed = random(-4,4);
    ySpeed = random(-4,4);
   /* PVector mouseLocation = new PVector(mouseX,mouseY);
    PVector ballLocation = new PVector(x,y);
    PVector ballVelocity = new PVector(xSpeed,ySpeed);
     PVector repulsionForce = PVector.sub(ballLocation,mouseLocation); */
   
  
  }
  void render()
  {
    fill(r1,g1,b1);
    ellipse(x,y,d,d);
  }
  void move()
  {
    //ballLocation.add(ballVelocity);
    x = x + xSpeed;
    y = y + ySpeed;
  }
  void bounce()
  {
    if(x > width - d/2)
    {
      xSpeed = -xSpeed;
    }

    if(x < 0 + d/2)
    {
      xSpeed = -xSpeed;
    }

    if(y > height - d/2)
    {
      ySpeed = -ySpeed;
    }

    if(y < 0 + d/2)
    {
      ySpeed = -ySpeed;
    }
  }
  void curs()
  {
    
    /* if (distanceFromMouse < 100) {
     repulsionForce.normalize();
     repulsionForce.mult(map(distanceFromMouse, 0, 100, 2, 0));
     ballVelocity.add(repulsionForce);
    }
    ballLocation.add(repulsionForce); */
  }
}

ball[] balls = new ball[100];
float x;
float y;
void setup()
{
  size(600, 400);
  x = random(0,width);
  y = random(0,height);
  for(int i=0; i < 100; i++)
  {
    balls[i] = new ball(x,y);
  }
}

void draw()
{
  background(50);
  for(int i = 0; i < 100; i++)
  {
    balls[i].render();
    balls[i].move();
    balls[i].bounce();
    balls[i].curs();
  }
}```

在这里,我修复了你的代码。结果如下:

您的主要问题是 scope。当您有多个同名变量时,作用域将决定哪个变量覆盖哪个变量,这里面会发生很多混乱。此外,您似乎并不完全了解所有这些坐标是怎么回事。您不需要 x y 花车来跟踪球的位置,因为您已经有一个 PVector ballLocation 可以为您完成。

所以这就是我所做的:一旦我意识到发生了什么,我...

  1. 消除了所有不需要的 xy 变量,并用旨在完成相同工作的 PVectors 替换它们。

  2. 重新组织了球class的更新顺序。我将其设置为 move -> render 而不是 render -> move -> bounce -> curs,同时确保 move 包含按以下顺序调用 bouncecursbounce -> curs -> move。这个想法是,如果您按以下顺序跟踪球离开屏幕等边缘情况会更容易:计算它们是否需要改变方向,计算速度变化,应用位置变化,然后将其绘制在屏幕上。

  3. 修改 curs 所以它改变了球的速度,而不是它的位置。我集中了位置的修改,所以一切都会影响速度,所以你必须只在一个地方检查边缘情况。如果屏幕上有 2 个鼠标光标,两者都会通过对球施加压力而不是四处移动来影响球的位置。

  4. 必须在每一帧为每个球重新计算repulsionForce,否则它们将永远不会考虑鼠标的当前位置。修正了那个。

  5. 当一个球试图离开屏幕时,添加了一个不利的动作,这样它们就不会消失在屏幕外。这不是当前的问题,但在某个时候它会成为一个问题,所以我在你必须处理它之前解决了它。

代码如下:

Ball[] balls = new Ball[100];

void setup()
{
  size(600, 400);
  for (int i=0; i < 100; i++)
  {
    balls[i] = new Ball(random(width), random(height));
  }
}

void draw()
{
  background(50);
  for (int i = 0; i < 100; i++)
  {
    balls[i].move();
    balls[i].render();
  }
}

class Ball
{
  float r1;
  float b1;
  float g1;
  float d;
  PVector ballLocation;
  PVector ballVelocity;
  PVector repulsionForce;
  float distanceFromMouse;

  Ball(float x, float y)
  {
    d = random(10, 30);
    r1= random(0, 255);
    b1= random(0, 255);
    g1= random(0, 255);
    ballVelocity = new PVector(random(-4, 4), random(-4, 4));
    ballLocation = new PVector(x, y);
    repulsionForce = PVector.sub(ballLocation, new PVector(mouseX, mouseY));
  }

  void render()
  {
    fill(r1, g1, b1);
    ellipse(ballLocation.x, ballLocation.y, d, d);
  }

  void move()
  {
    bounce();
    curs();
    ballLocation.add(ballVelocity);
  }

  void bounce()
  {
    if (ballLocation.x > width - d/2 || ballLocation.x < 0 + d/2)
    {
      ballVelocity.x = -ballVelocity.x;
      ballLocation.add(ballVelocity);
    }

    if (ballLocation.y > height - d/2 || ballLocation.y < 0 + d/2)
    {
      ballVelocity.y = -ballVelocity.y;
      ballLocation.add(ballVelocity);
    }
  }

  void curs()
  {
    repulsionForce = PVector.sub(ballLocation, new PVector(mouseX, mouseY));
    if (repulsionForce.mag() < 100) {
      repulsionForce.normalize();
      repulsionForce.mult(map(distanceFromMouse, 0, 100, 2, 0));
      ballVelocity.add(repulsionForce);
    }
  }
}

请花些时间了解一下,如果您需要任何解释,请告诉我。祝你好运!