Jondolf / avian

ECS-driven 2D and 3D physics engine for the Bevy game engine.
https://crates.io/crates/avian3d
Apache License 2.0
1.55k stars 120 forks source link

Why does my Transform::translation immediately get NaN values? #366

Open tliron opened 5 months ago

tliron commented 5 months ago

I've spent hours tried to debug this, but am stuck and need some help. Using bevy_xpbd_2d.

I tried to naïvely (following the documentation) add a RigidBody to an existing SpriteBundle entity. But then the Transform::translation x y values immediately go to NaN and the sprite of course disappears. Actually rotation also becomes Quat(0.0, 0.0, NaN, NaN), I'm confident those are related.

I've tried so many things! Different types of RigidBody (including Static) have the same effect. Removing all gravity, including inserting a GravityScale of 0, same effect. LockedAxes::ALL_LOCKED, same effect. Explicitly adding a Position at 0, same effect. Adding colliders and MassPropertiesBundle, same effect.

I just don't know what could possibly be causing this. Is it applying some velocity or force from something else? Is the sync between Position and Translation going wrong? There are no debug messages at all from bevy_xpbd_2d.

tliron commented 5 months ago

I made some progress on this. Through trial and error I discovered that the NaN problem pops up when I change the Transform::scale. I'm not really sure why it would have this effect. Does the Transform::scale affect collider sizes or other associated components of RigidBody?

tliron commented 5 months ago

Another discovery. The scale cannot have 0 in its z axis. This is surprising because we are working in 2D. But apparently something in the calculation still takes the z axis into account.

I would consider this a bug. At least it's something that should be documented.