I’m hoping someone can explain this to me, as I’m pretty new to this stuff. Here’s a program that shows the problem I’m having:
targ_pos = PVector(100, 300, 0)
mis_pos = PVector(400, 600, 0)
l_o_s = PVector()
class Foo:
def init(self, targ_pos, mis_pos):
self.targ_pos = targ_pos
self.mis_pos = mis_pos
self.l_o_s = PVector()
def sub_vec(self, target_pos):
# temp_vec = mis_pos.copy() # uncomment to clear error
# self.l_o_s = temp_vec.sub(self.targ_pos) # uncomment to clear error
self.l_o_s = self.mis_pos.sub(self.targ_pos) # comment to clear error
return self.l_o_s
foo = Foo(targ_pos, mis_pos)
i = 1
def draw():
global i
vec = foo.sub_vec(targ_pos)
print('targ_pos ', targ_pos)
print('mis_pos ', mis_pos)
print('vec ', vec)
if i == 2:
noLoop()
i += 1
The output looks like this:
('targ_pos ', [ 100.0, 300.0, 0.0 ])
('mis_pos ', [ 300.0, 300.0, 0.0 ])
('vec ', [ 300.0, 300.0, 0.0 ])
('targ_pos ', [ 100.0, 300.0, 0.0 ])
('mis_pos ', [ 200.0, 0.0, 0.0 ])
('vec ', [ 200.0, 0.0, 0.0 ])
The PVector ‘mis_pos’ is changing, though there is no instruction that would cause it to do so. If I make a copy of that vector and use the copy for the subtraction, it works as intended.
Any help appreciated, thanks, Mike