Forked from JesterXL/ropeJointvsDistanceJoint.lua
Last active
August 29, 2015 14:23
-
-
Save galahad9/68b588f88c7a76710932 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "physics" | |
display.setStatusBar( display.HiddenStatusBar ) | |
physics.setDrawMode("hybrid") | |
physics.start() | |
physics.setGravity(0, 9.8) | |
physics.setPositionIterations( 10 ) | |
function getGrapplePoint() | |
local point = display.newRect(0, 0, 10, 10) | |
physics.addBody(point,"static", {density = 1, friction = .5, bounce = .1}) | |
return point | |
end | |
function getMetalSphere(r) | |
local sphere = display.newCircle(0, 0, r) | |
physics.addBody(sphere, "dynamic", {density=3, radius=r, bounce=0.0, friction=0.7}) | |
return sphere | |
end | |
local function testingRope2() | |
local point1 = getGrapplePoint() | |
point1.x = 100 | |
point1.y = 100 | |
local point2 = getGrapplePoint() | |
point2.x = 300 | |
point2.y = 200 | |
local sphere = getMetalSphere(20) | |
sphere.x = 0 | |
sphere.y = 200 | |
local mappedX, mappedY = sphere:contentToLocal(sphere.x + 2, sphere.y - sphere.height / 2) | |
local rope1 = physics.newJoint("rope", sphere, point1, mappedX, mappedY) | |
-- now watch the bounce (elsaticity more accurate) of a distance joint | |
local sphere2 = getMetalSphere(20) | |
sphere2.x = 200 | |
sphere2.y = 300 | |
local distance = physics.newJoint("distance", sphere2, point2, sphere2.x, sphere2.y, point2.x, point2.y) | |
distance.length = 200 | |
distance.frequency = 0.7 | |
distance.dampingRatio = 0 | |
-- finally, MOVE 'EM UP! | |
local t = {} | |
t.speed = 5 | |
function t:timer() | |
if rope1.maxLength > 20 then | |
rope1.maxLength = rope1.maxLength - self.speed | |
end | |
if distance.length > 20 then | |
distance.length = distance.length - self.speed | |
end | |
end | |
-- t.id = timer.performWithDelay(10, t, 0) | |
local function startDrag( event ) | |
local t = event.target | |
local phase = event.phase | |
if "began" == phase then | |
display.getCurrentStage():setFocus( t ) | |
t.isFocus = true | |
-- Store initial position | |
t.x0 = event.x - t.x | |
t.y0 = event.y - t.y | |
-- Make body type temporarily "kinematic" (to avoid gravitional forces) | |
event.target.bodyType = "kinematic" | |
-- Stop current motion, if any | |
event.target:setLinearVelocity( 0, 0 ) | |
event.target.angularVelocity = 0 | |
elseif t.isFocus then | |
if "moved" == phase then | |
t.x = event.x - t.x0 | |
t.y = event.y - t.y0 | |
elseif "ended" == phase or "cancelled" == phase then | |
display.getCurrentStage():setFocus( nil ) | |
t.isFocus = false | |
t.bodyType = "dynamic" | |
end | |
end | |
-- Stop further propagation of touch event! | |
return true | |
end | |
sphere:addEventListener("touch", startDrag) | |
sphere2:addEventListener("touch", startDrag) | |
end | |
testingRope2() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment