微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

球从处理中的光标项目移开

如何解决球从处理中的光标项目移开

我正在尝试制作一个程序,该程序使用一个数组来显示 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,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();
  }
}```

解决方法

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

BOUNCE!

您的主要问题是scope。当您有多个同名的变量时,作用域将决定哪一个隐藏哪一个,并且在那里发生了很多混乱。此外,您似乎并不完全了解所有这些坐标发生了什么。您不需要 x y 浮动来跟踪球的位置,因为您已经有一个 PVector ballLocation 可以为您完成这项工作。

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

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

  2. 重新组织了球类的更新顺序。我将它设置为 render -> move -> bounce -> curs 而不是 move -> render,同时确保 move 按以下顺序包含对 bouncecurs 的调用:bounce -> 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,mouseY));
    if (repulsionForce.mag() < 100) {
      repulsionForce.normalize();
      repulsionForce.mult(map(distanceFromMouse,100,2,0));
      ballVelocity.add(repulsionForce);
    }
  }
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。