浏览代码

Add 2D sprite to physics samples.[ci skip]

aster2013 11 年之前
父节点
当前提交
c5c715ee84

+ 11 - 7
Bin/Data/LuaScripts/27_Urho2DPhysics.lua

@@ -50,6 +50,9 @@ function CreateScene()
     -- Create 2D physics world component
     scene_:CreateComponent("PhysicsWorld2D")
     
+    local boxSprite = cache:GetResource("Sprite2D", "Urho2D/Box.png")
+    local ballSprite = cache:GetResource("Sprite2D", "Urho2D/Ball.png")
+
     -- Create ground.
     local groundNode = scene_:CreateChild("Ground")
     groundNode.position = Vector3(0.0, -3.0, 0.0)
@@ -57,11 +60,14 @@ function CreateScene()
     
     -- Create 2D rigid body for gound
     local groundBody = groundNode:CreateComponent("RigidBody2D")
-    
+
+    local groundSprite = groundNode:CreateComponent("StaticSprite2D")
+    groundSprite.sprite = boxSprite
+
     -- Create box collider for ground
     local groundShape = groundNode:CreateComponent("CollisionBox2D")
     -- Set box size
-    groundShape.size = Vector2(0.1, 0.1)
+    groundShape.size = Vector2(0.32, 0.32)
     -- Set friction
     groundShape.friction = 0.5
     
@@ -74,13 +80,16 @@ function CreateScene()
         local body = node:CreateComponent("RigidBody2D")
         body.bodyType = BT_DYNAMIC
 
+        local staticSprite = node:CreateComponent("StaticSprite2D")
         local shape = nil
         if i % 2 == 0 then
+            staticSprite.sprite = boxSprite
             -- Create box
             shape = node:CreateComponent("CollisionBox2D")
             -- Set size
             shape.size = Vector2(0.32, 0.32)
         else
+            staticSprite.sprite = ballSprite
             -- Create circle
             shape = node:CreateComponent("CollisionCircle2D")
             -- Set radius
@@ -154,7 +163,6 @@ end
 function SubscribeToEvents()
     -- Subscribe HandleUpdate() function for processing update events
     SubscribeToEvent("Update", "HandleUpdate")
-    SubscribeToEvent("ScenePostUpdate", "HandleScenePostUpdate")
 end
 
 function HandleUpdate(eventType, eventData)
@@ -167,7 +175,3 @@ function HandleUpdate(eventType, eventData)
     -- Update scene
     scene_:Update(timeStep)
 end
-
-function HandleScenePostUpdate(eventType, eventData)
-    scene_:GetComponent("PhysicsWorld2D"):DrawDebugGeometry()
-end

+ 13 - 7
Bin/Data/Scripts/27_Urho2DPhysics.as

@@ -53,6 +53,9 @@ void CreateScene()
     // Create 2D physics world component
     scene_.CreateComponent("PhysicsWorld2D");
     
+    Sprite2D@ boxSprite = cache.GetResource("Sprite2D", "Urho2D/Box.png");
+    Sprite2D@ ballSprite = cache.GetResource("Sprite2D", "Urho2D/Ball.png");
+
     // Create ground.
     Node@ groundNode = scene_.CreateChild("Ground");
     groundNode.position = Vector3(0.0f, -3.0f, 0.0f);
@@ -61,10 +64,13 @@ void CreateScene()
     // Create 2D rigid body for gound
     RigidBody2D@ groundBody = groundNode.CreateComponent("RigidBody2D");
     
+    StaticSprite2D@ groundSprite = groundNode.CreateComponent("StaticSprite2D");
+    groundSprite.sprite = boxSprite;
+
     // Create box collider for ground
     CollisionBox2D@ groundShape = groundNode.CreateComponent("CollisionBox2D");
     // Set box size
-    groundShape.size = Vector2(0.1f, 0.1f);
+    groundShape.size = Vector2(0.32f, 0.32f);
     // Set friction
     groundShape.friction = 0.5f;
 
@@ -78,8 +84,12 @@ void CreateScene()
         RigidBody2D@ body = node.CreateComponent("RigidBody2D");
         body.bodyType = BT_DYNAMIC;
 
+        StaticSprite2D@ staticSprite = node.CreateComponent("StaticSprite2D");
+
         if (i % 2 == 0)
         {
+            staticSprite.sprite = boxSprite;
+
             // Create box
             CollisionBox2D@ box = node.CreateComponent("CollisionBox2D");
             // Set size
@@ -93,6 +103,8 @@ void CreateScene()
         }
         else
         {
+            staticSprite.sprite = ballSprite;
+
             // Create circle
             CollisionCircle2D@ circle = node.CreateComponent("CollisionCircle2D");
             // Set radius
@@ -167,7 +179,6 @@ void SubscribeToEvents()
 {
     // Subscribe HandleUpdate() function for processing update events
     SubscribeToEvent("Update", "HandleUpdate");
-    SubscribeToEvent("ScenePostUpdate", "HandleScenePostUpdate");
 }
 
 void HandleUpdate(StringHash eventType, VariantMap& eventData)
@@ -179,8 +190,3 @@ void HandleUpdate(StringHash eventType, VariantMap& eventData)
     MoveCamera(timeStep);
 }
 
-
-void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
-{
-    scene_.physicsWorld2D.DrawDebugGeometry();
-}

二进制
Bin/Data/Urho2D/Ball.png


二进制
Bin/Data/Urho2D/Box.png


+ 17 - 10
Source/Samples/27_Urho2DPhysics/Urho2DPhysics.cpp

@@ -33,9 +33,12 @@
 #include "Octree.h"
 #include "PhysicsWorld2D.h"
 #include "Renderer.h"
+#include "ResourceCache.h"
 #include "RigidBody2D.h"
 #include "Scene.h"
 #include "SceneEvents.h"
+#include "Sprite2D.h"
+#include "StaticSprite2D.h"
 #include "Text.h"
 #include "Urho2DPhysics.h"
 
@@ -88,6 +91,10 @@ void Urho2DPhysics::CreateScene()
     // Create 2D physics world component
     PhysicsWorld2D* physicsWorld = scene_->CreateComponent<PhysicsWorld2D>();
     
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    Sprite2D* boxSprite = cache->GetResource<Sprite2D>("Urho2D/Box.png");
+    Sprite2D* ballSprite = cache->GetResource<Sprite2D>("Urho2D/Ball.png");
+
     // Create ground.
     Node* groundNode = scene_->CreateChild("Ground");
     groundNode->SetPosition(Vector3(0.0f, -3.0f, 0.0f));
@@ -96,10 +103,13 @@ void Urho2DPhysics::CreateScene()
     // Create 2D rigid body for gound
     RigidBody2D* groundBody = groundNode->CreateComponent<RigidBody2D>();
     
+    StaticSprite2D* groundSprite = groundNode->CreateComponent<StaticSprite2D>();
+    groundSprite->SetSprite(boxSprite);
+
     // Create box collider for ground
     CollisionBox2D* groundShape = groundNode->CreateComponent<CollisionBox2D>();
     // Set box size
-    groundShape->SetSize(Vector2(0.1f, 0.1f));
+    groundShape->SetSize(Vector2(0.32f, 0.32f));
     // Set friction
     groundShape->SetFriction(0.5f);
 
@@ -112,8 +122,12 @@ void Urho2DPhysics::CreateScene()
         RigidBody2D* body = node->CreateComponent<RigidBody2D>();
         body->SetBodyType(BT_DYNAMIC);
 
+        StaticSprite2D* staticSprite = node->CreateComponent<StaticSprite2D>();
+
         if (i % 2 == 0)
         {
+            staticSprite->SetSprite(boxSprite);
+
             // Create box
             CollisionBox2D* box = node->CreateComponent<CollisionBox2D>();
             // Set size
@@ -127,6 +141,8 @@ void Urho2DPhysics::CreateScene()
         }
         else
         {
+            staticSprite->SetSprite(ballSprite);
+
             // Create circle
             CollisionCircle2D* circle = node->CreateComponent<CollisionCircle2D>();
             // Set radius
@@ -206,8 +222,6 @@ void Urho2DPhysics::SubscribeToEvents()
 {
     // Subscribe HandleUpdate() function for processing update events
     SubscribeToEvent(E_UPDATE, HANDLER(Urho2DPhysics, HandleUpdate));
-
-    SubscribeToEvent(E_SCENEPOSTUPDATE, HANDLER(Urho2DPhysics, HandleScenePostUpdate));
 }
 
 void Urho2DPhysics::HandleUpdate(StringHash eventType, VariantMap& eventData)
@@ -220,10 +234,3 @@ void Urho2DPhysics::HandleUpdate(StringHash eventType, VariantMap& eventData)
     // Move the camera, scale movement with time step
     MoveCamera(timeStep);
 }
-
-void Urho2DPhysics::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
-{
-    PhysicsWorld2D* physicWorld = scene_->GetComponent<PhysicsWorld2D>();
-    physicWorld->DrawDebugGeometry();
-}
-

+ 0 - 2
Source/Samples/27_Urho2DPhysics/Urho2DPhysics.h

@@ -58,8 +58,6 @@ private:
     void SubscribeToEvents();
     /// Handle the logic update event.
     void HandleUpdate(StringHash eventType, VariantMap& eventData);
-    /// Handle scene post update event.
-    void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
 
     /// Scene.
     SharedPtr<Scene> scene_;