Python - Changing velocity of one group instance - Without affecting others

Hi,
I’m working on a destruction system, where any impacted cube splits into 8 smaller cubes.


Basically there’s a cube with a script running, calculating its acceleration from its previous and current velocities. when the ‘acceleration’ variable reaches a certain value, the cube adds a group instance, containing the eight smaller cubes.

To keep the smaller cubes moving in the direction the larger one was going, their initial velocities must be set to match the bigger one’s before it disappears.

But since I’m adding a group instance, I can’t access the eight cubes directly with objectLastCreated (that accesses the group object). Instead, I have to go,

added = add.objectLastCreated
     for i in added.groupMembers:
          obj[str(i)].setLinearVelocity(own.getLinearVelocity())
          obj[str(i)].setAngularVelocity(own.getAngularVelocity())

This affects all cubes that belong to the instanced group object. It works great when there’s only one cube breaking, but when there’s multiple cubes breaking, there’s multiple instanced groups being added. And all of them have the same name.
Thus, all groups are affected - regaurdless of the cube they came from:
cube2

As you can see, the cubes on the right change trajectory mid-course when the left lands. when velocity is set for one group of cubes, it resets all groups of cubes - because it’s the same group being added!

So my question is, how would I set velocity for one group instance, without affecting any others?
Here’s the .blend (made in UPBGE):
break.blend (996.6 KB)

Thanks for reading!

KX_GameObject.groupMembers is either a list of game object instances or None.
With these references, there’s no need to look things up by string, you can call the object directly.
So loop through the list of group members and manipulate them inside that loop.

for member in added.groupMembers:
    member.setLinearVelocity(own.getLinearVelocity())
    member.setAngularVelocity(own.getAngularVelocity())
1 Like

Thank you so much! I thought I’d tried that to begin with, but I tried it again anyway and it works like magic!