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++;
}
*/
}
}