# Particles inside a 3D object to be attracted

**URL:** <https://discourse.processing.org/t/particles-inside-a-3d-object-to-be-attracted/25375>\
**Category:** Coding Questions\
**Created:** [November 12, 2020, 5:14pm UTC](https://discourse.processing.org/t/particles-inside-a-3d-object-to-be-attracted/25375 "2020-11-12T17:14:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BlueJack](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@BlueJack](https://discourse.processing.org/u/BlueJack)\
**Post date:** [November 12, 2020, 5:14pm UTC](https://discourse.processing.org/t/particles-inside-a-3d-object-to-be-attracted/25375/1 "2020-11-12T17:14:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 12, 2020, 7:08pm UTC](https://discourse.processing.org/t/particles-inside-a-3d-object-to-be-attracted/25375/2 "2020-11-12T19:08:50Z")

</div>

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

```auto

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);
  location.add(velocity);
  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();
  }
}
//

```
