Changes made by setPos don't seem to be saving

I’m trying to make a program that move “atoms” (high poly sphere models with random starting locations) across the screen randomly but I’m running to some issues with changes to the positions of the atoms made with setPos not persisting between loop iterations. Is this some sort of scope issue I’m not noticing? I’ve tried a couple different things ranging from creating separate tasks and/or functions for setting positions as well as using async tasks. Any help would be greatly appreciated!

Here’s the code I’m using:

# Add the moveAtomsTask to task manager
self.taskMgr.add(self.moveAtomsTask, "MoveAtomsTask")

def moveAtomsTask(self, task):
  # Give the atoms random movement that changes every self.delayTime seconds
  for i in range(0, self.atom_count):
      atom = self.atoms[i]
      # Get old position and get random new position
      old_pos = atom.getPos()
      new_pos = [randrange(-50, 50, 1) / 10, randrange(-50, 50, 1) / 10,
                 randrange(-50, 50, 1) / 10]
      # Set posInterval that changes the location of the atom to a new one over
      # the course of self.delayTime seconds
      posInterval = atom.posInterval(self.delayTime, Point3(old_pos[0], old_pos[1], old_pos[2]),
                                     Point3(new_pos[0], new_pos[1], new_pos[2]))
      atom.setPos(Point3(new_pos[0], new_pos[1], new_pos[2]))
      # Debugging print statement to check if atom position is being changed properly
      if i == 0:
          print(f"Old pos: {old_pos} \t New pos: {new_pos} \t Get pos: {atom.getPos()}")
  self.taskMgr.doMethodLater(self.delayTime, self.moveAtomsTask, "MoveAtomsTask")
  return Task.done

The output given by the print statement running over a couple timesteps is the following:
Note: the “Get pos” should match “Old pos” of the next timestep. FOr example for timesteps 1 and 2 the “Old pos” of timestep 2 should be equal to “Get pos” of timestep 1.

Old pos: LPoint3f(-1.4, -0.1, 4.1) New pos: [-1.1, 2.8, 2.4] Get pos: LPoint3f(-1.1, 2.8, 2.4)

Old pos: LPoint3f(-1.39993, -0.099359, 4.09962) New pos: [-3.3, -3.4, -2.1] Get pos: LPoint3f(-3.3, -3.4, -2.1)

Old pos: LPoint3f(-1.40144, -0.10198, 4.0947) New pos: [4.4, 4.7, 2.8] Get pos: LPoint3f(4.4, 4.7, 2.8)

Old pos: LPoint3f(-1.30336, -0.020793, 4.07281) New pos: [-2.0, -2.0, 3.0] Get pos: LPoint3f(-2, -2, 3)

Old pos: LPoint3f(-1.30351, -0.0212363, 4.07257) New pos: [-1.0, 0.7, 2.6] Get pos: LPoint3f(-1, 0.7, 2.6)

Old pos: LPoint3f(-1.30313, -0.0203211, 4.0707) New pos: [2.7, 3.1, -0.7] Get pos: LPoint3f(2.7, 3.1, -0.7)

First of all, greetings, and welcome to the forum! I hope that you find your time here to be positive! :slight_smile:

As to your question, I think that the problem may be twofold:

First, and most fundamentally, your code–if I’m not much mistaken–creates a “posInterval” that moves the atom from “new_pos” to “old_pos”. As a result, the atoms always end at the location given by “old_pos”–which is where they were in the first place. Which in turn means that they always end in the same place, no matter what.

And second, note that just before you print out your atom-positions, you set the position of each atom to “new_pos”. As a result, the call to “getPos” then matches the value of “new_pos”.


Thanks for the welcome! :smiley:

And OMG thank you so much!
I did not realise I had the start and end positions backwards. I switched them and now its working flawlessly!

I’ve been looking at this for days trying to figure it out :sweat_smile:

1 Like

Such, I’m afraid, is sometimes the way of programming! ^^;

I’m glad to have helped! :slight_smile:

1 Like