From f7d31cec38c795909c4d1e0917f54aa118d380d7 Mon Sep 17 00:00:00 2001 From: Josh Grams Date: Wed, 20 Apr 2016 20:49:37 -0400 Subject: 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. --- doc/base/classes.xml | 19 +++++++++++++++---- scene/2d/physics_body_2d.cpp | 7 +++++++ scene/2d/physics_body_2d.h | 2 ++ servers/physics_2d/body_2d_sw.cpp | 4 ++++ servers/physics_2d_server.cpp | 1 + servers/physics_2d_server.h | 1 + 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 - + - + - + - + + + @@ -30035,6 +30037,13 @@ This method controls whether the position between two cached points is interpola Return the body mass. + + + + + 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... + + @@ -30238,12 +30247,14 @@ This method controls whether the position between two cached points is interpola + Set a constant torque which will be applied to this body. + Return the torque which is being applied to this body. 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, -- cgit v1.2.3