-
Discussed in #1545Originally posted by paulocoutinhox December 26, 2023 I have a small problem when i use Physics with animation. I made a small project to show: When i use this code:
The bomb goes to: But when i use this code:
The bomb goes to down, correctly. Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 12 comments
-
Hi, I have updated the bomb-test project to create two sprites, one with animation and other without animation and left then fall with physics/gravity: I also make a video to ppl understand: movie.mov |
Beta Was this translation helpful? Give feedback.
-
As @rh101 has written: @paulocoutinhox It's only a known bug at this moment...may not easy to fix it in short time. |
Beta Was this translation helpful? Give feedback.
-
@paulocoutinhox If you printed out the PhysicsBody variables for each of the two sprites, such as position, rotation etc. etc., you would have noticed that nothing is different between the two objects, except the position. You could have narrowed the issue down further by checking the
The contents of the
For a quick test, one could simply comment out the contents to see what happens, and funny enough, things start working. Uncommenting lines until the issue re-appears would reveal this one line as the culprit: Putting a breakpoint on that call shows the Now, a few things to note: 1 - The frame sizes are all If you comment out the hard-coded line, That is because of this line: So, move this code to
To unload it, you can do so in the destructor, Now, knowing that removing the call Remember the The content scale factor of 1 would mean the
The only thing I can think of is that changing the size of the sprite after the physics body has been created is somehow affecting the transforms, which are used in calculations within the PhysicsBody. So, in summary, what works is any of the following: Delete the line or Set it to the correct scaled size (being or Only set the content size of the sprite if and only if the sprite is not animating, so calling or Use a content scale factor of 1.0 via The most important thing to remember is: The content size of a sprite cannot be changed after the physics object is set on a node. This means you should not call In the end, this entire issue can be reproduced with a few lines that have nothing to do with
|
Beta Was this translation helpful? Give feedback.
-
Added a wiki hint |
Beta Was this translation helpful? Give feedback.
-
Hi, In my code don't have anything changing content size after set physics body. I don't know if you see my code, but i will paste it here:
It create a animated or not sprite, and at last, it add physics body. The line that change something on it and make it work is related to scale as you write above, not content size position in my code, since it is before physics body definition:
Thanks. |
Beta Was this translation helpful? Give feedback.
-
As already mentioned in my previous post,
That is not correct. It is definitely If that is still not completely clear, just re-read the post I made above, and follow the code path yourself to see what actually happens. |
Beta Was this translation helpful? Give feedback.
-
Hi, relax man, i read it, all lines.
What i do to solve is set content size with original frame size and change only the scale:
This is wrong? |
Beta Was this translation helpful? Give feedback.
-
Sorry, I'll clarify. This code is correct: What is not correct is this remark regarding the content size and your usage of it:
The reason is that the line |
Beta Was this translation helpful? Give feedback.
-
Nice. Thanks. |
Beta Was this translation helpful? Give feedback.
-
rsrsrsrs |
Beta Was this translation helpful? Give feedback.
-
What is the reason to put it here? Please close this issue if is "fixed". |
Beta Was this translation helpful? Give feedback.
-
I can't, it was not open by me |
Beta Was this translation helpful? Give feedback.
Sorry, I'll clarify. This code is correct:
bomb->setContentSize(Vec2(78, 64) / AX_CONTENT_SCALE_FACTOR());
What is not correct is this remark regarding the content size and your usage of it:
The reason is that the line
bomb->setContentSize(Vec2(78, 64)
was setting a content size of78,64
, but when it is animating,setSpriteFrame
was callingsetContentSize
with97.5, 80.0
, because of the scale factor. As a result of this, whatever parameters are changed within the node end up affecting the physics …