Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Setting Anchor Point to Coordinate — Gideros Forum

Setting Anchor Point to Coordinate

SeebalSeebal Member
edited August 2013 in General questions
Hi,

I am trying to set the anchor point of a bitmap to a point that is beyond the object, and located inside another bitmap. The current method that I am trying to do right now is to find the difference between their x and y positions and then dividing it by the first bitmap's widths and heights to find the exact anchor point relative to said bitmap. This method however, does not work and I was wondering if there is a way to set the anchor point to a coordinate or if someone knows an algorithm that converts a coordinate pair (x,y) to an anchor point of a bitmap.

For example, if the bitmap was at (25, 25), and the anchorpoint we want it to be at is (65, 65), what would we need Bitmap:setAnchorPoint(a, b) to be? And by changing the anchorpoint we would also change the position so the first bitmap would no longer be at (25, 25), right?! Hopefully this is not too confusing and someone on here can help me out! Thanks.

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited August 2013
    hmm, I'm really not sure that Bitmap can have anchorpoint outside it's own image, let me check
    EDIT:
    Wow it seems it actually can.
  • ar2rsawseenar2rsawseen Maintainer
    edited August 2013
    So solution to your problem.
    if your bitmap is at (25,25) like with (0,0) anchor point, and you want the anchor point to be at (65, 65) coordinate, it means you want to move anchor point by (40,40) pixels. And we need to calculate how much in precentage 40 pixels is from the height and width of your bitmap
    So something like this should work:
    function Bitmap:setAnchorPointTo(x, y)
        local curX, curY = self:getPosition()
        local diffX = curX - x
        local diffY = curY - y
        self:setAnchorPoint(diffX/self:getWidth(), diffY/self:getHeight())
    end
    Hmm but that actually does not make any sense and yes it will change the position of the bitmap.
    Please tell more what exactly do you want to achieve.

    The code I provided is more meant like if you want to set the coordinate of Bitmap to (25, 25) but want it to appear at (65, 65) coordinate.
  • @ar2rsawseen I am trying to keep the position of the bitmap at (25,25), but make it rotate around (65,65). The only way I am aware of rotating a bitmap around is by using the anchor point, and thus this post was born. Thanks for the help so far.
  • petecpetec Member
    Accepted Answer
    @Seebal I've just tried doing it a different way by adding a sprite at (65,65) and then adding the bitmap as a child of the sprite and positioning it at (-40,-40) so that it is at (25,25) on the stage. Then rotating the sprite causes the bitmap to rotate around (65,65). Is that what you are trying to achieve? Here's what I did:
    local centreRot=Sprite.new()
    centreRot:setPosition(65,65)
    stage:addChild(centreRot)
     
    local bitmap=Bitmap.new(Texture.new("bitmap.png",true))
    bitmap:setPosition(-40,-40)
    centreRot:addChild(bitmap)
     
    for n=1,36 do
    	Timer.delayedCall(200*n,function()
    		centreRot:setRotation(centreRot:getRotation()+10)
    	end)
    end
    +1 -1 (+2 / -0 )Share on Facebook
  • @petec Wow! That makes so much more sense than messing around with the anchor points... Thanks for the help!
  • @Seebal Glad it helped.
Sign In or Register to comment.