# Boolean trouble or something

**URL:** <https://discourse.processing.org/t/boolean-trouble-or-something/16217>\
**Category:** Beginners\
**Created:** [December 7, 2019, 2:13pm UTC](https://discourse.processing.org/t/boolean-trouble-or-something/16217 "2019-12-07T14:13:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [December 7, 2019, 2:13pm UTC](https://discourse.processing.org/t/boolean-trouble-or-something/16217/1 "2019-12-07T14:13:17Z")

</div>

I can’t see why the value of pressLoc is constantly updated to the mouse PVector while the mouse is pressed and dragged around.  
Can someone please help me?

```auto
PVector m, pressLoc;
float dragDist;
boolean firstPressCaptured;
void setup() {
  size(400, 600);
  m = new PVector();
  pressLoc = new PVector();
}
void draw() {
  background(127);
  m.x = mouseX;
  m.y = mouseY;

  if (mousePressed && !firstPressCaptured) {
    pressLoc = m;
    firstPressCaptured = true;
  }

  if (!mousePressed) {
    firstPressCaptured = false;
  }

  String pressLocStr = "[" + nf(round(pressLoc.x), 3) + ", " + nf(round(pressLoc.y), 3) + "]";
  String mousePressStr = "mousePressed: " + mousePressed;
  String firstPressCapturedStr = "firstPressCaptured: " + firstPressCaptured + " at " + pressLocStr;
  textAlign(LEFT, TOP);
  text(mousePressStr, 5, 5);
  text(firstPressCapturedStr, 5, 25);
}

```

---

<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:** [December 7, 2019, 2:16pm UTC](https://discourse.processing.org/t/boolean-trouble-or-something/16217/2 "2019-12-07T14:16:57Z")

</div>

> [@ddownn](#):
>
> `pressLoc = m;`

> **[set() / Reference](https://processing.org/reference/PVector_set_.html)**
>
> Sets the x, y, and z component of the vector using two or three separate variables, the data from a PVector, or the values from a float array.

`pressLoc.set(m);`

---

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [December 7, 2019, 2:20pm UTC](https://discourse.processing.org/t/boolean-trouble-or-something/16217/3 "2019-12-07T14:20:57Z")

</div>

That works, thanks.

Does `pressLoc = m` create some kind of persistent “link”? I don’t understand how pressLoc value is being changed when those conditions aren’t true anymore.

---

<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:** [December 7, 2019, 2:34pm UTC](https://discourse.processing.org/t/boolean-trouble-or-something/16217/4 "2019-12-07T14:34:58Z")

</div>

pressLoc.set(m) takes the values of m and applies them to pressLoc

If pressLoc is (5, 7) and m is (10, 15), then afterwards pressLoc is (10, 15)

On the other Hand :

pressLoc = m sets pressLoc to be m (not equal to, but actually be!)

Meaning, if pressLoc was (5,7) and m is (10, 15), then afterwards pressLoc is m.  
So if you change m to (100, 90), then pressLoc will be (100,90).

Or more obvious :

pressLoc = new NamedPVector(“PressLoc”,5,7)  
m = new NamedPVector(“M”, 10, 15);

pressLoc.set(m) //does the same as pressLoc.x = m.x, pressLoc.y = m.y

pressLoc = m; // println(pressLoc.name) prints („M“)

Or again in other words, pressLoc = m sets the PVector object pressLoc to be the PVector object m. So any changes to m will obviously be also shown if you access m through pressLoc.

---

<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:** [December 7, 2019, 2:38pm UTC](https://discourse.processing.org/t/boolean-trouble-or-something/16217/5 "2019-12-07T14:38:44Z")

</div>

> [@ddownn](#):
>
> Does `pressLoc = m` create some kind of persistent “link”?

- Variables/fields/parameters store a single value.
- They range from 1 byte to 8 bytes in most languages.
- At the statement `m = new PVector();` the operator `new` creates a contiguous memory block enough to fit in all PVector’s non-`static` fields.
- And when it’s done, it returns the 1st memory address of that block.
- And finally, that 1st memory address is stored in the field _m_ as a reference value.
- Now, when you do `pressLoc = m`, you’re assigning field _pressLoc_ w/ the exact value of _m_.
- After that assignment, both _m_ & _pressLoc_ fields point to the same PVector object, b/c they now store the same 1st memory address value of it.
- In other words, they’re now alias to the same object.

---

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [December 7, 2019, 3:17pm UTC](https://discourse.processing.org/t/boolean-trouble-or-something/16217/6 "2019-12-07T15:17:16Z")

</div>

I really like `PVector.set()` But unfortunately I have developed an aversion to it because in the iCompiler, due to some bug, using it sometimes results in the z value remaining “undefined.” So I find myself humming NaNNaNNaN a lot.

---

<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:** [December 7, 2019, 4:00pm UTC](https://discourse.processing.org/t/boolean-trouble-or-something/16217/7 "2019-12-07T16:00:45Z")

</div>

NaNNaN NaNNaN NaNNaN NaNNaN… Batmaaaan! 😅

Never happend to me in the iCompiler, but i still tend to avoid .set, because it doesn‘t really work well all the time for me… just like .add, .sub and mult and div… and they also don‘t allow followups like .add().mult()… which should work in the IDE, if i‘m not mistaken…

---

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [December 7, 2019, 8:08pm UTC](https://discourse.processing.org/t/boolean-trouble-or-something/16217/8 "2019-12-07T20:08:20Z")

</div>

This leaves z undefined

```auto
PVector m1, zero;

void setup() {
   size(400, 600);
   m1 = new PVector();
   zero = new PVector();
}

void draw() {
   background(127);
   m1.set(mouseX, mouseY);
   
   m1Dist = PVector.dist(zero, m1);
   
   println("m1:", m1, m1Dist);
}

```

`m1.set(mouseX, mouseY, 0);`works _sometimes_, but not reliably enough. It’s annoying.

---

<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:** [December 7, 2019, 9:35pm UTC](https://discourse.processing.org/t/boolean-trouble-or-something/16217/9 "2019-12-07T21:35:21Z")

</div>

Oh… indeed 😅  
Never noticed, cause i just use dist(p.x, p.y, p2.x, p2.y). So that doesn‘t happen like this 😅

---

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [December 8, 2019, 12:38am UTC](https://discourse.processing.org/t/boolean-trouble-or-something/16217/10 "2019-12-08T00:38:06Z")

</div>

Thank you for the detailed explanation. I’m not likely to make this mistake again not that I understand what’s going on.
