I want to have doors in my game which slide on a timer, user tap or whatever. Either way, they need to be physics objects and they need to open and close via sliding. Obviously, I can’t just translate their X and Y values (because that would be fighting the physics engine) but I’m not sure how to get them to move otherwise.
My initial investigation (proof) uses the touch joint and piston joint method to control the axis and range of motion of the door, with a touch joint being used not by the user but by an internal mechanism.
I’d really like to get other developer’s input and ideas on this, please?..
In my proof code, below, I’m using either a tap or touch on the device to set the touch joint position. Before that happens the “door” (blue square) falls with gravity (initially set towards the left) but after that the door is held in place by the touch joint…
[lua]local physics = require(“physics”)
– turn status bar off
display.setStatusBar( display.HiddenStatusBar )
math.randomseed( os.time() )
– start physics
physics.start()
physics.setGravity( 0, 0 )
physics.setDrawMode( “normal” ) – normal, hybrid, debug
local defaultrectbody = { friction=0, bounce=0, density=0 }
local defaultroundbody = { friction=0, bounce=0, density=0, radius=5 }
local anchor = display.newCircle( 20, 20, 5 )
physics.addBody( anchor, “static”, defaultroundbody )
local white = display.newRect( 100, 100, 300, 100 )
physics.addBody( white, “dynamic”, defaultbody )
local weld = physics.newJoint( “weld”, anchor, white, anchor.x, anchor.y )
local blue = display.newRect( 100, 100, 100, 100 )
blue:setFillColor( 0, 0, 255 )
physics.addBody( blue, “dynamic”, defaultbody )
local piston = physics.newJoint( “piston”, white, blue, white.x,white.y, 10, 0 )
piston.isLimitEnabled = true
piston:setLimits( 0, 200 )
local touch = display.newCircle( 150, 150, 20 )
touch:setFillColor( 255, 0, 0 )
function createTouchJoint()
if (not touch.joint) then
touch.joint = physics.newJoint( “touch”, blue, blue.x, blue.y )
end
end
function tap( event )
createTouchJoint()
touch.x, touch.y = event.x, event.y
touch.joint:setTarget( touch.x, touch.y )
return true
end
Runtime:addEventListener( “tap”, tap )
function drag( event )
createTouchJoint()
touch.x, touch.y = event.x, event.y
touch.joint:setTarget( touch.x, touch.y )
return true
end
Runtime:addEventListener( “touch”, drag )[/lua]
[import]uid: 8271 topic_id: 19786 reply_id: 319786[/import]