-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Body.copy() with multiple shapes attached to a body #239
Comments
Oh.. It seems to work properly if the whole space is copied. But not if the shape or body is copied separately.. In the your last case it looks like the issue is that the two shapes copies have different bodies, but only one of them is added to the space which is why it breaks. I will have to investigate a bit |
In the last case the shapes are attached to the same body, so the I was (maybe naively :) ) expecting that the shape copies would reference the same body object. Instead, it seems that the copy is done sequentially and each shape is attached to its own copy of the initial body: print(hex(id(b1)))
shape_copies = []
for shape in b1.shapes:
shape_copies.append(shape.copy())
print(hex(id(shape_copies[-1].body)))
s.add(*shape_copies, shape_copies[0].body)
Thus both body copies should be added to the space, but the shapes will not be part of the same body anymore. A workaround could be to replace the body of the additional shape copies with the one from the first shape copy, as below, but it means that we may be doing a lot of body copying for nothing: shape_copies = []
for shape in b1.shapes:
shape_copies.append(shape.copy())
for shape in shape_copies[1:]:
shape.body = shape_copies[0].body
s.add(*shape_copies, shape_copies[0].body) |
Thinking about this some more, Im not sure if this is a bug or feature :) Copy a space is "easy", or at least the expectation of what should happen is easy (except for maybe callback functions and other special cases). Everything inside should be copied. But for single bodies or shapes Im not sure:
I guess the copy could follow all references between bodies, shapes and constraints so if you copy one thing then everything else is included. However, this is quite complicated since creation of the copies has to happen in the right order or it will break.. Given all this Im not sure I would have an API for it at all except to copy space unless it didnt already exist.. |
From a user perspective, it would make sense to have a "ready to use" copied object, with all referenced objects. But I see that it would be very challenging from the API perspective, so I guess it's not an idea to consider 😄 As an alternative, could it make sense to have an option to only copy the shape (and not the referenced body for instance) so no unnecessary copying happens when working with multiple shapes? Assuming that these body extra copies have an impact on performances. |
The copy of a body attached to multiple shapes raises an
AssertionError: The shape's body must be added to the space before (or at the same time) as the shape.
when following the usage shown in the examples.The only workaround I have found is to get a new body and only copy the shapes (see below).
The text was updated successfully, but these errors were encountered: