I was following the guide on how to mimic radial gravity using touch joints found here
https://coronalabs.com/blog/2013/04/09/physics-radial-gravity-and-predicting-trajectory/
I want the object within the raidal field to be attracted to the centre, however I cant quite get it working. (probably because I have no idea what i’m doing…) here is the code I am using
local field = display.newCircle( 0, 0, 100) ; field.alpha = 0.3 field.name = "field" field.x = display.contentCenterX ; field.y = display.contentCenterY physics.addBody( field, "static", { isSensor=true, radius=100 } ) local eggplant = display.newImageRect( scene.perRunGroup, "shapes/monster.png", 45, 45, display.contentCenterX, display.contentCenterY, 30) eggplant.alpha = 1 eggplant.name = eggplant physics.addBody(eggplant, "dynamic", {radius = 20, friction = .4, density = 0.00001, bounce = 0}) eggplant.x = 40 eggplant.y = 0 eggplant.rotation = 0 eggplant.collisionType = "eggplant" function eggplantCollide( self, event ) local otherName = event.other.name local function onDelay( event ) local action = "" if ( action == "makeJoint" ) then self.hasJoint = true self.touchJoint = physics.newJoint( "touch", self, self.x, self.y ) self.touchJoint.frequency = 20 self.touchJoint.dampingRatio = 0.0 self.touchJoint:setTarget( event.other.x, event.other.y ) end end if ( event.phase == "began" and otherName == "field" and self.hasJoint == false ) then local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "makeJoint" end end
I basically want the eggplant to be attracted the center of the field and the have the touch joint removed after it has left the field.