# Particles inside a 3D object to be attracted

Hello, what is an effective way to deal with particles trapped inside a 3D object (box or cylinder, for exaple) being attracted by outer particle. The result is to get inside particles moving forward to the attractor, then bound the object wall and slide along the wall and get stuck to the nearest point to attractor (typically in the inner corner).
I have been trying to find some examples or clues but failed. On 2D it is simple but I need 3D.

Hey, and welcome to the forum!

Great to have you here!

here is an example of a box, but just reflection, no attractor

Warm regards,

Chrisir

final color RED = color (255, 0, 0); // RED

PVector location;
PVector velocity;
float boxSize = 350;
int ballSize = 14;
color backgroundFill=0;

void setup() {
size(600, 600, P3D);
//frameRate(8);
background(backgroundFill);
location = new PVector(random(3), random(0.4, 3), random(0.4, 3));
velocity = new PVector(random(0.4, 3), random(0.4, 2), random(0.4, 1));
}

void draw() {
background(backgroundFill);
backgroundFill=0;

lights();

// ---------------------------------------------

pushMatrix();
translate(width/2, height/2, 0);
noFill();
stroke(255);
strokeWeight(5);
box(boxSize);
popMatrix();

// ---------------------------------------------

pushMatrix();
translate(width/2, height/2, 0);
translate(location.x, location.y, location.z);
bounce();
noStroke();
fill(255, 0, 0); // RED
sphere(ballSize);
popMatrix();
}

// ---------------------------------------------

void bounce() {

//println(location.x, location.y, location.z);

// X ------------------------------

if ((location.x > boxSize/2 - ballSize || location.x < -boxSize/2 + ballSize)) {
velocity.x = velocity.x * -1;
// backgroundFill=RED;  // RED

pushMatrix();
//translate(0, 0, location.z);
rotateY(PI/2);
fill(RED);
// stroke(255);
noStroke();
ellipse(0, 0, 12, 12 );
ellipse(0, 0, 22, 22 );
ellipse(0, 0, 32, 32 );
ellipse(0, 0, 132, 132 );
popMatrix();
}

// Y ------------------------------

if ((location.y > boxSize/2 -  ballSize|| location.y < -boxSize/2 + ballSize)) {
velocity.y = velocity.y * -1;
//backgroundFill=color(0, 255, 0); // GREEN

pushMatrix();
//translate(0, 0, location.z);
rotateX(PI/2);
fill(0, 255, 0);
// stroke(255);
noStroke();
ellipse(0, 0, 12, 12 );
ellipse(0, 0, 22, 22 );
ellipse(0, 0, 32, 32 );
ellipse(0, 0, 132, 132 );
popMatrix();
}

// Z ------------------------------
if (location.z > boxSize/2 -  ballSize ) {
velocity.z = velocity.z * -1;
//   backgroundFill=color(0, 0, 255); // BLUE
pushMatrix();
//translate(0, 0, location.z);
fill(0, 0, 255);
//stroke(255);
noStroke();
ellipse(0, 0, 12, 12 );
ellipse(0, 0, 22, 22 );
ellipse(0, 0, 32, 32 );
ellipse(0, 0, 132, 132 );
popMatrix();
}
if (location.z < -boxSize/2 + ballSize) {
velocity.z = velocity.z * -1;
//  backgroundFill=color(0, 0, 255); // BLUE
// ellipse(location.x, location.y, location.z );
pushMatrix();
//translate(0, 0, location.z);
fill(0, 0, 255);
//stroke(255);
noStroke();
ellipse(0, 0, 12, 12 );
ellipse(0, 0, 22, 22 );
ellipse(0, 0, 32, 32 );
ellipse(0, 0, 132, 132 );
popMatrix();
}
}
//