浏览代码

Added Enable/Disable flag for RigidBody.
Reduced GCC optimization level to -O2 for stability.

Lasse Öörni 12 年之前
父节点
当前提交
e037de8c74
共有 5 个文件被更改,包括 45 次插入19 次删除
  1. 2 2
      CMakeLists.txt
  2. 3 3
      Engine/Physics/CollisionShape.cpp
  3. 1 1
      Engine/Physics/CollisionShape.h
  4. 32 12
      Engine/Physics/RigidBody.cpp
  5. 7 1
      Engine/Physics/RigidBody.h

+ 2 - 2
CMakeLists.txt

@@ -84,8 +84,8 @@ if (MSVC)
     set (CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /OPT:REF /OPT:ICF")
 else ()
     if (NOT IOS)
-        set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32 -O3 -ffast-math")
-        set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-invalid-offsetof -m32 -O3 -ffast-math")
+        set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32 -O2 -ffast-math")
+        set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-invalid-offsetof -m32 -O2 -ffast-math")
         if (ENABLE_SSE)
             set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse")
             set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse")

+ 3 - 3
Engine/Physics/CollisionShape.cpp

@@ -55,8 +55,8 @@ namespace Urho3D
 
 static const float DEFAULT_COLLISION_MARGIN = 0.04f;
 
-static const btVector3 white(1.0f, 1.0f, 1.0f);
-static const btVector3 green(0.0f, 1.0f, 0.0f);
+static const btVector3 WHITE(1.0f, 1.0f, 1.0f);
+static const btVector3 GREEN(0.0f, 1.0f, 0.0f);
 
 static const char* typeNames[] = 
 {
@@ -342,7 +342,7 @@ void CollisionShape::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
         
         btDiscreteDynamicsWorld* world = physicsWorld_->GetWorld();
         world->debugDrawObject(btTransform(ToBtQuaternion(worldRotation), ToBtVector3(worldPosition)), shape_, bodyActive ?
-            white : green);
+            WHITE : GREEN);
         
         physicsWorld_->SetDebugRenderer(0);
     }

+ 1 - 1
Engine/Physics/CollisionShape.h

@@ -128,7 +128,7 @@ public:
     virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
     /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
     virtual void ApplyAttributes();
-        /// Handle enabled/disabled state change.
+    /// Handle enabled/disabled state change.
     virtual void OnSetEnabled();
     /// Visualize the component as debug geometry.
     virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);

+ 32 - 12
Engine/Physics/RigidBody.cpp

@@ -74,7 +74,8 @@ RigidBody::RigidBody(Context* context) :
     phantom_(false),
     useGravity_(true),
     hasSmoothedTransform_(false),
-    readdBody_(false)
+    readdBody_(false),
+	inWorld_(false)
 {
     compoundShape_ = new btCompoundShape();
 }
@@ -94,6 +95,7 @@ void RigidBody::RegisterObject(Context* context)
 {
     context->RegisterFactory<RigidBody>();
     
+    ACCESSOR_ATTRIBUTE(RigidBody, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
     ACCESSOR_ATTRIBUTE(RigidBody, VAR_VECTOR3, "Physics Position", GetPosition, SetPosition, Vector3, Vector3::ZERO, AM_FILE | AM_NOEDIT);
     ACCESSOR_ATTRIBUTE(RigidBody, VAR_QUATERNION, "Physics Rotation", GetRotation, SetRotation, Quaternion, Quaternion::IDENTITY, AM_FILE | AM_NOEDIT);
     ATTRIBUTE(RigidBody, VAR_FLOAT, "Mass", mass_, DEFAULT_MASS, AM_DEFAULT);
@@ -131,10 +133,17 @@ void RigidBody::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
 void RigidBody::ApplyAttributes()
 {
     if (readdBody_)
-    {
         AddBodyToWorld();
-        readdBody_ = false;
-    }
+}
+
+void RigidBody::OnSetEnabled()
+{
+	bool enabled = IsEnabledEffective();
+
+	if (enabled && !inWorld_)
+		AddBodyToWorld();
+	else if (!enabled && inWorld_)
+		RemoveBodyFromWorld();
 }
 
 void RigidBody::getWorldTransform(btTransform &worldTrans) const
@@ -752,11 +761,7 @@ void RigidBody::ReleaseBody()
         for (PODVector<Constraint*>::Iterator i = constraints.Begin(); i != constraints.End(); ++i)
             (*i)->ReleaseConstraint();
         
-        if (physicsWorld_)
-        {
-            btDiscreteDynamicsWorld* world = physicsWorld_->GetWorld();
-            world->removeRigidBody(body_);
-        }
+		RemoveBodyFromWorld();
         
         delete body_;
         body_ = 0;
@@ -829,9 +834,8 @@ void RigidBody::AddBodyToWorld()
     
     bool massUpdated = false;
     
-    btDiscreteDynamicsWorld* world = physicsWorld_->GetWorld();
     if (body_)
-        world->removeRigidBody(body_);
+        RemoveBodyFromWorld();
     else
     {
         // Correct inertia will be calculated below
@@ -883,8 +887,14 @@ void RigidBody::AddBodyToWorld()
         flags &= ~btCollisionObject::CF_KINEMATIC_OBJECT;
     body_->setCollisionFlags(flags);
     
+	if (!IsEnabledEffective())
+		return;
+
+    btDiscreteDynamicsWorld* world = physicsWorld_->GetWorld();
     world->addRigidBody(body_, collisionLayer_, collisionMask_);
-    
+    inWorld_ = true;
+	readdBody_ = false;
+
     if (mass_ > 0.0f)
         Activate();
     else
@@ -894,6 +904,16 @@ void RigidBody::AddBodyToWorld()
     }
 }
 
+void RigidBody::RemoveBodyFromWorld()
+{
+    if (physicsWorld_ && body_ && inWorld_)
+	{
+		btDiscreteDynamicsWorld* world = physicsWorld_->GetWorld();
+		world->removeRigidBody(body_);
+		inWorld_ = false;
+	}
+}
+
 void RigidBody::HandleTargetPosition(StringHash eventType, VariantMap& eventData)
 {
     // Copy the smoothing target position to the rigid body

+ 7 - 1
Engine/Physics/RigidBody.h

@@ -62,7 +62,9 @@ public:
     virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
     /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
     virtual void ApplyAttributes();
-    /// Return initial world transform to Bullet.
+    /// Handle enabled/disabled state change.
+    virtual void OnSetEnabled();
+	/// Return initial world transform to Bullet.
     virtual void getWorldTransform(btTransform &worldTrans) const;
     /// Update world transform from Bullet.
     virtual void setWorldTransform(const btTransform &worldTrans);
@@ -219,6 +221,8 @@ protected:
 private:
     /// Create the rigid body, or re-add to the physics world with changed flags. Calls UpdateMass().
     void AddBodyToWorld();
+	/// Remove the rigid body from the physics world.
+	void RemoveBodyFromWorld();
     /// Handle SmoothedTransform target position update.
     void HandleTargetPosition(StringHash eventType, VariantMap& eventData);
     /// Handle SmoothedTransform target rotation update.
@@ -258,6 +262,8 @@ private:
     bool hasSmoothedTransform_;
     /// Readd body to world flag.
     bool readdBody_;
+	/// Body exists in world flag.
+	bool inWorld_;
 };
 
 }