首页 > 解决方案 > 球在处理中远离光标项目

问题描述

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

标签: processing

解决方案


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

弹跳!

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

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

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

  2. 重组了球类的更新顺序。而不是render -> move -> bounce -> curs我将其设置为move -> render同时确保包含对和按以下顺序的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);
    }
  }
}

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


推荐阅读