I'm making a 2D game that uses 2D transformations to get an objects position in its environment to the relative position it will be drawn at. As if viewed through a moving camera.
In the zoom function I take the position vector (self.pos) and scale it with a value (z). Assigning this value to a different attribute (self.zoom_pos) However the line: self.zoom_pos.x=self.pos.x*z changes the original position vector which I don't want to do.
Any explanations?
def zoom(self,z):
print(self.pos.x)
self.zoom_pos.x=self.pos.x*z
self.zoom_pos.y=self.pos.y*z
print(self.pos.x,z)
INPUT self.pos.x = 100 z = 2
OUPUT self.zoom_pos.x = 200 self.pos.x = 200
DESIRED OUTPUT self.zoom_pos.x = 200 self.pos.x = 200
edit: print statements were just for testing
CodePudding user response:
while it is not clear from your question, it seems like you have self.zoom_pos = self.pos
somewhere in your code, so these two variables are now pointing to the same object, and any change to one will change the other
an easy fix is to change that line to:
import copy # somewhere at the top
self.zoom_pos = copy.copy(self.pos)
this will only make self.zoom_pos a copy of the object in self.pos, and not the same object.
you should also check Facts and myths about Python names and values