Changing the attribute of one object changes the same attribute in other instances? /u/nobody384 Python Education

I am making a 2d free body gravity simulator. In this, I have a loop that updates the acceleration due to gravity, and then updates velocity and position based on that acceleration. Self in the loop is a singleton Simulator object that contains a list of masses, x is a Mass object with attributes self.AG, self.deltaV, and self.vi, among others, all as lists representing motion vectors. I have so far only been testing these with 2 masses in self.masses.

 for x in self.masses: x.updateAG() for x in self.masses: x.updatePos() 

Now, as far as I can tell, updateAG() works properly, the problem comes with updatePos(), defined as follows, where main in the singleton that contains it.

def updatePos(self): self.deltaV[0] = self.AG[0] * self.main.deltaT self.deltaV[1] = self.AG[1] * self.main.deltaT # xf = xi + vi + 1/2at^2 self.x += self.vi[0] * self.main.deltaT + 0.5 * self.deltaV[0] * self.main.deltaT self.y += self.vi[1] * self.main.deltaT + 0.5 * self.deltaV[1] * self.main.deltaT # vf = vi + at self.vi[0] += self.deltaV[0] self.vi[1] += self.deltaV[1] 

Everything works fine until we get to

 self.vi[0] += self.deltaV[0] self.vi[1] += self.deltaV[1] 

where on the first iteration of the for loop, it updates the self.vi of the other instance to have the same value, and on the second iteration, it adds the correct deltaV to the wrong vi. This results in both masses traveling in the same direction at the same speed. It has me stumped and unable to find similar issues. If you want to fuck around with it yourself, source code is on GitHub. To add a mass, there is an add button in the GUI. To get meaningful speeds, you need to add a large mass somewhere around 1e15 to 1e18 and a smaller one probably.

Thanks to anyone that helps.

submitted by /u/nobody384
[link] [comments]

​r/learnpython I am making a 2d free body gravity simulator. In this, I have a loop that updates the acceleration due to gravity, and then updates velocity and position based on that acceleration. Self in the loop is a singleton Simulator object that contains a list of masses, x is a Mass object with attributes self.AG, self.deltaV, and self.vi, among others, all as lists representing motion vectors. I have so far only been testing these with 2 masses in self.masses. for x in self.masses: x.updateAG() for x in self.masses: x.updatePos() Now, as far as I can tell, updateAG() works properly, the problem comes with updatePos(), defined as follows, where main in the singleton that contains it. def updatePos(self): self.deltaV[0] = self.AG[0] * self.main.deltaT self.deltaV[1] = self.AG[1] * self.main.deltaT # xf = xi + vi + 1/2at^2 self.x += self.vi[0] * self.main.deltaT + 0.5 * self.deltaV[0] * self.main.deltaT self.y += self.vi[1] * self.main.deltaT + 0.5 * self.deltaV[1] * self.main.deltaT # vf = vi + at self.vi[0] += self.deltaV[0] self.vi[1] += self.deltaV[1] Everything works fine until we get to self.vi[0] += self.deltaV[0] self.vi[1] += self.deltaV[1] where on the first iteration of the for loop, it updates the self.vi of the other instance to have the same value, and on the second iteration, it adds the correct deltaV to the wrong vi. This results in both masses traveling in the same direction at the same speed. It has me stumped and unable to find similar issues. If you want to fuck around with it yourself, source code is on GitHub. To add a mass, there is an add button in the GUI. To get meaningful speeds, you need to add a large mass somewhere around 1e15 to 1e18 and a smaller one probably. Thanks to anyone that helps. submitted by /u/nobody384 [link] [comments] 

I am making a 2d free body gravity simulator. In this, I have a loop that updates the acceleration due to gravity, and then updates velocity and position based on that acceleration. Self in the loop is a singleton Simulator object that contains a list of masses, x is a Mass object with attributes self.AG, self.deltaV, and self.vi, among others, all as lists representing motion vectors. I have so far only been testing these with 2 masses in self.masses.

 for x in self.masses: x.updateAG() for x in self.masses: x.updatePos() 

Now, as far as I can tell, updateAG() works properly, the problem comes with updatePos(), defined as follows, where main in the singleton that contains it.

def updatePos(self): self.deltaV[0] = self.AG[0] * self.main.deltaT self.deltaV[1] = self.AG[1] * self.main.deltaT # xf = xi + vi + 1/2at^2 self.x += self.vi[0] * self.main.deltaT + 0.5 * self.deltaV[0] * self.main.deltaT self.y += self.vi[1] * self.main.deltaT + 0.5 * self.deltaV[1] * self.main.deltaT # vf = vi + at self.vi[0] += self.deltaV[0] self.vi[1] += self.deltaV[1] 

Everything works fine until we get to

 self.vi[0] += self.deltaV[0] self.vi[1] += self.deltaV[1] 

where on the first iteration of the for loop, it updates the self.vi of the other instance to have the same value, and on the second iteration, it adds the correct deltaV to the wrong vi. This results in both masses traveling in the same direction at the same speed. It has me stumped and unable to find similar issues. If you want to fuck around with it yourself, source code is on GitHub. To add a mass, there is an add button in the GUI. To get meaningful speeds, you need to add a large mass somewhere around 1e15 to 1e18 and a smaller one probably.

Thanks to anyone that helps.

submitted by /u/nobody384
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *