Boolean trouble or something

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?

PVector m, pressLoc;
float dragDist;
boolean firstPressCaptured;
void setup() {
  size(400, 600);
  m = new PVector();
  pressLoc = new PVector();
void draw() {
  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);
1 Like

1 Like

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.

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( 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.

  • 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.

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.

NaNNaN NaNNaN NaNNaN NaNNaN… Batmaaaan! :sweat_smile:

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…

This leaves z undefined

PVector m1, zero;

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

void draw() {
   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.

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

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