# Drawing the path of Mover under the influence of Attractor

**URL:** <https://discourse.processing.org/t/drawing-the-path-of-mover-under-the-influence-of-attractor/5463>\
**Category:** Coding Questions\
**Created:** [November 12, 2018, 12:16am UTC](https://discourse.processing.org/t/drawing-the-path-of-mover-under-the-influence-of-attractor/5463 "2018-11-12T00:16:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahariri72](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@ahariri72](https://discourse.processing.org/u/ahariri72)\
**Post date:** [November 12, 2018, 12:16am UTC](https://discourse.processing.org/t/drawing-the-path-of-mover-under-the-influence-of-attractor/5463/1 "2018-11-12T00:16:53Z")

</div>

Hi all,

I am trying to track the movement of a mover class that is moving based on the gravity of a couple of attractors. I need to show the trajectory of this movement using lines/points. Currently the ellipse movement location is determined using velocity/acceleration PVectors and ellipse redraws as it moves thru locations.

Any help is much appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 12, 2018, 1:25am UTC](https://discourse.processing.org/t/drawing-the-path-of-mover-under-the-influence-of-attractor/5463/2 "2018-11-12T01:25:55Z")

</div>

```auto
class Mover {
  // ...
  ArrayList<PVector> prev_ps = new ArrayList();
  // ...
  void draw(){ // Or whenever it moves...
    // ...
    while( prev_ps.size) > 100) prev_ps.remove(0);
    prev_ps.add( new PVector( x, y, 0 ) );
    // ...
    for( int i = 0; i < prev_ps.size()-1; i++) line( prev_ps[i].x, prev_ps[i].y, prev_ps[i+1].x, prev_ps[i+1].y );
  }
  // ...
}

```

Untested code - Example only - YMMV

---

<div class="post-metadata">

**Author:** ![ahariri72](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@ahariri72](https://discourse.processing.org/u/ahariri72)\
**Post date:** [November 12, 2018, 3:26pm UTC](https://discourse.processing.org/t/drawing-the-path-of-mover-under-the-influence-of-attractor/5463/3 "2018-11-12T15:26:54Z")

</div>

> [@TfGuy44](#):
>
> for( int i = 0; i \< prev\_ps.size()-1; i++) line( prev\_ps[i].x, prev\_ps[i].y, prev\_ps[i+1].x, prev\_ps[i+1].y );

thanks! can you please help me with where to add this code in my mover per below?

class Mover {  
PVector location;  
PVector velocity;  
PVector acceleration;  
float mass;

Mover() {  
mass = 1;  
location = new PVector(30,30);  
velocity = new PVector(0,0);  
acceleration = new PVector(0,0);  
}

void applyForce(PVector force1) {  
PVector f1 = PVector.div(force1,mass);  
acceleration.add(f1);  
}  
void applyFroce(PVector force2) {  
PVector f2 = PVector.div(force2,mass);  
acceleration.add(f2);  
}  
void update() {  
velocity.add(acceleration);  
location.add(velocity);  
acceleration.mult(0);  
}  
void display() {  
stroke(0);  
fill(175);  
ellipse(location.x,location.y,mass_10,mass_10);  
}  
void checkEdges() {  
if (location.x \> width) {  
location.x = width;  
velocity.x \*= -1;  
} else if (location.x \< 0) {  
velocity.x \*= -1;  
location.x = 0;  
}

```
 if (location.y > height) {
   velocity.y *= -1;
   location.y = height;
 }

```

}  
}

Mover m;  
Attractor1 a1;  
Attractor2 a2;

void setup() {  
size(640,360);  
m = new Mover();  
a1 = new Attractor1();  
a2 = new Attractor2();  
}

void draw() {  
background(255);

PVector force1 = a1.attract(m);  
PVector force2 = a2.attract(m);  
m.applyForce(force1);  
m.applyForce(force2);  
m.update();

a1.display();  
a2.display();  
m.display();  
}

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 12, 2018, 4:56pm UTC](https://discourse.processing.org/t/drawing-the-path-of-mover-under-the-influence-of-attractor/5463/4 "2018-11-12T16:56:40Z")

</div>

You could also do it with a secondary PGraphics where you draw the line on. Might be more direct.
