Browse Source

Add function to control 2D physics update

cosmy 10 years ago
parent
commit
04996445cc

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

@@ -394,6 +394,8 @@ static void RegisterPhysicsWorld2D(asIScriptEngine* engine)
     engine->RegisterObjectMethod("PhysicsWorld2D", "RigidBody2D@+ GetRigidBody(const Vector2&, uint collisionMask = 0xffff)", asMETHODPR(PhysicsWorld2D, GetRigidBody, (const Vector2&, unsigned), RigidBody2D*), asCALL_THISCALL);
     engine->RegisterObjectMethod("PhysicsWorld2D", "RigidBody2D@+ GetRigidBody(int, int, uint collisionMask = 0xffff)", asMETHODPR(PhysicsWorld2D, GetRigidBody, (int, int, unsigned), RigidBody2D*), asCALL_THISCALL);
     engine->RegisterObjectMethod("PhysicsWorld2D", "Array<RigidBody2D@>@ GetRigidBodies(const Rect&in, uint collisionMask = 0xffff)", asFUNCTION(PhysicsWorld2DGetRigidBodies), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectMethod("PhysicsWorld2D", "void set_updateEnabled(bool)", asMETHOD(PhysicsWorld2D, SetUpdateEnabled), asCALL_THISCALL);
+    engine->RegisterObjectMethod("PhysicsWorld2D", "bool get_updateEnabled() const", asMETHOD(PhysicsWorld2D, IsUpdateEnabled), asCALL_THISCALL);
     engine->RegisterObjectMethod("PhysicsWorld2D", "void set_drawShape(bool)", asMETHOD(PhysicsWorld2D, SetDrawShape), asCALL_THISCALL);
     engine->RegisterObjectMethod("PhysicsWorld2D", "bool get_drawShape() const", asMETHOD(PhysicsWorld2D, GetDrawShape), asCALL_THISCALL);
     engine->RegisterObjectMethod("PhysicsWorld2D", "void set_drawJoint(bool)", asMETHOD(PhysicsWorld2D, SetDrawJoint), asCALL_THISCALL);

+ 3 - 0
Source/Urho3D/LuaScript/pkgs/Urho2D/PhysicsWorld2D.pkg

@@ -14,6 +14,7 @@ struct PhysicsRaycastResult2D
 class PhysicsWorld2D : Component
 {
     void DrawDebugGeometry();
+	void SetUpdateEnabled(bool enable);
     void SetDrawShape(bool drawShape);
     void SetDrawJoint(bool drawJoint);
     void SetDrawAabb(bool drawAabb);
@@ -37,6 +38,7 @@ class PhysicsWorld2D : Component
     // void GetRigidBodies(PODVector<RigidBody2D*>& result, const Rect& aabb, unsigned collisionMask = M_MAX_UNSIGNED);
     tolua_outside const PODVector<RigidBody2D*>& PhysicsWorld2DGetRigidBodies @ GetRigidBodies(const Rect& aabb, unsigned collisionMask = M_MAX_UNSIGNED);
 
+	bool IsUpdateEnabled() const;
     bool GetDrawShape() const;
     bool GetDrawJoint() const;
     bool GetDrawAabb() const;
@@ -51,6 +53,7 @@ class PhysicsWorld2D : Component
     int GetVelocityIterations() const;
     int GetPositionIterations() const;
 
+	tolua_property__is_set bool updateEnabled;
     tolua_property__get_set bool drawShape;
     tolua_property__get_set bool drawJoint;
     tolua_property__get_set bool drawAabb;

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

@@ -53,7 +53,8 @@ PhysicsWorld2D::PhysicsWorld2D(Context* context) :
     positionIterations_(DEFAULT_POSITION_ITERATIONS),
     debugRenderer_(0),
     physicsSteping_(false),
-    applyingTransforms_(false)
+    applyingTransforms_(false),
+    updateEnabled_(true)
 {
     // Set default debug draw flags
     m_drawFlags = e_shapeBit;
@@ -226,6 +227,9 @@ void PhysicsWorld2D::DrawTransform(const b2Transform& xf)
 
 void PhysicsWorld2D::Update(float timeStep)
 {
+    if (!updateEnabled_)
+        return;
+
     URHO3D_PROFILE(UpdatePhysics2D);
 
     using namespace PhysicsPreStep2D;
@@ -256,6 +260,11 @@ void PhysicsWorld2D::DrawDebugGeometry()
         DrawDebugGeometry(debug, false);
 }
 
+void PhysicsWorld2D::SetUpdateEnabled(bool enable)
+{
+    updateEnabled_ = enable;
+}
+
 void PhysicsWorld2D::SetDrawShape(bool drawShape)
 {
     if (drawShape)

+ 7 - 0
Source/Urho3D/Urho2D/PhysicsWorld2D.h

@@ -97,6 +97,8 @@ public:
     void Update(float timeStep);
     /// Add debug geometry to the debug renderer.
     void DrawDebugGeometry();
+    /// Enable or disable physics update.
+    void SetUpdateEnabled(bool enable);
     /// Set draw shape.
     void SetDrawShape(bool drawShape);
     /// Set draw joint.
@@ -141,6 +143,9 @@ 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.
+    bool IsUpdateEnabled() const { return updateEnabled_; }
+
     /// Return draw shape.
     bool GetDrawShape() const { return (m_drawFlags & e_shapeBit) != 0; }
 
@@ -213,6 +218,8 @@ private:
     /// Debug draw depth test mode.
     bool debugDepthTest_;
 
+    /// Physics update state.
+    bool updateEnabled_;
     /// Physics steping.
     bool physicsSteping_;
     /// Applying transforms.