summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Grams <josh@qualdan.com>2016-04-20 20:49:37 -0400
committerJosh Grams <josh@qualdan.com>2016-04-20 20:49:37 -0400
commitf7d31cec38c795909c4d1e0917f54aa118d380d7 (patch)
tree842bb5cd786dbce408bb7f5c563a9502542c05eb
parentdbabe4c07cd9c0d825c14b961bd472f77e525162 (diff)
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.
-rw-r--r--doc/base/classes.xml19
-rw-r--r--scene/2d/physics_body_2d.cpp7
-rw-r--r--scene/2d/physics_body_2d.h2
-rw-r--r--servers/physics_2d/body_2d_sw.cpp4
-rw-r--r--servers/physics_2d_server.cpp1
-rw-r--r--servers/physics_2d_server.h1
6 files changed, 30 insertions, 4 deletions
diff --git a/doc/base/classes.xml b/doc/base/classes.xml
index 9b951be0d2..6aebbabea9 100644
--- a/doc/base/classes.xml
+++ b/doc/base/classes.xml
@@ -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">
diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp
index cc6145677e..933e1579b2 100644
--- a/scene/2d/physics_body_2d.cpp
+++ b/scene/2d/physics_body_2d.cpp
@@ -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);
diff --git a/scene/2d/physics_body_2d.h b/scene/2d/physics_body_2d.h
index 40cb25d8c1..7523413df3 100644
--- a/scene/2d/physics_body_2d.h
+++ b/scene/2d/physics_body_2d.h
@@ -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;
diff --git a/servers/physics_2d/body_2d_sw.cpp b/servers/physics_2d/body_2d_sw.cpp
index ab1c7ef66f..97f08a6aa3 100644
--- a/servers/physics_2d/body_2d_sw.cpp
+++ b/servers/physics_2d/body_2d_sw.cpp
@@ -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;
diff --git a/servers/physics_2d_server.cpp b/servers/physics_2d_server.cpp
index 2d267a5749..0ff5dba6ee 100644
--- a/servers/physics_2d_server.cpp
+++ b/servers/physics_2d_server.cpp
@@ -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);
diff --git a/servers/physics_2d_server.h b/servers/physics_2d_server.h
index 25875f16d3..2d6c4fe3f8 100644
--- a/servers/physics_2d_server.h
+++ b/servers/physics_2d_server.h
@@ -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,