Can I use physics to do this?

Hello,

I was wondering if it is possible to make a door handle like object and be able to use physics to push it down. Like this:

luxurious-office-door-handles-ZnqVk.jpg

So when you tap the handle, it goes down, and when you lift up your finger, it goes back up.

I’m pretty sure this is possible, but potentially not needed. A sprite animation or a transition.to() transitioning rotation would probably take care of it.

A simple example:

 local physics = require "physics" physics.setDrawMode( "hybrid" ) physics.start() physics.setGravity(0, 0) local ball = display.newCircle(100, 240, 5) local base = display.newCircle(200, 200, 50) local handle = display.newRect(base.x, 200, 200, 40) handle:setFillColor(0.5) handle.anchorX = 0.9 physics.addBody(ball, "dynamic") physics.addBody(handle, "dynamic") physics.addBody(base, "static" ) base.isSensor = true local joint = physics.newJoint( "pivot", handle, base, base.x, base.y) joint:setRotationLimits( 0, 90 ) joint.isLimitEnabled = true local function handleTouch(event) if event.phase == "began" then display.getCurrentStage():setFocus(handle) handle:applyAngularImpulse( -40 ) elseif event.phase == "ended" then transition.to(handle, {rotation = 0, time = 1000}) handle:applyAngularImpulse( 40 ) end end handle:addEventListener("touch", handleTouch)

But as Rob says, physics might not be needed, in which case it becomes simpler. To turn the above into a physics-less version, all you have to do is remove all the physics-related bits and use transition.to instead of applying an angular impulse.

Thanks Rob and hasty. I got it working now.

I’m pretty sure this is possible, but potentially not needed. A sprite animation or a transition.to() transitioning rotation would probably take care of it.

A simple example:

 local physics = require "physics" physics.setDrawMode( "hybrid" ) physics.start() physics.setGravity(0, 0) local ball = display.newCircle(100, 240, 5) local base = display.newCircle(200, 200, 50) local handle = display.newRect(base.x, 200, 200, 40) handle:setFillColor(0.5) handle.anchorX = 0.9 physics.addBody(ball, "dynamic") physics.addBody(handle, "dynamic") physics.addBody(base, "static" ) base.isSensor = true local joint = physics.newJoint( "pivot", handle, base, base.x, base.y) joint:setRotationLimits( 0, 90 ) joint.isLimitEnabled = true local function handleTouch(event) if event.phase == "began" then display.getCurrentStage():setFocus(handle) handle:applyAngularImpulse( -40 ) elseif event.phase == "ended" then transition.to(handle, {rotation = 0, time = 1000}) handle:applyAngularImpulse( 40 ) end end handle:addEventListener("touch", handleTouch)

But as Rob says, physics might not be needed, in which case it becomes simpler. To turn the above into a physics-less version, all you have to do is remove all the physics-related bits and use transition.to instead of applying an angular impulse.

Thanks Rob and hasty. I got it working now.