Problem with Particle and Black hole

#1

Hello guys. I’m writing a code where a particle is attracted by a black hole. The particle should spiral into the black hole but something strange happens. When the xPos of the Particle is = 250 it begins to move downward and the x component of its velocity become 0. Why?

This is the code:

File 1

Particle p1 = new Particle(50, 50, 1, 2);
BlackHole b1 = new BlackHole(250, 250, 1, p1);

void setup()
{
  
  size(500, 500);
  
  
}

void draw()
{
  
  background(255);
  fill(0);//Black hole
  b1.attract();
  fill(255);//White particle
  p1.update();
  println(p1.xPos);
  println(p1.yPos);
  
  
}

Particle Class:

class Particle
{
  
  int xPos, yPos, vX, vY;
  
  Particle(int x, int y, int speedX, int speedY)//X position, Y, speed X, speed Y
  {
    
    xPos = x;
    yPos = y;
    vX = speedX;
    vY = speedY;
    
  }
  
  void update()
  {
    
    ellipse(xPos, yPos, 10, 10);
    
    xPos = xPos + vX;
    yPos = yPos + vY;
    
  }
  
}

BlackHole Class:

class BlackHole
{
  
  int xPos, yPos;
  float power;
  Particle part;
  
  BlackHole(int x, int y, float p, Particle p2)
  {
    
    xPos = x;
    yPos = y;
    power = p;
    part = p2;
    
  }
  
  void attract()
  {
    ellipse(xPos, yPos, 20, 20);
    
    if (xPos < part.xPos && yPos < part.yPos)//If the particle is at the right and at the bottom of the black hole
    {
      
      part.xPos -= power;
      part.yPos -= power;
      
    }
    
    else if (xPos > part.xPos && yPos < part.yPos)//If the particle is at the left and at the bottom of  the black hole
    {
      
      part.xPos += power;
      part.yPos -= power;
    }
    
    
    else if (xPos > part.xPos && yPos > part.yPos)//If the particle is at the left and upon the black hole
    {
      
      part.xPos += power;
      part.yPos += power;
      
    }
    
    else if (xPos < part.xPos && yPos > part.yPos)//If the particle is at the right and upon the black hole
    {
      
      part.xPos -= power;
      part.yPos += power;
      
    }
    /*
    else if(xPos == part.xPos)
    {
      
      part.xPos++;
      
    }
    
    else if (yPos == part.yPos)
    {
      
      part.yPos++;
      
    }
    */
  }
  
}
2 Likes

#2

Ah ha. I think I see the problem.


Okay, so, What happens?

You call b1.attract(); which updates the particle’s position based on where it is in relation to the black hole.

Then

You call p1.update(); which updates the particle based on the particle’s speed.

Right?


So here’s what I think is happening:

The particle finally gets to the right side of the screen.

b1.attract(); is called, and because the particle is on the right side of the screen, the particles’s position is moved left one pixel. (-= power).

Then

You call p1.update();, and the particle’s position is updated based on its speed - it moves right one pixel.

BUT THE NET EFFECT (of moving it left one and then right one) is that IT DOESN’T MOVE.

Worse, It will do the same thing the next time draw() is called… which makes it look like it’s suddenly lost its horizontal movement. (Actually it hasn’t - it’s being moved back and forth the same amount each frame!)


So what can we do about it?

One solution would be to change the VELOCITY of the particle (not the POSITION) when calling b1.attract();. Example:

Particle p1 = new Particle(50, 50, 1, 2);
BlackHole b1 = new BlackHole(250, 250, 1, p1);

void setup()
{
  
  size(500, 500);
  
  
}

void draw()
{
  
  background(255);
  fill(0);//Black hole
  b1.attract();
  fill(255);//White particle
  p1.update();
  
  //println(p1.xPos);
  //println(p1.yPos);
  
  
}

// Particle Class:

class Particle
{
  
  int xPos, yPos, vX, vY;
  
  Particle(int x, int y, int speedX, int speedY)//X position, Y, speed X, speed Y
  {
    
    xPos = x;
    yPos = y;
    vX = speedX;
    vY = speedY;
    
  }
  
  void update()
  {
    
    ellipse(xPos, yPos, 10, 10);
    
    xPos = xPos + vX;
    yPos = yPos + vY;
    
  }
  
}

//BlackHole Class:

class BlackHole
{
  
  int xPos, yPos;
  float power;
  Particle part;
  
  BlackHole(int x, int y, float p, Particle p2)
  {
    
    xPos = x;
    yPos = y;
    power = p;
    part = p2;
    
  }
  
  void attract()
  {
    ellipse(xPos, yPos, 20, 20);
    
    if (xPos < part.xPos && yPos < part.yPos)//If the particle is at the right and at the bottom of the black hole
    {
      
      part.vX -= power;
      part.vY -= power;
      
    }
    
    else if (xPos > part.xPos && yPos < part.yPos)//If the particle is at the left and at the bottom of  the black hole
    {
      
      part.vX += power;
      part.vY -= power;
    }
    
    
    else if (xPos > part.xPos && yPos > part.yPos)//If the particle is at the left and upon the black hole
    {
      
      part.vX += power;
      part.vY += power;
      
    }
    
    else if (xPos < part.xPos && yPos > part.yPos)//If the particle is at the right and upon the black hole
    {
      
      part.vX -= power;
      part.vY += power;
      
    }
    /*
    else if(xPos == part.xPos)
    {
      
      part.xPos++;
      
    }
    
    else if (yPos == part.yPos)
    {
      
      part.yPos++;
      
    }
    */
  }
  
}

And behold! Our particles now orbits the black hole as we would expect. Huzzah!

(All the changes are in the attract() function - part.vX and part.vY are updated now, not part.posX and part.posY!)

3 Likes