Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Attribute scaling anomaly

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading