PVector p;
PVector newP;
void setup() {
p = new PVector(500, 300);
newP = new PVector(500, 300);
}
void draw() {
newP = p;
println(newP + " < newP before");
// Adds to p
p.add(50, 100);
println(newP + " < newP after");
println(p + " < p");
println();
delay(5000);
}
When I .add to p, the variable newP changes aswell. Am I missing something here? Does the .add() method do something else than I thought it would? Can someone explain this behavior?
Result:
[ 500.0, 300.0, 0.0 ] < newP before
[ 550.0, 400.0, 0.0 ] < newP after
[ 550.0, 400.0, 0.0 ] < p
Yeah, these are objects. p and newP are only pointers to these objects. So, if you do newP = p;, it’s setting both of these pointers to the same object, and, as a result,since any manipulations directed at the pointer happen on the object that it’s pointing at, it’s getting reflected in any pointers of that object.