PVector and line() question
in
Programming Questions
•
2 years ago
Hello Processing community
I'm hoping someone can help me troubleshoot a problem I have with using PVector and the line() function. I have used line() before to draw continuous lines in the following manner (pseudocode):
xOld = x, yOld = y
update x and y to new locations
draw line(xOld, yOld, x, y)
repeat
Now I'm trying to get my head around OOP and PVector, and the above technique doesn't seem to be working for me. Using the following (actual) code in my update function (where plocation and location are PVector vars),
void update() {
plocation=location; // where plocation should get the position info from the location variable
PVector direction = PVector.sub(attractor,location);
direction.normalize();
direction.mult(0.1);
accelaration = direction;
accelaration.mult(0.75);
velocity.add(accelaration);
velocity.mult(0.95);
velocity.limit(topSpeed);
location.add(velocity); // now location is updated, so plocation and location should be different
}
This always gives me the same value for plocation and location, even though location is changed in the last line, after the value has been passed.
Is this to be expected and just the way PVector works, or is it to be expected from working with classes and functions? Or am I doing something else wrong entirely?
Any answers or hints greatly appreciated. I've tried to be as succinct as possible with the provided code so as to not clutter the problem, but of course if I failed to post any relevant information, please feel free to ask for it!
Joris
I'm hoping someone can help me troubleshoot a problem I have with using PVector and the line() function. I have used line() before to draw continuous lines in the following manner (pseudocode):
xOld = x, yOld = y
update x and y to new locations
draw line(xOld, yOld, x, y)
repeat
Now I'm trying to get my head around OOP and PVector, and the above technique doesn't seem to be working for me. Using the following (actual) code in my update function (where plocation and location are PVector vars),
void update() {
plocation=location; // where plocation should get the position info from the location variable
PVector direction = PVector.sub(attractor,location);
direction.normalize();
direction.mult(0.1);
accelaration = direction;
accelaration.mult(0.75);
velocity.add(accelaration);
velocity.mult(0.95);
velocity.limit(topSpeed);
location.add(velocity); // now location is updated, so plocation and location should be different
}
This always gives me the same value for plocation and location, even though location is changed in the last line, after the value has been passed.
Is this to be expected and just the way PVector works, or is it to be expected from working with classes and functions? Or am I doing something else wrong entirely?
Any answers or hints greatly appreciated. I've tried to be as succinct as possible with the provided code so as to not clutter the problem, but of course if I failed to post any relevant information, please feel free to ask for it!
Joris
1
