Skip to content
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

can't use Rigigbody2D's apply_impulse and apply_torque_impulse when in _ready() function #29888

Open
Tracked by #45334
Raphael2048 opened this issue Jun 19, 2019 · 5 comments

Comments

@Raphael2048
Copy link
Contributor

Raphael2048 commented Jun 19, 2019

Godot version:

d2f38db

OS/device including version:

Issue description:

Create a normal rigidbody2D in scene, attach a script to it, and try to call apply_impulse or apply_torque_impulse in _ready(),it has no effect.
Reason: Rigidbody2d's updating inertia is delayed to next frame, so when you call a function need mass or inertia in _ready(), it has no effect.

void Body2DSW::_update_inertia() {
if (!user_inertia && get_space() && !inertia_update_list.in_list())
get_space()->body_add_to_inertia_update_list(&inertia_update_list);
}

RigidBody has the same problem, the difference is RigidBody's inertia is inited with identity instead of zero.
Steps to reproduce:

Minimal reproduction project:

bug2.zip

@KoBeWi
Copy link
Member

KoBeWi commented Jun 19, 2019

Doesn't call_deferred solve the issue?

@Raphael2048
Copy link
Contributor Author

If it's updated at end of frame, you can't get calculated result at once after you apply impulse.

@KoBeWi
Copy link
Member

KoBeWi commented Jun 19, 2019

Deferring the calculation too would be inconvenient, I guess? I'll tag it as bug, but it might be possible that it can't be fixed and it's more a documentation issue.

@bmwalters
Copy link

I just encountered this too. My Player instances a throwing knife on a keypress and flings it using apply_impulse right after instancing it. I have to delay this impulse for 2 physics frames otherwise the knife flings, but doesn't start spinning at all.

I was clued into this by a 2 year old reddit post which links to a similarly old forum post.

@quinnyo
Copy link
Contributor

quinnyo commented Oct 11, 2022

I did some testing and found the following:

  • The issue isn't strictly about _ready(), but the body's first frame. i.e. you get the same behaviour if you apply_impulse on the first call to _physics_process

  • The body's linear velocity is affected. The torque has no effect.

  • The issue seems to be that the body has zero (inverse) inertia at that point. (it hasn't been auto-calculated yet, I guess)

  • If you set the inertia manually, applying torque on a body's first frame works as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants