Преглед изворни кода

Added Bullet rolling friction parameter to RigidBody.
RigidBody code cleanup.

Lasse Öörni пре 12 година
родитељ
комит
c14bf5bd69

+ 32 - 64
Source/Engine/Physics/RigidBody.cpp

@@ -46,6 +46,7 @@ namespace Urho3D
 static const float DEFAULT_MASS = 0.0f;
 static const float DEFAULT_FRICTION = 0.5f;
 static const float DEFAULT_RESTITUTION = 0.0f;
+static const float DEFAULT_ROLLING_FRICTION = 0.0f;
 static const unsigned DEFAULT_COLLISION_LAYER = 0x1;
 static const unsigned DEFAULT_COLLISION_MASK = M_MAX_UNSIGNED;
 
@@ -105,6 +106,7 @@ void RigidBody::RegisterObject(Context* context)
     ACCESSOR_ATTRIBUTE(RigidBody, VAR_VECTOR3, "Physics Position", GetPosition, SetPosition, Vector3, Vector3::ZERO, AM_FILE | AM_NOEDIT);
     ATTRIBUTE(RigidBody, VAR_FLOAT, "Mass", mass_, DEFAULT_MASS, AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(RigidBody, VAR_FLOAT, "Friction", GetFriction, SetFriction, float, DEFAULT_FRICTION, AM_DEFAULT);
+    ACCESSOR_ATTRIBUTE(RigidBody, VAR_FLOAT, "Rolling Friction", GetRollingFriction, SetRollingFriction, float, DEFAULT_ROLLING_FRICTION, AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(RigidBody, VAR_FLOAT, "Restitution", GetRestitution, SetRestitution, float, DEFAULT_RESTITUTION, AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(RigidBody, VAR_VECTOR3, "Linear Velocity", GetLinearVelocity, SetLinearVelocity, Vector3, Vector3::ZERO, AM_DEFAULT | AM_LATESTDATA);
     ACCESSOR_ATTRIBUTE(RigidBody, VAR_VECTOR3, "Angular Velocity", GetAngularVelocity, SetAngularVelocity, Vector3, Vector3::ZERO, AM_FILE);
@@ -369,6 +371,15 @@ void RigidBody::SetFriction(float friction)
     }
 }
 
+void RigidBody::SetRollingFriction(float friction)
+{
+    if (body_)
+    {
+        body_->setRollingFriction(friction);
+        MarkNetworkUpdate();
+    }
+}
+
 void RigidBody::SetRestitution(float restitution)
 {
     if (body_)
@@ -569,130 +580,87 @@ Vector3 RigidBody::GetPosition() const
 
 Quaternion RigidBody::GetRotation() const
 {
-    if (body_)
-        return ToQuaternion(body_->getWorldTransform().getRotation());
-    else
-        return Quaternion::IDENTITY;
+    return body_ ? ToQuaternion(body_->getWorldTransform().getRotation()) : Quaternion::IDENTITY;
 }
 
 Vector3 RigidBody::GetLinearVelocity() const
 {
-    if (body_)
-        return ToVector3(body_->getLinearVelocity());
-    else
-        return Vector3::ZERO;
+    return body_ ? ToVector3(body_->getLinearVelocity()) : Vector3::ZERO;
 }
 
 Vector3 RigidBody::GetLinearFactor() const
 {
-    if (body_)
-        return ToVector3(body_->getLinearFactor());
-    else
-        return Vector3::ZERO;
+    return body_ ? ToVector3(body_->getLinearFactor()) : Vector3::ZERO;
 }
 
 Vector3 RigidBody::GetVelocityAtPoint(const Vector3& position) const
 {
-    if (body_)
-        return ToVector3(body_->getVelocityInLocalPoint(ToBtVector3(position - centerOfMass_)));
-    else
-        return Vector3::ZERO;
+    return body_ ? ToVector3(body_->getVelocityInLocalPoint(ToBtVector3(position - centerOfMass_))) : Vector3::ZERO;
 }
 
 float RigidBody::GetLinearRestThreshold() const
 {
-    if (body_)
-        return body_->getLinearSleepingThreshold();
-    else
-        return 0.0f;
+    return body_ ? body_->getLinearSleepingThreshold() : 0.0f;
 }
 
 float RigidBody::GetLinearDamping() const
 {
-    if (body_)
-        return body_->getLinearDamping();
-    else
-        return 0.0f;
+    return body_ ? body_->getLinearDamping() : 0.0f;
 }
 
 Vector3 RigidBody::GetAngularVelocity() const
 {
-    if (body_)
-        return ToVector3(body_->getAngularVelocity());
-    else
-        return Vector3::ZERO;
+    return body_ ? ToVector3(body_->getAngularVelocity()) : Vector3::ZERO;
 }
 
 Vector3 RigidBody::GetAngularFactor() const
 {
-    if (body_)
-        return ToVector3(body_->getAngularFactor());
-    else
-        return Vector3::ZERO;
+    return body_ ? ToVector3(body_->getAngularFactor()) : Vector3::ZERO;
 }
 
 float RigidBody::GetAngularRestThreshold() const
 {
-    if (body_)
-        return body_->getAngularSleepingThreshold();
-    else
-        return 0.0f;
+    return body_ ? body_->getAngularSleepingThreshold() : 0.0f;
 }
 
 float RigidBody::GetAngularDamping() const
 {
-    if (body_)
-        return body_->getAngularDamping();
-    else
-        return 0.0f;
+    return body_ ? body_->getAngularDamping() : 0.0f;
 }
 
 float RigidBody::GetFriction() const
 {
-    if (body_)
-        return body_->getFriction();
-    else
-        return 0.0f;
+    return body_ ? body_->getFriction() : 0.0f;
+}
+
+float RigidBody::GetRollingFriction() const
+{
+    return body_ ? body_->getRollingFriction() : 0.0f;
 }
 
 float RigidBody::GetRestitution() const
 {
-    if (body_)
-        return body_->getRestitution();
-    else
-        return 0.0f;
+    return body_ ? body_->getRestitution() : 0.0f;
 }
 
 float RigidBody::GetContactProcessingThreshold() const
 {
-    if (body_)
-        return body_->getContactProcessingThreshold();
-    else
-        return 0.0f;
+    return body_ ? body_->getContactProcessingThreshold() : 0.0f;
 }
 
 float RigidBody::GetCcdRadius() const
 {
-    if (body_)
-        return body_->getCcdSweptSphereRadius();
-    else
-        return 0.0f;
+    return body_ ? body_->getCcdSweptSphereRadius() : 0.0f;
 }
 
 float RigidBody::GetCcdMotionThreshold() const
 {
-    if (body_)
-        return body_->getCcdMotionThreshold();
-    else
-        return 0.0f;
+    return body_ ? body_->getCcdMotionThreshold() : 0.0f;
 }
 
 bool RigidBody::IsActive() const
 {
-    if (body_)
-        return body_->isActive();
-    else
-        return false;
+    return body_ ? body_->isActive() : false;
 }
 
 void RigidBody::GetCollidingBodies(PODVector<RigidBody*>& result) const

+ 4 - 0
Source/Engine/Physics/RigidBody.h

@@ -97,6 +97,8 @@ public:
     void SetAngularDamping(float factor);
     /// Set friction coefficient.
     void SetFriction(float friction);
+    /// Set rolling friction coefficient.
+    void SetRollingFriction(float friction);
     /// Set restitution coefficient.
     void SetRestitution(float restitution);
     /// Set contact processing threshold.
@@ -172,6 +174,8 @@ public:
     float GetAngularDamping() const;
     /// Return friction coefficient.
     float GetFriction() const;
+    /// Return rolling friction coefficient.
+    float GetRollingFriction() const;
     /// Return restitution coefficient.
     float GetRestitution() const;
     /// Return contact processing threshold.

+ 2 - 0
Source/Engine/Script/PhysicsAPI.cpp

@@ -153,6 +153,8 @@ static void RegisterRigidBody(asIScriptEngine* engine)
     engine->RegisterObjectMethod("RigidBody", "float get_angularDamping() const", asMETHOD(RigidBody, GetAngularDamping), asCALL_THISCALL);
     engine->RegisterObjectMethod("RigidBody", "void set_friction(float)", asMETHOD(RigidBody, SetFriction), asCALL_THISCALL);
     engine->RegisterObjectMethod("RigidBody", "float get_friction() const", asMETHOD(RigidBody, GetFriction), asCALL_THISCALL);
+    engine->RegisterObjectMethod("RigidBody", "void set_rollingFriction(float)", asMETHOD(RigidBody, SetRollingFriction), asCALL_THISCALL);
+    engine->RegisterObjectMethod("RigidBody", "float get_rollingFriction() const", asMETHOD(RigidBody, GetRollingFriction), asCALL_THISCALL);
     engine->RegisterObjectMethod("RigidBody", "void set_restitution(float)", asMETHOD(RigidBody, SetRestitution), asCALL_THISCALL);
     engine->RegisterObjectMethod("RigidBody", "float get_restitution() const", asMETHOD(RigidBody, GetRestitution), asCALL_THISCALL);
     engine->RegisterObjectMethod("RigidBody", "void set_contactProcessingThreshold(float)", asMETHOD(RigidBody, SetContactProcessingThreshold), asCALL_THISCALL);

+ 3 - 0
Source/Extras/LuaScript/pkgs/Physics/RigidBody.pkg

@@ -22,6 +22,7 @@ class RigidBody : public Component
     void SetAngularRestThreshold(float threshold);
     void SetAngularDamping(float factor);
     void SetFriction(float friction);
+    void SetRollingFriction(float friction);
     void SetRestitution(float restitution);
     void SetContactProcessingThreshold(float threshold);
     void SetCcdRadius(float radius);
@@ -59,6 +60,7 @@ class RigidBody : public Component
     float GetAngularRestThreshold() const;
     float GetAngularDamping() const;
     float GetFriction() const;
+    float GetRollingFriction() const;
     float GetRestitution() const;
     float GetContactProcessingThreshold() const;
     float GetCcdRadius() const;
@@ -93,6 +95,7 @@ class RigidBody : public Component
     tolua_property__get_set float angularRestThreshold;
     tolua_property__get_set float angularDamping;
     tolua_property__get_set float friction;
+    tolua_property__get_set float rollingFriction;
     tolua_property__get_set float restitution;
     tolua_property__get_set float contactProcessingThreshold;
     tolua_property__get_set float ccdRadius;