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

Added Rigid Body Method "get_inverse_inertia_tensor" #39817

Merged
merged 1 commit into from
Jul 21, 2020

Conversation

yrk06
Copy link
Contributor

@yrk06 yrk06 commented Jun 25, 2020

Exposed inertia tensor to GDScript

How

This was done by creating a variable on the rigidbody class. The variable receives the inverse inertia tensor value each time the RigidBody state gets changed, then the get_inverse_inertia_tensor() returns the the inverse_inertia_tensor. The method is exposed to GDScript with bind_method

Consequences

As a consequence of this, rotations with rigid bodies can be controlled more accurately through the use of Physics Formulas of Torque

Requests

This was proposed in: godotengine/godot-proposals#1114 also in #2125 and kind of related to godotengine/godot-proposals#945

@yrk06 yrk06 marked this pull request as ready for review June 25, 2020 01:15
@yrk06 yrk06 requested a review from a team as a code owner June 25, 2020 01:15
@Calinou Calinou added the cherrypick:3.x Considered for cherry-picking into a future 3.x release label Jun 25, 2020
@Calinou Calinou added this to the 4.0 milestone Jun 25, 2020
@Calinou Calinou removed the cherrypick:3.x Considered for cherry-picking into a future 3.x release label Jun 25, 2020
@Calinou Calinou modified the milestones: 4.0, 3.2 Jun 25, 2020
@Calinou
Copy link
Member

Calinou commented Jun 25, 2020

Also, pull requests should be opened against the master branch unless it can't be applied to the master branch for some reason. We can then cherry-pick the change into the 3.2 branch if it doesn't break backwards compatibility.

@yrk06
Copy link
Contributor Author

yrk06 commented Jun 25, 2020

Also, pull requests should be opened against the master branch unless it can't be applied to the master branch for some reason. We can then cherry-pick the change into the 3.2 branch if it doesn't break backwards compatibility.

I tried to make the change using the Master branch, but I couldn't get the engine to run on my local computer. That's why I decided to go with the 3.2 version

@@ -765,6 +766,10 @@ Vector3 RigidBody::get_angular_velocity() const {
return angular_velocity;
}

Basis RigidBody::get_inertia_tensor() {
return inverse_inertia_tensor.inverse();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inverse of the inertia tensor is used for two reasons:

  1. For convenience: To calculate the rotational acceleration, we need to multiply the torque by the inverse of the inertia tensor.
  2. For StaticBodies: The inertia tensor needs to be infinite, which is not possible. However the inverse is zero, which is possible.

Inverting the zeroed inertial tensor of a StaticBody, or a RigidBody in Static mode, would result in division by zero.

In short, don't invert the inverse of the inertia tensor. If you want to provide access to the inertia tensor, return the inverse instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for pointing out. I didn't know that. I knew only about the convenience for calculations, but in order to calculate torque, the not-inversed is needed, thus the function was to get the not-inversed. I'll change that and commit it

@yrk06 yrk06 requested review from Calinou and madmiraal June 25, 2020 20:37
@yrk06 yrk06 changed the title Added Rigid Body Method "get_inertia_tensor" Added Rigid Body Method "get_inverse_inertia_tensor" Jun 25, 2020
Copy link
Contributor

@madmiraal madmiraal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, aside from my nitpick below, the commits just need to be squashed into a single commit.

</return>
<description>
Returns the inverse inertia tensor basis. This is used to calculate the angular acceleration made from a certain torque on the RigidBody.
</description>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking, but I think this would read better if it didn't include the word "certain" i.e. something like:

This is used to calculate the angular acceleration resulting from a torque applied to the RigidBody.

@yrk06 yrk06 force-pushed the ExposeInertiaTensor branch from 6681fef to 952b04a Compare July 2, 2020 16:45
@yrk06 yrk06 force-pushed the ExposeInertiaTensor branch from 952b04a to 0cd43cb Compare July 2, 2020 16:54
@yrk06
Copy link
Contributor Author

yrk06 commented Jul 2, 2020

I force pushed the commit again in order to change the method name in the commit from get_inertia_tensor to get_inverse_inertia_tensor

Copy link
Contributor

@madmiraal madmiraal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm was just looking at that. This should have been created as a property with a getter and a setter.

@yrk06
Copy link
Contributor Author

yrk06 commented Jul 2, 2020

I'm was just looking at that. This should have been created as a property with a getter and a setter.

I didn't do it because the idea was just exposing using the already existent function on class PhysicsDirectBodyState in C++ to RigidBodies in GDscript

I Haven't looked yet on how the engine calculates the Inertia Tensor, but my guess is that it would require a new function inside the Bullet Physics Classes in order to Overwrite it with a Setter

@madmiraal
Copy link
Contributor

Okay, let's leave it as it is then.

One more change, please update the commit message, which still has all the individual commit messages to just the first commit message. Also, commit messages should be written in the present tense; so 'Add Rigid Body Method "get_inverse_inertia_tensor"'.

Finally, please create a second PR against the master branch referencing this one. If you've thoroughly tested it on the 3.2 branch, you should be confident that it will work on the master branch without being able to run a test project.

@akien-mga akien-mga merged commit 7b4b83e into godotengine:3.2 Jul 21, 2020
@akien-mga
Copy link
Member

Thanks!

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

Successfully merging this pull request may close these issues.

4 participants