浏览代码

Also add updateEnabled to PhysicsWorld. Make Scene, PhysicsWorld and PhysicsWorld2D behave in the same way: when auto update is disabled, manual stepping can still be performed. Rewrite some comments for clarity.

Lasse Öörni 10 年之前
父节点
当前提交
d4a5b74cc5

+ 2 - 0
Source/Urho3D/AngelScript/PhysicsAPI.cpp

@@ -313,6 +313,8 @@ static void RegisterPhysicsWorld(asIScriptEngine* engine)
     engine->RegisterObjectMethod("PhysicsWorld", "int get_numIterations() const", asMETHOD(PhysicsWorld, GetNumIterations), asCALL_THISCALL);
     engine->RegisterObjectMethod("PhysicsWorld", "void set_fps(int)", asMETHOD(PhysicsWorld, SetFps), asCALL_THISCALL);
     engine->RegisterObjectMethod("PhysicsWorld", "int get_fps() const", asMETHOD(PhysicsWorld, GetFps), asCALL_THISCALL);
+    engine->RegisterObjectMethod("PhysicsWorld", "void set_updateEnabled(bool)", asMETHOD(PhysicsWorld, SetUpdateEnabled), asCALL_THISCALL);
+    engine->RegisterObjectMethod("PhysicsWorld", "bool get_updateEnabled() const", asMETHOD(PhysicsWorld, IsUpdateEnabled), asCALL_THISCALL);
     engine->RegisterObjectMethod("PhysicsWorld", "void set_interpolation(bool)", asMETHOD(PhysicsWorld, SetInterpolation), asCALL_THISCALL);
     engine->RegisterObjectMethod("PhysicsWorld", "bool get_interpolation() const", asMETHOD(PhysicsWorld, GetInterpolation), asCALL_THISCALL);
     engine->RegisterObjectMethod("PhysicsWorld", "void set_internalEdge(bool)", asMETHOD(PhysicsWorld, SetInternalEdge), asCALL_THISCALL);

+ 3 - 0
Source/Urho3D/LuaScript/pkgs/Physics/PhysicsWorld.pkg

@@ -19,6 +19,7 @@ class PhysicsWorld : public Component
     void SetGravity(const Vector3& gravity);
     void SetMaxSubSteps(int num);
     void SetNumIterations(int num);
+    void SetUpdateEnabled(bool enable);
     void SetInterpolation(bool enable);
     void SetInternalEdge(bool enable);
     void SetSplitImpulse(bool enable);
@@ -46,6 +47,7 @@ class PhysicsWorld : public Component
     Vector3 GetGravity() const;
     int GetMaxSubSteps() const;
     int GetNumIterations() const;
+    bool IsUpdateEnabled() const;
     bool GetInterpolation() const;
     bool GetInternalEdge() const;
     bool GetSplitImpulse() const;
@@ -55,6 +57,7 @@ class PhysicsWorld : public Component
     tolua_property__get_set Vector3 gravity;
     tolua_property__get_set int maxSubSteps;
     tolua_property__get_set int numIterations;
+    tolua_property__is_set bool updateEnabled;
     tolua_property__get_set bool interpolation;
     tolua_property__get_set bool internalEdge;
     tolua_property__get_set bool splitImpulse;

+ 9 - 1
Source/Urho3D/Physics/PhysicsWorld.cpp

@@ -125,6 +125,7 @@ PhysicsWorld::PhysicsWorld(Context* context) :
     maxSubSteps_(0),
     timeAcc_(0.0f),
     maxNetworkAngularVelocity_(DEFAULT_MAX_NETWORK_ANGULAR_VELOCITY),
+    updateEnabled_(true),
     interpolation_(true),
     internalEdge_(true),
     applyingTransforms_(false),
@@ -313,6 +314,11 @@ void PhysicsWorld::SetNumIterations(int num)
     MarkNetworkUpdate();
 }
 
+void PhysicsWorld::SetUpdateEnabled(bool enable)
+{
+    updateEnabled_ = enable;
+}
+
 void PhysicsWorld::SetInterpolation(bool enable)
 {
     interpolation_ = enable;
@@ -698,8 +704,10 @@ void PhysicsWorld::OnSceneSet(Scene* scene)
 
 void PhysicsWorld::HandleSceneSubsystemUpdate(StringHash eventType, VariantMap& eventData)
 {
-    using namespace SceneSubsystemUpdate;
+    if (!updateEnabled_)
+        return;
 
+    using namespace SceneSubsystemUpdate;
     Update(eventData[P_TIMESTEP].GetFloat());
 }
 

+ 7 - 0
Source/Urho3D/Physics/PhysicsWorld.h

@@ -145,6 +145,8 @@ public:
     void SetMaxSubSteps(int num);
     /// Set number of constraint solver iterations.
     void SetNumIterations(int num);
+    /// Enable or disable automatic physics simulation during scene update. Enabled by default.
+    void SetUpdateEnabled(bool enable);
     /// Set whether to interpolate between simulation steps.
     void SetInterpolation(bool enable);
     /// Set whether to use Bullet's internal edge utility for trimesh collisions. Disabled by default.
@@ -185,6 +187,9 @@ public:
     /// Return number of constraint solver iterations.
     int GetNumIterations() const;
 
+    /// Return whether physics world will automatically simulate during scene update.
+    bool IsUpdateEnabled() const { return updateEnabled_; }
+
     /// Return whether interpolation between simulation steps is enabled.
     bool GetInterpolation() const { return interpolation_; }
 
@@ -295,6 +300,8 @@ private:
     float timeAcc_;
     /// Maximum angular velocity for network replication.
     float maxNetworkAngularVelocity_;
+    /// Automatic simulation update enabled flag.
+    bool updateEnabled_;
     /// Interpolation flag.
     bool interpolation_;
     /// Use internal edge utility flag.

+ 4 - 3
Source/Urho3D/Scene/Scene.cpp

@@ -961,10 +961,11 @@ void Scene::MarkReplicationDirty(Node* node)
 
 void Scene::HandleUpdate(StringHash eventType, VariantMap& eventData)
 {
-    using namespace Update;
+    if (!updateEnabled_)
+        return;
 
-    if (updateEnabled_)
-        Update(eventData[P_TIMESTEP].GetFloat());
+    using namespace Update;
+    Update(eventData[P_TIMESTEP].GetFloat());
 }
 
 void Scene::HandleResourceBackgroundLoaded(StringHash eventType, VariantMap& eventData)

+ 9 - 10
Source/Urho3D/Urho2D/PhysicsWorld2D.cpp

@@ -52,7 +52,7 @@ PhysicsWorld2D::PhysicsWorld2D(Context* context) :
     velocityIterations_(DEFAULT_VELOCITY_ITERATIONS),
     positionIterations_(DEFAULT_POSITION_ITERATIONS),
     debugRenderer_(0),
-    physicsSteping_(false),
+    physicsStepping_(false),
     applyingTransforms_(false),
     updateEnabled_(true)
 {
@@ -116,8 +116,8 @@ void PhysicsWorld2D::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
 
 void PhysicsWorld2D::BeginContact(b2Contact* contact)
 {
-    // Only handle contact event when physics steping
-    if (!physicsSteping_)
+    // Only handle contact event while stepping the physics simulation
+    if (!physicsStepping_)
         return;
 
     b2Fixture* fixtureA = contact->GetFixtureA();
@@ -130,8 +130,7 @@ void PhysicsWorld2D::BeginContact(b2Contact* contact)
 
 void PhysicsWorld2D::EndContact(b2Contact* contact)
 {
-    // Only handle contact event when physics steping
-    if (!physicsSteping_)
+    if (!physicsStepping_)
         return;
 
     b2Fixture* fixtureA = contact->GetFixtureA();
@@ -227,9 +226,6 @@ void PhysicsWorld2D::DrawTransform(const b2Transform& xf)
 
 void PhysicsWorld2D::Update(float timeStep)
 {
-    if (!updateEnabled_)
-        return;
-
     URHO3D_PROFILE(UpdatePhysics2D);
 
     using namespace PhysicsPreStep2D;
@@ -239,9 +235,9 @@ void PhysicsWorld2D::Update(float timeStep)
     eventData[P_TIMESTEP] = timeStep;
     SendEvent(E_PHYSICSPRESTEP2D, eventData);
 
-    physicsSteping_ = true;
+    physicsStepping_ = true;
     world_->Step(timeStep, velocityIterations_, positionIterations_);
-    physicsSteping_ = false;
+    physicsStepping_ = false;
 
     for (unsigned i = 0; i < rigidBodies_.Size(); ++i)
         rigidBodies_[i]->ApplyWorldTransform();
@@ -629,6 +625,9 @@ void PhysicsWorld2D::OnSceneSet(Scene* scene)
 
 void PhysicsWorld2D::HandleSceneSubsystemUpdate(StringHash eventType, VariantMap& eventData)
 {
+    if (!updateEnabled_)
+        return;
+
     using namespace SceneSubsystemUpdate;
     Update(eventData[P_TIMESTEP].GetFloat());
 }

+ 5 - 5
Source/Urho3D/Urho2D/PhysicsWorld2D.h

@@ -97,7 +97,7 @@ public:
     void Update(float timeStep);
     /// Add debug geometry to the debug renderer.
     void DrawDebugGeometry();
-    /// Enable or disable physics update.
+    /// Enable or disable automatic physics simulation during scene update. Enabled by default.
     void SetUpdateEnabled(bool enable);
     /// Set draw shape.
     void SetDrawShape(bool drawShape);
@@ -143,7 +143,7 @@ public:
     /// Return rigid bodies by a box query.
     void GetRigidBodies(PODVector<RigidBody2D*>& result, const Rect& aabb, unsigned collisionMask = M_MAX_UNSIGNED);
 
-    /// Return whether updates are enabled.
+    /// Return whether physics world will automatically simulate during scene update.
     bool IsUpdateEnabled() const { return updateEnabled_; }
 
     /// Return draw shape.
@@ -218,10 +218,10 @@ private:
     /// Debug draw depth test mode.
     bool debugDepthTest_;
 
-    /// Physics update state.
+    /// Automatic simulation update enabled flag.
     bool updateEnabled_;
-    /// Physics steping.
-    bool physicsSteping_;
+    /// Whether is currently stepping the world. Used internally.
+    bool physicsStepping_;
     /// Applying transforms.
     bool applyingTransforms_;
     /// Rigid bodies.