RigidBody2D: add and bind get_inertia() method.

You can't set this value very well, since it's automatically computed
from the mass and the collision shapes. But since the values are higher
than many people might suspect, so being able to read it helps estimate
the amount of torque you might need to apply.
This commit is contained in:
Josh Grams 2016-04-20 20:49:37 -04:00
parent dbabe4c07c
commit f7d31cec38
6 changed files with 30 additions and 4 deletions

View File

@ -24867,13 +24867,15 @@ This method controls whether the position between two cached points is interpola
</constant>
<constant name="BODY_PARAM_MASS" value="2">
</constant>
<constant name="BODY_PARAM_GRAVITY_SCALE" value="3">
<constant name="BODY_PARAM_INERTIA" value="3">
</constant>
<constant name="BODY_PARAM_LINEAR_DAMP" value="4">
<constant name="BODY_PARAM_GRAVITY_SCALE" value="4">
</constant>
<constant name="BODY_PARAM_ANGULAR_DAMP" value="5">
<constant name="BODY_PARAM_LINEAR_DAMP" value="5">
</constant>
<constant name="BODY_PARAM_MAX" value="6">
<constant name="BODY_PARAM_ANGULAR_DAMP" value="6">
</constant>
<constant name="BODY_PARAM_MAX" value="7">
</constant>
<constant name="BODY_STATE_TRANSFORM" value="0">
</constant>
@ -30035,6 +30037,13 @@ This method controls whether the position between two cached points is interpola
Return the body mass.
</description>
</method>
<method name="get_inertia" qualifiers="const">
<return type="float">
</return>
<description>
Return the body's moment of inertia. This is automatically computed from the mass and the shapes, so you can't set it. But at least you can find out what it's current value is...
</description>
</method>
<method name="set_weight">
<argument index="0" name="weight" type="float">
</argument>
@ -30238,12 +30247,14 @@ This method controls whether the position between two cached points is interpola
<argument index="0" name="torque" type="float">
</argument>
<description>
Set a constant torque which will be applied to this body.
</description>
</method>
<method name="get_applied_torque" qualifiers="const">
<return type="float">
</return>
<description>
Return the torque which is being applied to this body.
</description>
</method>
<method name="set_sleeping">

View File

@ -602,6 +602,11 @@ real_t RigidBody2D::get_mass() const{
return mass;
}
real_t RigidBody2D::get_inertia() const{
return Physics2DServer::get_singleton()->body_get_param(get_rid(),Physics2DServer::BODY_PARAM_INERTIA);
}
void RigidBody2D::set_weight(real_t p_weight){
set_mass(p_weight/9.8);
@ -868,6 +873,8 @@ void RigidBody2D::_bind_methods() {
ObjectTypeDB::bind_method(_MD("set_mass","mass"),&RigidBody2D::set_mass);
ObjectTypeDB::bind_method(_MD("get_mass"),&RigidBody2D::get_mass);
ObjectTypeDB::bind_method(_MD("get_inertia"),&RigidBody2D::get_inertia);
ObjectTypeDB::bind_method(_MD("set_weight","weight"),&RigidBody2D::set_weight);
ObjectTypeDB::bind_method(_MD("get_weight"),&RigidBody2D::get_weight);

View File

@ -217,6 +217,8 @@ public:
void set_mass(real_t p_mass);
real_t get_mass() const;
real_t get_inertia() const; // read-only: auto-computed from mass & shapes.
void set_weight(real_t p_weight);
real_t get_weight() const;

View File

@ -186,6 +186,10 @@ float Body2DSW::get_param(Physics2DServer::BodyParameter p_param) const {
case Physics2DServer::BODY_PARAM_MASS: {
return mass;
} break;
case Physics2DServer::BODY_PARAM_INERTIA: {
if(_inv_inertia == 0) return INFINITY;
else return 1.0 / _inv_inertia;
} break;
case Physics2DServer::BODY_PARAM_GRAVITY_SCALE: {
return gravity_scale;
} break;

View File

@ -677,6 +677,7 @@ void Physics2DServer::_bind_methods() {
BIND_CONSTANT( BODY_PARAM_BOUNCE );
BIND_CONSTANT( BODY_PARAM_FRICTION );
BIND_CONSTANT( BODY_PARAM_MASS );
BIND_CONSTANT( BODY_PARAM_INERTIA );
BIND_CONSTANT( BODY_PARAM_GRAVITY_SCALE );
BIND_CONSTANT( BODY_PARAM_LINEAR_DAMP);
BIND_CONSTANT( BODY_PARAM_ANGULAR_DAMP);

View File

@ -421,6 +421,7 @@ public:
BODY_PARAM_BOUNCE,
BODY_PARAM_FRICTION,
BODY_PARAM_MASS, ///< unused for static, always infinite
BODY_PARAM_INERTIA, // read-only: computed from mass & shapes
BODY_PARAM_GRAVITY_SCALE,
BODY_PARAM_LINEAR_DAMP,
BODY_PARAM_ANGULAR_DAMP,