# Using a PVector

**URL:** https://discourse.processing.org/t/using-a-pvector/10866
**Category:** Coding Questions
**Created:** [May 3, 2019, 8:30am UTC](https://discourse.processing.org/t/using-a-pvector/10866 "2019-05-03T08:30:54Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Process321](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@Process321](https://discourse.processing.org/u/Process321)
#### Post date: [May 3, 2019, 8:30am UTC](https://discourse.processing.org/t/using-a-pvector/10866/1 "2019-05-03T08:30:54Z")

</div>

Looking to adapt the code below

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [May 3, 2019, 9:19am UTC](https://discourse.processing.org/t/using-a-pvector/10866/2 "2019-05-03T09:19:41Z")

</div>

and where you got this code from?

---

<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: [May 3, 2019, 9:25am UTC](https://discourse.processing.org/t/using-a-pvector/10866/3 "2019-05-03T09:25:29Z")

</div>

[https://processing.org/examples/bouncybubbles.html](https://processing.org/examples/bouncybubbles.html)

I think

---

<div class="post-metadata">

### Author: ![Process321](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@Process321](https://discourse.processing.org/u/Process321)
#### Post date: [May 3, 2019, 9:27am UTC](https://discourse.processing.org/t/using-a-pvector/10866/4 "2019-05-03T09:27:20Z")

</div>

Yeh it’s from the processing examples

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [May 3, 2019, 9:41am UTC](https://discourse.processing.org/t/using-a-pvector/10866/5 "2019-05-03T09:41:14Z")

</div>

you mean, you can copy and use it,  
without credit from where you take it?

even you see that processing example site say:

> **Bouncy Bubbles** based on code from Keith Peters.

and you not need to mention where you got it from?

* * *

```auto

/*
int numBalls = 10;
float spring = 0.05;
float gravity = 0.06;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];

void setup() {
  size(640, 360);
  for (int i = 0; i < numBalls; i++) {
    balls[i] = new Ball(random(width), random(height), random(30, 32), i, balls);
  }
  stroke(0, 0, 255);
  fill(135, 206, 250);
}

void draw() {
  background(255);
  for (Ball ball : balls) {
    ball.collide();
    ball.move();
    ball.display();  
  }
}

class Ball {
  
  float x, y;
  float diameter;
  float vx = 0;
  float vy = 0;
  int id;
  Ball[] others;
 
  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
    others = oin;
  } 
  
  void collide() {
    for (int i = id + 1; i < numBalls; i++) {
      float dx = others[i].x - x;
      float dy = others[i].y - y;
      float distance = sqrt(dx*dx + dy*dy);
      float minDist = others[i].diameter/2 + diameter/2;
      if (distance < minDist) { 
        float angle = atan2(dy, dx);
        float targetX = x + cos(angle) * minDist;
        float targetY = y + sin(angle) * minDist;
        float ax = (targetX - others[i].x) * spring;
        float ay = (targetY - others[i].y) * spring;
        vx -= ax;
        vy -= ay;
        others[i].vx += ax;
        others[i].vy += ay;
      }
    }   
  }
  
  void move() {
    vy += gravity;
    x += vx;
    y += vy;
    if (x + diameter/2 > width) {
      x = width - diameter/2;
      vx *= friction; 
    }
    else if (x - diameter/2 < 0) {
      x = diameter/2;
      vx *= friction;
    }
    if (y + diameter/2 > height) {
      y = height - diameter/2;
      vy *= friction; 
    } 
    else if (y - diameter/2 < 0) {
      y = diameter/2;
      vy *= friction;
    }
  }
  
  void display() {
    ellipse(x, y, diameter, diameter);
  }
}
*/

// Bouncy Bubbles based on code from Keith Peters. 
// https://processing.org/examples/bouncybubbles.html
// try a PVector version

int numBalls = 10;
float spring = 0.05;
float gravity = 0.06;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];
PVector ballpos = new PVector(0, 0);

void setup() {
  size(640, 360);
  for (int i = 0; i < numBalls; i++) {
    float bx = random(width);
    float by = random(height);
    ballpos.set( bx, by );
    //println("i "+i+" x "+(int)bx+" y "+(int)by);
    balls[i] = new Ball(ballpos, random(30, 32), i, balls);
  }
  stroke(0, 0, 255);
  fill(135, 206, 250);
}

void draw() {
  background(255);
  for (Ball ball : balls) {
    ball.collide();
    ball.move();
    ball.display();
  }
}

class Ball {
  //float x, y;
  PVector pos = new PVector(0 , 0);
  float diam;
// float vx = 0;
// float vy = 0;
  PVector v = new PVector(0 , 0);
  int id;
  Ball[] others;

  Ball(PVector posin, float diamin, int idin, Ball[] othersin) {
    pos = posin.copy(); // important, not assign, use copy
    diam = diamin;
    id = idin;
    others = othersin;
  } 

  void collide() {
    for (int i = id + 1; i < numBalls; i++) {
      float dx = others[i].pos.x - pos.x;
      float dy = others[i].pos.y - pos.y;
      float distance = sqrt(sq(dx) + sq(dy));
      float minDist = others[i].diam/2 + diam/2;
      if (distance <= minDist) { 
        float angle = atan2(dy, dx);
        float targetX = pos.x + cos(angle) * minDist;
        float targetY = pos.y + sin(angle) * minDist;
        float ax = (targetX - others[i].pos.x) * spring;
        float ay = (targetY - others[i].pos.y) * spring;
        v.x -= ax;
        v.y -= ay;
        others[i].v.x += ax;
        others[i].v.y += ay;
      }
    }
  }

  void move() {
    v.y += gravity;
    pos.x += v.x;
    pos.y += v.y;
    if (pos.x + diam/2 > width) {
      pos.x = width - diam/2;
      v.x *= friction;
    } else if (pos.x - diam/2 < 0) {
      pos.x = diam/2;
      v.x *= friction;
    }
    if (pos.y + diam/2 > height) {
      pos.y = height - diam/2;
      v.y *= friction;
    } else if (pos.y - diam/2 < 0) {
      pos.y = diam/2;
      v.y *= friction;
    }
  }

  void display() {
    ellipse(pos.x, pos.y, diam, diam);
  }
}

```

---

<div class="post-metadata">

### Author: ![Process321](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@Process321](https://discourse.processing.org/u/Process321)
#### Post date: [May 3, 2019, 9:51am UTC](https://discourse.processing.org/t/using-a-pvector/10866/6 "2019-05-03T09:51:02Z")

</div>

Thank you so much for the help 🙂

All codes I use and adapt will be referenced accordingly - I’m only playing about with various codes at the moment, trying to learn how processing works.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [May 3, 2019, 9:53am UTC](https://discourse.processing.org/t/using-a-pvector/10866/7 "2019-05-03T09:53:18Z")

</div>

good, and learn coding means also learn documenting it  
and that only works if you do it right at the beginning,  
as 5 min later you forget from what webpage you take something…

and not forget the legal issues

---

<div class="post-metadata">

### Author: ![Process321](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@Process321](https://discourse.processing.org/u/Process321)
#### Post date: [May 3, 2019, 10:05am UTC](https://discourse.processing.org/t/using-a-pvector/10866/8 "2019-05-03T10:05:51Z")

</div>

I’ll be sure to do so!

Is there anything in the adapted code you made that would only work in Processing 3, as I am currently using Processing 2.2.1, and it doesn’t seem to want to run the code (gives me a grey display)?

I appreciate your time and the help, thank you!

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [May 3, 2019, 10:22am UTC](https://discourse.processing.org/t/using-a-pvector/10866/9 "2019-05-03T10:22:20Z")

</div>

i test under:

> win 7 / 64 bit /  
> processing 3.5.3

 ![SNAG-0102](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8ab2ac8040a9056acd8d0c93d2c9fd295222d000.jpeg)

besides PVector no other “tricks” used  
ok,  
your error description is very poor, the processing 2.2.1 not show a white screen,  
it give a error about a unknown command

> PVector.copy();

change to

```auto
    pos = new PVector(posin.x,posin.y); //posin.copy(); // important, not assign, use copy

```

and it runs here.

 ![SNAG-0103](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/830664a0eb68d52fbbaac62a15b287828c4d9c5c.jpeg)

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 3, 2019, 11:16am UTC](https://discourse.processing.org/t/using-a-pvector/10866/10 "2019-05-03T11:16:57Z")

</div>

Method PVector::**copy()** is merely a silly alias of the original method PVector::**get()**: 🤡  
[ProcessingJS.org/reference/PVector\_get\_/](http://ProcessingJS.org/reference/PVector_get_/)

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [May 3, 2019, 11:27am UTC](https://discourse.processing.org/t/using-a-pvector/10866/11 "2019-05-03T11:27:18Z")

</div>

yes, there might be many ways, but NOT

```auto
pos = posin;

```

try what happens!

---

<div class="post-metadata">

### Author: ![Process321](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@Process321](https://discourse.processing.org/u/Process321)
#### Post date: [May 3, 2019, 12:30pm UTC](https://discourse.processing.org/t/using-a-pvector/10866/12 "2019-05-03T12:30:20Z")

</div>

I have incorporated this into my updated code, however there seems to still be a slight issue. My code runs, however the balls do not seem to be moving. Any help with fixing my issue would be amazing, thank you!

I have attached my code below (apologies for the length).

References of code used and adapted:  
[https://processing.org/examples/bouncybubbles.html](https://processing.org/examples/bouncybubbles.html) - by Keith Peters  
[https://processing.org/examples/reflection1.html](https://processing.org/examples/reflection1.html) - by Ira Greenberg

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [May 3, 2019, 3:25pm UTC](https://discourse.processing.org/t/using-a-pvector/10866/13 "2019-05-03T15:25:20Z")

</div>

you say it runs?  
that code does not run, and after get it running still pictures missing,  
so you are on your own.

---

<div class="post-metadata">

### Author: ![Process321](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@Process321](https://discourse.processing.org/u/Process321)
#### Post date: [May 3, 2019, 3:27pm UTC](https://discourse.processing.org/t/using-a-pvector/10866/14 "2019-05-03T15:27:19Z")

</div>

By runs I mean it produces a display, showing a dam and a background - the issue is that the balls do not appear or move

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [May 3, 2019, 4:28pm UTC](https://discourse.processing.org/t/using-a-pvector/10866/15 "2019-05-03T16:28:22Z")

</div>

> [@Process321](#):
>
> it produces a display, showing a dam and a background

When I cut and paste your code above into a new PDE window, it says:

> The variable “spring” does not exist

…?

---

<div class="post-metadata">

### Author: ![Process321](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@Process321](https://discourse.processing.org/u/Process321)
#### Post date: [May 3, 2019, 4:32pm UTC](https://discourse.processing.org/t/using-a-pvector/10866/16 "2019-05-03T16:32:18Z")

</div>

Hmm sorry about that - there should be ‘float spring = 0.05;’ under the ‘class Balls {’ section

I should also note I am using Version 2.2.1

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [May 3, 2019, 4:34pm UTC](https://discourse.processing.org/t/using-a-pvector/10866/17 "2019-05-03T16:34:19Z")

</div>

Go ahead and edit your code post above – fix it, and add the comment. Please don’t post code as “running” that you haven’t press the “Run” button and confirmed that it actually runs!

The fact that you are using 2.2.1 is very important for people trying to help you – any particular reason why you are using that and not 3.x?

---

<div class="post-metadata">

### Author: ![Process321](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@Process321](https://discourse.processing.org/u/Process321)
#### Post date: [May 3, 2019, 4:40pm UTC](https://discourse.processing.org/t/using-a-pvector/10866/18 "2019-05-03T16:40:24Z")

</div>

I think you have just found the problem! Thanks!

My computer did run it, but never gave me the ’ ‘spring’ does not exist’ comment

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [May 3, 2019, 4:47pm UTC](https://discourse.processing.org/t/using-a-pvector/10866/19 "2019-05-03T16:47:45Z")

</div>

Perhaps your sketch had multiple tabs, and spring was defined in another tab. It isn’t possible for the code you posted to run alone, without that variable being defined – it could never compile.
