浏览代码

Delay send contact event.

aster 11 年之前
父节点
当前提交
cb0211e586

+ 4 - 1
Source/Engine/Urho2D/MaterialCache2D.cpp

@@ -72,7 +72,10 @@ Material* MaterialCache2D::GetMaterial(Texture2D* texture, BlendMode blendMode)
 Material* MaterialCache2D::CreateMaterial(Texture2D* texture, BlendMode blendMode)
 Material* MaterialCache2D::CreateMaterial(Texture2D* texture, BlendMode blendMode)
 {
 {
     Material* material = new Material(context_);
     Material* material = new Material(context_);
-    material->SetName(texture->GetName() + "_" + blendModeNames[blendMode]);
+    if (texture)
+        material->SetName(texture->GetName() + "_" + blendModeNames[blendMode]);
+    else
+        material->SetName(blendModeNames[blendMode]);
 
 
     Technique* tech = new Technique(context_);
     Technique* tech = new Technique(context_);
     Pass* pass = tech->CreatePass(PASS_ALPHA);
     Pass* pass = tech->CreatePass(PASS_ALPHA);

+ 1 - 1
Source/Engine/Urho2D/PhysicsEvents2D.h

@@ -52,7 +52,7 @@ EVENT(E_PHYSICSBEGINCONTACT2D, PhysicsBeginContact2D)
 }
 }
 
 
 /// Physics end contract.
 /// Physics end contract.
-EVENT(E_PHYSICSENDCOLLISION2D, PhysicsEndContact2D)
+EVENT(E_PHYSICSENDCONTACT2D, PhysicsEndContact2D)
 {
 {
     PARAM(P_WORLD, World);                  // PhysicsWorld2D pointer
     PARAM(P_WORLD, World);                  // PhysicsWorld2D pointer
     PARAM(P_BODYA, BodyA);                  // RigidBody2D pointer
     PARAM(P_BODYA, BodyA);                  // RigidBody2D pointer

+ 73 - 24
Source/Engine/Urho2D/PhysicsWorld2D.cpp

@@ -117,18 +117,7 @@ void PhysicsWorld2D::BeginContact(b2Contact* contact)
     if (!fixtureA || !fixtureB)
     if (!fixtureA || !fixtureB)
         return;
         return;
 
 
-    using namespace PhysicsBeginContact2D;
-    VariantMap& eventData = GetEventDataMap();
-    eventData[P_WORLD] = this;
-
-    RigidBody2D* rigidBodyA = (RigidBody2D*)(fixtureA->GetBody()->GetUserData());
-    RigidBody2D* rigidBodyB = (RigidBody2D*)(fixtureB->GetBody()->GetUserData());
-    eventData[P_BODYA] = rigidBodyA;
-    eventData[P_BODYB] = rigidBodyB;
-    eventData[P_NODEA] = rigidBodyA->GetNode();
-    eventData[P_NODEB] = rigidBodyB->GetNode();
-
-    SendEvent(E_PHYSICSBEGINCONTACT2D, eventData);
+    beginContactInfos_.Push(ContactInfo(contact));
 }
 }
 
 
 void PhysicsWorld2D::EndContact(b2Contact* contact)
 void PhysicsWorld2D::EndContact(b2Contact* contact)
@@ -138,18 +127,7 @@ void PhysicsWorld2D::EndContact(b2Contact* contact)
     if (!fixtureA || !fixtureB)
     if (!fixtureA || !fixtureB)
         return;
         return;
 
 
-    using namespace PhysicsEndContact2D;
-    VariantMap& eventData = GetEventDataMap();
-    eventData[P_WORLD] = this;
-
-    RigidBody2D* rigidBodyA = (RigidBody2D*)(fixtureA->GetBody()->GetUserData());
-    RigidBody2D* rigidBodyB = (RigidBody2D*)(fixtureB->GetBody()->GetUserData());
-    eventData[P_BODYA] = rigidBodyA;
-    eventData[P_BODYB] = rigidBodyB;
-    eventData[P_NODEA] = rigidBodyA->GetNode();
-    eventData[P_NODEB] = rigidBodyB->GetNode();
-
-    SendEvent(E_PHYSICSENDCOLLISION2D, eventData);
+    endContactInfos_.Push(ContactInfo(contact));
 }
 }
 
 
 void PhysicsWorld2D::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
 void PhysicsWorld2D::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
@@ -249,6 +227,9 @@ void PhysicsWorld2D::Update(float timeStep)
     for (unsigned i = 0; i < rigidBodies_.Size(); ++i)
     for (unsigned i = 0; i < rigidBodies_.Size(); ++i)
         rigidBodies_[i]->ApplyWorldTransform();
         rigidBodies_[i]->ApplyWorldTransform();
 
 
+    SendBeginContactEvents();
+    SendEndContactEvents();
+
     using namespace PhysicsPostStep2D;
     using namespace PhysicsPostStep2D;
     SendEvent(E_PHYSICSPOSTSTEP2D, eventData);
     SendEvent(E_PHYSICSPOSTSTEP2D, eventData);
 }
 }
@@ -632,4 +613,72 @@ void PhysicsWorld2D::HandleSceneSubsystemUpdate(StringHash eventType, VariantMap
     Update(eventData[P_TIMESTEP].GetFloat());
     Update(eventData[P_TIMESTEP].GetFloat());
 }
 }
 
 
+void PhysicsWorld2D::SendBeginContactEvents()
+{
+    if (beginContactInfos_.Empty())
+        return;
+
+    using namespace PhysicsBeginContact2D;
+    VariantMap& eventData = GetEventDataMap();
+    eventData[P_WORLD] = this;
+
+    for (unsigned i = 0; i < beginContactInfos_.Size(); ++i)
+    {
+        ContactInfo& contactInfo = beginContactInfos_[i];
+        eventData[P_BODYA] = contactInfo.bodyA_.Get();
+        eventData[P_BODYB] = contactInfo.bodyB_.Get();
+        eventData[P_NODEA] = contactInfo.nodeA_.Get();
+        eventData[P_NODEB] = contactInfo.nodeB_.Get();
+
+        SendEvent(E_PHYSICSBEGINCONTACT2D, eventData);
+    }
+
+    beginContactInfos_.Clear();
+}
+
+void PhysicsWorld2D::SendEndContactEvents()
+{
+   if (endContactInfos_.Empty())
+        return;
+
+    using namespace PhysicsEndContact2D;
+    VariantMap& eventData = GetEventDataMap();
+    eventData[P_WORLD] = this;
+
+    for (unsigned i = 0; i < endContactInfos_.Size(); ++i)
+    {
+        ContactInfo& contactInfo = endContactInfos_[i];
+        eventData[P_BODYA] = contactInfo.bodyA_.Get();
+        eventData[P_BODYB] = contactInfo.bodyB_.Get();
+        eventData[P_NODEA] = contactInfo.nodeA_.Get();
+        eventData[P_NODEB] = contactInfo.nodeB_.Get();
+
+        SendEvent(E_PHYSICSENDCONTACT2D, eventData);
+    }
+
+    endContactInfos_.Clear();
+}
+
+PhysicsWorld2D::ContactInfo::ContactInfo()
+{
+}
+
+PhysicsWorld2D::ContactInfo::ContactInfo(b2Contact* contact)
+{
+    b2Fixture* fixtureA = contact->GetFixtureA();
+    b2Fixture* fixtureB = contact->GetFixtureB();
+    bodyA_ = (RigidBody2D*)(fixtureA->GetBody()->GetUserData());
+    bodyB_ = (RigidBody2D*)(fixtureB->GetBody()->GetUserData());
+    nodeA_ = bodyA_->GetNode();
+    nodeB_ = bodyB_->GetNode();
+}
+
+PhysicsWorld2D::ContactInfo::ContactInfo(const ContactInfo& other) :
+    bodyA_(other.bodyA_),
+    bodyB_(other.bodyB_),
+    nodeA_(other.nodeA_),
+    nodeB_(other.nodeB_)
+{
+}
+
 }
 }

+ 28 - 0
Source/Engine/Urho2D/PhysicsWorld2D.h

@@ -175,6 +175,10 @@ protected:
 private:
 private:
     /// Handle the scene subsystem update event, step simulation here.
     /// Handle the scene subsystem update event, step simulation here.
     void HandleSceneSubsystemUpdate(StringHash eventType, VariantMap& eventData);
     void HandleSceneSubsystemUpdate(StringHash eventType, VariantMap& eventData);
+    /// Send begin contact events.
+    void SendBeginContactEvents();
+    /// Send end contact events.
+    void SendEndContactEvents();
 
 
     /// Box2D physics world.
     /// Box2D physics world.
     b2World* world_;
     b2World* world_;
@@ -196,6 +200,30 @@ private:
     bool applyingTransforms_;
     bool applyingTransforms_;
     /// Rigid bodies.
     /// Rigid bodies.
     Vector<WeakPtr<RigidBody2D> > rigidBodies_;
     Vector<WeakPtr<RigidBody2D> > rigidBodies_;
+
+    /// Contact info.
+    struct ContactInfo
+    {
+        /// Construct.
+        ContactInfo();
+        /// Construct.
+        ContactInfo(b2Contact* contract);
+        /// Copy construct.
+        ContactInfo(const ContactInfo& other);
+
+        /// Rigid body A.
+        SharedPtr<RigidBody2D> bodyA_;
+        /// Rigid body B.
+        SharedPtr<RigidBody2D> bodyB_;
+        /// Node A.
+        SharedPtr<Node> nodeA_;
+        /// Node B.
+        SharedPtr<Node> nodeB_;
+    };
+    /// Begin contact infos.
+    Vector<ContactInfo> beginContactInfos_;
+    /// End contact infos.
+    Vector<ContactInfo> endContactInfos_;
 };
 };
 
 
 }
 }