How can I make the object rotate around an anchor point?

Hi.

How can I make the object rotate around an anchor point?

 The thing is I want to rotate the object around a specific point. Not around the object itself.

I tried something like the following code. But, the movement is small. I want the object to rotate making a bigger circle .

[lua]

local circle = display.newCircle( 160, 240, 10 )
circle.anchorX = -160; circle.anchorY = 2040;
circle.x = 160;

function move()
circle.rotation = circle.rotation +7
end

Runtime:addEventListener( “enterFrame”, move )

[/lua]

I want the object(circle) to rotate making a bigger circle instead of the small one it’s making currently.

thanks.

What version of Corona are you using?  i.e. I am currently running 2016.2841

I seem to recall that anchor points beyond [-1,1] are supported, but if you’re using an older version of Corona it may not support the feature yet.

https://coronalabs.com/blog/2015/10/20/tutorial-extending-anchor-points/

Regardless, that is not how I would approach rotating an object about an arbitray point.

I have answered a question similar to this before.  Please download this:

http://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/transitions.zip

Then, look at example ~/5-askEd/rotateAboutPoint2

** UPDATE ** This part of my archive of answers seems to be corrupted/misordered?  If you can’t find the code let me know Also you can read the key parts here:

http://github.com/roaminggamer/CoronaGeek/tree/master/Hangouts/transitions/5%20-%20askEd/rotateAboutPoint2

It won’t necessarily match your needs exactly, but you can modify it to suit your own needs.

local function vecScale( x, y, s ) return x \*s, y \* s end local function angle2Vec( angle ) local screenAngle = math.rad( -(angle+90) ) return -math.cos( screenAngle ), math.sin( screenAngle ) end local function rotateAbout( obj, x, y, params ) x = x or display.contentCenterX y = y or display.contentCenterY params = params or {} local radius = params.radius or 50 obj.\_pathRot = params.startA or 0 local endA = params.endA or (obj.\_pathRot + 360 ) local time = params.time or 1000 local myEasing = params.myEasing or easing.linear local debugEn = params.debugEn local delay = params.delay local onComplete = params.onComplete -- Start at right position local vx,vy = angle2Vec( obj.\_pathRot ) vx,vy = vecScale( vx, vy, radius ) obj.x = x + vx obj.y = y + vy -- remove 'enterFrame' listener when we finish the transition. obj.onComplete = function( self ) Runtime:removeEventListener( "enterFrame", self ) if( onComplete ) then onComplete( self ) end end -- Update position every frame obj.enterFrame = function ( self ) local vx,vy = angle2Vec( self.\_pathRot ) vx,vy = vecScale( vx, vy, radius ) self.x = x + vx self.y = y + vy if( debugEn ) then local tmp = display.newCircle( self.parent, self.x, self.y, 1 ) tmp:toBack() end end Runtime:addEventListener( "enterFrame", obj ) -- Use transition to change the angle (gives us access to nice effects) transition.to( obj, { \_pathRot = endA, delay = delay, time = time, transition = myEasing, onComplete = obj } ) end return rotateAbout

If you want the object to rotate around another object why not use a distance joint?

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

@

roaminggamer

Thanks a lot. That helped a lot.

@

InfiSnyp

I’m trying to avoid physics as much as I can :wink:

What version of Corona are you using?  i.e. I am currently running 2016.2841

I seem to recall that anchor points beyond [-1,1] are supported, but if you’re using an older version of Corona it may not support the feature yet.

https://coronalabs.com/blog/2015/10/20/tutorial-extending-anchor-points/

Regardless, that is not how I would approach rotating an object about an arbitray point.

I have answered a question similar to this before.  Please download this:

http://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/transitions.zip

Then, look at example ~/5-askEd/rotateAboutPoint2

** UPDATE ** This part of my archive of answers seems to be corrupted/misordered?  If you can’t find the code let me know Also you can read the key parts here:

http://github.com/roaminggamer/CoronaGeek/tree/master/Hangouts/transitions/5%20-%20askEd/rotateAboutPoint2

It won’t necessarily match your needs exactly, but you can modify it to suit your own needs.

local function vecScale( x, y, s ) return x \*s, y \* s end local function angle2Vec( angle ) local screenAngle = math.rad( -(angle+90) ) return -math.cos( screenAngle ), math.sin( screenAngle ) end local function rotateAbout( obj, x, y, params ) x = x or display.contentCenterX y = y or display.contentCenterY params = params or {} local radius = params.radius or 50 obj.\_pathRot = params.startA or 0 local endA = params.endA or (obj.\_pathRot + 360 ) local time = params.time or 1000 local myEasing = params.myEasing or easing.linear local debugEn = params.debugEn local delay = params.delay local onComplete = params.onComplete -- Start at right position local vx,vy = angle2Vec( obj.\_pathRot ) vx,vy = vecScale( vx, vy, radius ) obj.x = x + vx obj.y = y + vy -- remove 'enterFrame' listener when we finish the transition. obj.onComplete = function( self ) Runtime:removeEventListener( "enterFrame", self ) if( onComplete ) then onComplete( self ) end end -- Update position every frame obj.enterFrame = function ( self ) local vx,vy = angle2Vec( self.\_pathRot ) vx,vy = vecScale( vx, vy, radius ) self.x = x + vx self.y = y + vy if( debugEn ) then local tmp = display.newCircle( self.parent, self.x, self.y, 1 ) tmp:toBack() end end Runtime:addEventListener( "enterFrame", obj ) -- Use transition to change the angle (gives us access to nice effects) transition.to( obj, { \_pathRot = endA, delay = delay, time = time, transition = myEasing, onComplete = obj } ) end return rotateAbout

If you want the object to rotate around another object why not use a distance joint?

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

@

roaminggamer

Thanks a lot. That helped a lot.

@

InfiSnyp

I’m trying to avoid physics as much as I can :wink: