Swipe object on a perfect axis?

Hello All

Im currently trying to code a distance joint where the object can be swiped around the centre ring without any elasticity. At the moment it seems to not stay within the Circle and is prone to being dragged outside of it where it then bounces back if I let go.

Is there a way to fix its boundary exactly on the circle and so it can also be swiped within the circle only? The other issue I’m facing is constant freezing when dragging , i.e. doesn’t move with my finger unless I tap again, then again I’ve only tested this on a PC. Heres my code, am I going about it the right way?

local physics = require(“physics”)
physics.start()
physics.setGravity( 0,0 )

local staticBox = display.newCircle(display.contentCenterX,display.contentCenterY,150,150)
staticBox:setFillColor(0.2,0.2,1)
staticBox.strokeWidth = 10
physics.addBody(staticBox ,“static”)

local shape = display.newRect(20,20,50,50)
shape:setFillColor(1,0.2,0.4)
physics.addBody(shape,“dynamic”)

joint = physics.newJoint(“distance”,staticBox ,shape,staticBox.x,staticBox.y,shape.x,shape.y)
joint.dampingRatio = 0
joint.length = 150

– touch listener function
function shape:touch( event )
if event.phase == “began” then
self.markX = self.x – store x location of object
self.markY = self.y – store y location of object
elseif event.phase == “moved” then
local x = (event.x - event.xStart) + self.markX
local y = (event.y - event.yStart) + self.markY
self.x, self.y = x, y – move object based on calculations above
end
return true
end
shape:addEventListener( “touch”, myObject )
 

Hi @never3den,

Please see the following guide on physics joints. You should set the “dampingRatio” to 1 for critical damping, and also set “frequency” to some absurdly high value like 1000000 to make sure the joint isn’t soft in any way.

http://docs.coronalabs.com/guide/physics/physicsJoints/index.html#distance

Take care,

Brent

Thank you Brent. It’s still bouncing back but I might try and approach it differently , i.e. like a wheel on an axis

Hi @never3den,

Happy to help. Yes, I suppose it would be possible to force-override a distance joint to exceed its length. The wheel concept is probably better for this, and you could just (visually) represent one side/portion of the rotating object to make it appear like it’s swinging around a central axis, but of course the physics system would be treating it just like a wheel.

Brent

Yep, looks like thats the way to go ! Managed to get a wheel done where I swipe on any part of the screen to rotate and drew the object on Paint rather than render it using the SDK.

Hi @never3den,

Please see the following guide on physics joints. You should set the “dampingRatio” to 1 for critical damping, and also set “frequency” to some absurdly high value like 1000000 to make sure the joint isn’t soft in any way.

http://docs.coronalabs.com/guide/physics/physicsJoints/index.html#distance

Take care,

Brent

Thank you Brent. It’s still bouncing back but I might try and approach it differently , i.e. like a wheel on an axis

Hi @never3den,

Happy to help. Yes, I suppose it would be possible to force-override a distance joint to exceed its length. The wheel concept is probably better for this, and you could just (visually) represent one side/portion of the rotating object to make it appear like it’s swinging around a central axis, but of course the physics system would be treating it just like a wheel.

Brent

Yep, looks like thats the way to go ! Managed to get a wheel done where I swipe on any part of the screen to rotate and drew the object on Paint rather than render it using the SDK.