Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Urho2D physics Constraints sample.
- -- This sample is designed to help understanding and chosing the right constraint.
- -- This sample demonstrates:
- -- - Creating physics constraints
- -- - Creating Edge and Polygon Shapes from vertices
- -- - Displaying physics debug geometry and constraints' joints
- -- - Using SetOrderInLayer to alter the way sprites are drawn in relation to each other
- -- - Using Text3D to display some text affected by zoom
- -- - Setting the background color for the scene
- require "LuaScripts/Utilities/Sample"
- local camera = nil
- function Start()
- SampleStart()
- CreateScene()
- input.mouseVisible = true -- Show mouse cursor
- CreateInstructions()
- -- Set the mouse mode to use in the sample
- SampleInitMouseMode(MM_FREE)
- SubscribeToEvents()
- print(PIXEL_SIZE)
- end
- function CreateLetterSprite(x,y,z,c,color,size)
- local node = scene_:CreateChild("Background")
- local t=node:CreateComponent("Text3D")
- t.text=c
- t.font=cache:GetResource("Font", "Fonts/BlueHighway.ttf")
- t.fontSize=size
- t.textEffect=TE_STROKE
- t.color=color
- t.horizontalAlignment=HA_CENTER
- t.verticalAlignment=VA_CENTER
- t.effectColor=Color(0,0,0)
- node.position=Vector3(x,y,z)
- return node
- end
- function CreateScene()
- scene_ = Scene()
- scene_:CreateComponent("Octree")
- scene_:CreateComponent("DebugRenderer")
- -- Create camera
- cameraNode = scene_:CreateChild("Camera")
- cameraNode.position = Vector3(0, 0, 0) -- Note that Z setting is discarded; use camera.zoom instead (see MoveCamera() below for example)
- camera = cameraNode:CreateComponent("Camera")
- camera.orthographic = true
- camera.orthoSize = graphics.height * 0.01
- camera.zoom = 1 -- Set zoom according to user's resolution to ensure full visibility (initial zoom (1.2) is set for full visibility at 1280x800 resolution)
- renderer:SetViewport(0, Viewport:new(scene_, camera))
- renderer.defaultZone.fogColor = Color(0.1, 0.1, 0.1) -- Set background color for the scene
- local x,y
- for x=0,10,1 do
- for y=0,10,1 do
- local n=CreateLetterSprite(x*0.8,y*0.8,1, '#', Color(0.5,0.5,0.5), 100)
- end
- end
- dude=CreateLetterSprite(5*0.5, 5*0.8, 0.5, '@', Color(0.5,0.5,1), 100)
- end
- function CreateInstructions()
- -- Construct new Text object, set string to display and font to use
- local instructionText = ui.root:CreateChild("Text")
- instructionText:SetText("Use WASD keys and mouse to move, Use PageUp PageDown to zoom.\n Space to toggle debug geometry and joints - F5 to save the scene.")
- instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
- instructionText.textAlignment = HA_CENTER -- Center rows in relation to each other
- -- Position the text relative to the screen center
- instructionText.horizontalAlignment = HA_CENTER
- instructionText.verticalAlignment = VA_CENTER
- instructionText:SetPosition(0, ui.root.height / 4)
- end
- function MoveCamera(timeStep)
- if ui.focusElement ~= nil then return end -- Do not move if the UI has a focused element (the console)
- local MOVE_SPEED = 4 -- Movement speed as world units per second
- -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
- if input:GetKeyDown(KEY_W) then cameraNode:Translate(Vector3(0, 1, 0) * MOVE_SPEED * timeStep) end
- if input:GetKeyDown(KEY_S) then cameraNode:Translate(Vector3(0, -1, 0) * MOVE_SPEED * timeStep) end
- if input:GetKeyDown(KEY_A) then cameraNode:Translate(Vector3(-1, 0, 0) * MOVE_SPEED * timeStep) end
- if input:GetKeyDown(KEY_D) then cameraNode:Translate(Vector3(1, 0, 0) * MOVE_SPEED * timeStep) end
- if input:GetKeyDown(KEY_PAGEUP) then camera.zoom = camera.zoom * 1.01 end -- Zoom In
- if input:GetKeyDown(KEY_PAGEDOWN) then camera.zoom = camera.zoom * 0.99 end -- Zoom Out
- end
- function SubscribeToEvents()
- SubscribeToEvent("Update", "HandleUpdate")
- SubscribeToEvent("PostRenderUpdate", "HandlePostRenderUpdate")
- SubscribeToEvent("MouseButtonDown", "HandleMouseButtonDown")
- if touchEnabled then SubscribeToEvent("TouchBegin", "HandleTouchBegin3") end
- -- Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
- UnsubscribeFromEvent("SceneUpdate")
- end
- function HandleUpdate(eventType, eventData)
- local timestep = eventData["TimeStep"]:GetFloat()
- MoveCamera(timestep) -- Move the camera according to frame's time step
- if input:GetKeyPress(KEY_SPACE) then drawDebug = not drawDebug end -- Toggle debug geometry with space
- if input:GetKeyPress(KEY_F5) then scene_:SaveXML(fileSystem:GetProgramDir().."Data/Scenes/Constraints.xml") end -- Save scene
- local campos=cameraNode.position
- dude.position=Vector3(campos.x, campos.y, dude.position.z)
- end
- function HandlePostRenderUpdate(eventType, eventData)
- if drawDebug then physicsWorld:DrawDebugGeometry() end
- end
- function HandleMouseButtonDown(eventType, eventData)
- local rigidBody = physicsWorld:GetRigidBody(input.mousePosition.x, input.mousePosition.y) -- Raycast for RigidBody2Ds to pick
- if rigidBody ~= nil then
- pickedNode = rigidBody.node
- local staticSprite = pickedNode:GetComponent("StaticSprite2D")
- staticSprite.color = Color(1, 0, 0, 1) -- Temporary modify color of the picked sprite
- -- ConstraintMouse2D - Temporary apply this constraint to the pickedNode to allow grasping and moving with the mouse
- local constraintMouse = pickedNode:CreateComponent("ConstraintMouse2D")
- constraintMouse.target = GetMousePositionXY()
- constraintMouse.maxForce = 1000 * rigidBody.mass
- constraintMouse.collideConnected = true
- constraintMouse.otherBody = dummyBody -- Use dummy body instead of rigidBody. It's better to create a dummy body automatically in ConstraintMouse2D
- end
- SubscribeToEvent("MouseMove", "HandleMouseMove")
- SubscribeToEvent("MouseButtonUp", "HandleMouseButtonUp")
- end
- function HandleMouseButtonUp(eventType, eventData)
- if pickedNode ~= nil then
- local staticSprite = pickedNode:GetComponent("StaticSprite2D")
- staticSprite.color = Color(1, 1, 1, 1) -- Restore picked sprite color
- pickedNode:RemoveComponent("ConstraintMouse2D") -- Remove temporary constraint
- pickedNode = nil
- end
- UnsubscribeFromEvent("MouseMove")
- UnsubscribeFromEvent("MouseButtonUp")
- end
- function GetMousePositionXY()
- local screenPoint = Vector3(input.mousePosition.x / graphics.width, input.mousePosition.y / graphics.height, 0)
- local worldPoint = camera:ScreenToWorldPoint(screenPoint)
- return Vector2(worldPoint.x, worldPoint.y)
- end
- function HandleMouseMove(eventType, eventData)
- if pickedNode ~= nil then
- local constraintMouse = pickedNode:GetComponent("ConstraintMouse2D")
- constraintMouse.target = GetMousePositionXY()
- end
- end
- function HandleTouchBegin3(eventType, eventData)
- local rigidBody = physicsWorld:GetRigidBody(eventData["X"]:GetInt(), eventData["Y"]:GetInt()) -- Raycast for RigidBody2Ds to pick
- if rigidBody ~= nil then
- pickedNode = rigidBody.node
- local staticSprite = pickedNode:GetComponent("StaticSprite2D")
- staticSprite.color = Color(1, 0, 0, 1) -- Temporary modify color of the picked sprite
- local rigidBody = pickedNode:GetComponent("RigidBody2D")
- -- ConstraintMouse2D - Temporary apply this constraint to the pickedNode to allow grasping and moving with touch
- local constraintMouse = pickedNode:CreateComponent("ConstraintMouse2D")
- constraintMouse.target = camera:ScreenToWorldPoint(Vector3(eventData["X"]:GetInt() / graphics.width, eventData["Y"]:GetInt() / graphics.height, 0))
- constraintMouse.maxForce = 1000 * rigidBody.mass
- constraintMouse.collideConnected = true
- constraintMouse.otherBody = dummyBody -- Use dummy body instead of rigidBody. It's better to create a dummy body automatically in ConstraintMouse2D
- constraintMouse.dampingRatio = 0
- end
- SubscribeToEvent("TouchMove", "HandleTouchMove3")
- SubscribeToEvent("TouchEnd", "HandleTouchEnd3")
- end
- function HandleTouchMove3(eventType, eventData)
- if pickedNode ~= nil then
- local constraintMouse = pickedNode:GetComponent("ConstraintMouse2D")
- constraintMouse.target = camera:ScreenToWorldPoint(Vector3(eventData["X"]:GetInt() / graphics.width, eventData["Y"]:GetInt() / graphics.height, 0))
- end
- end
- function HandleTouchEnd3(eventType, eventData)
- if pickedNode ~= nil then
- local staticSprite = pickedNode:GetComponent("StaticSprite2D")
- staticSprite.color = Color(1, 1, 1, 1) -- Restore picked sprite color
- pickedNode:RemoveComponent("ConstraintMouse2D") -- Remove temporary constraint
- pickedNode = nil
- end
- UnsubscribeFromEvent("TouchMove")
- UnsubscribeFromEvent("TouchEnd")
- end
- -- Create XML patch instructions for screen joystick layout specific to this sample app
- function GetScreenJoystickPatchString()
- return
- "<patch>" ..
- " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" ..
- " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" ..
- " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" ..
- " <element type=\"Text\">" ..
- " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
- " <attribute name=\"Text\" value=\"PAGEUP\" />" ..
- " </element>" ..
- " </add>" ..
- " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" ..
- " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" ..
- " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" ..
- " <element type=\"Text\">" ..
- " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
- " <attribute name=\"Text\" value=\"PAGEDOWN\" />" ..
- " </element>" ..
- " </add>" ..
- "</patch>"
- end
Advertisement
Add Comment
Please, Sign In to add comment