I have created a new Class, “Polygon” that uses a PVector to store the position on the screen that a shape is displayed. However, whenever I update the position of one the Polygons it updates the position of all the others, effectively making all Polygons centred around the same position. I know this has something to do with PVectors calling on the same object or something but I don’t properly understand it. Is there anyway around this problem or should I just store the x and y position as 2 separate floats?
Processing.org/reference/PVector_set_.html
class Polygon {
final PVector vec = new PVector();
Polygon(final PVector v) {
vec.set(v);
}
}
1 Like
What does ‘final’ do? I’ve done ‘vec = v;’ rather than using ‘vec.set(v)’ because for some reason ‘vec.set(v)’ causes an error for some reason, but that shouldn’t make a difference right? But my issue is that when something like this is run:
PVector position = new PVector(200, 200); Polygon square = new Polygon(position); Polygon pentagon = new Polygon(position); pentagon.vec.x = 400; println(square.vec.x);
This prints 400 because line 4 updates the x position for all Polygons. When I want it to print 200.
void setup() {
final PVector pos = new PVector(200, 200);
final Polygon square = new Polygon(pos);
final Polygon pentagon = new Polygon(pos);
pentagon.vec.x = 400;
println(square.vec.x); // 200.0
exit();
}
class Polygon {
final PVector vec = new PVector();
Polygon(final PVector v) {
vec.set(v);
}
}
1 Like