Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Sprite:setAnchorPoint / Sprite:getAnchorPoint? — Gideros Forum

Sprite:setAnchorPoint / Sprite:getAnchorPoint?

MellsMells Guru
edited July 2013 in Suggestions & requests
Hi,

===============================
UPDATE : Now available
Gideros Coding Easy is a lib that provides anchor points for Sprites but also other useful things.

At some point, this library will (or will not) be included in Gideros. I think it has not been clearly defined yet.
===============================

are there any reason why those methods are available for Bitmap and not directly from Sprite?
I can see many cases where it would be very useful.

So if there are no particular technical reasons that make it impossible to do, I would like to add my voice for this request.

Note : the last related discussion that I found happened in nov 2011 : link.
twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
+1 -1 (+2 / -0 )Share on Facebook

Comments

  • Seconded - anchoring a sprite group at a given location could be quite useful.

    However I think you could implement it yourself by overriding the Sprite:setPosition() method and checking the bounds and translating the position yourself with something like this.
    local oldSetPos = Sprite.setPosition
     
    Sprite:setAnchorPoint(ax,ay)
        self.ax = ax
        self.ay = ay
    end
     
    Sprite:setPosition(x,y)
        local newX = self:getX() + (self:getWidth() * self.ax)
        local newY = self:getY() + (self:getHeight() * self.ay)
        oldSetPos:(newX,newY)
    end

    Likes: jack0088

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
    +1 -1 (+1 / -0 )Share on Facebook
  • jack0088jack0088 Member
    edited July 2012
    Seconded - anchoring a sprite group at a given location could be quite useful.

    However I think you could implement it yourself by overriding the Sprite:setPosition() method and checking the bounds and translating the position yourself with something like this.
    local oldSetPos = Sprite.setPosition
     
    Sprite:setAnchorPoint(ax,ay)
        self.ax = ax
        self.ay = ay
    end
     
    Sprite:setPosition(x,y)
        local newX = self:getX() + (self:getWidth() * self.ax)
        local newY = self:getY() + (self:getHeight() * self.ay)
        oldSetPos:(newX,newY)
    end
    shouldn't it be like
    Sprite.setPosition = function(x, y)
        local newX = self:getX() - (self:getWidth()  * self.ax)
        local newY = self:getY() - (self:getHeight() * self.ay)
        oldSetPos(self, newX, newY)
    end
    ?

    PS: @atilim: you should hurry with the AnchorPoint for Sprites! :))
    Owltwins. Smart design and creative code.
    »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
  • I used @techdojo suggestion and have implemented it in my Primitives class like:
    function Primitive:init()
    	self.AnchorPoint = { x=0, y=0 }
     
    	local position = Sprite.setPosition --cache
     
    	Sprite.setPosition = function(self, x, y)
    		local ax, ay = self:getAnchorPoint()
    		local newX   = (self:getX() - self:getWidth()  * ax) + x
    		local newY   = (self:getY() - self:getHeight() * ay) + y
    		position(self, newX, newY)
    	end
    end
     
    function Primitive:setAnchorPoint(ax, ay)
    	self.AnchorPoint = { x=ax, y=ay }
    	self:setPosition(0, 0) --for update purposes
    end
     
    function Primitive:getAnchorPoint(ax, ay)
    	return self.AnchorPoint.x, self.AnchorPoint.y
    end
    It seems to work so far, BUT it doesn't null out the position of the Shape itself. Example to make it clear:
    local circle = Primitive.new()
    circle:setAnchorPoint(0.95, 0) --Anchor!
    circle:newCircle(0,0, 50)
    stage:addChild(circle)
     
    print(circle:getPosition()) -- (-95.95, 0), where it should be (0,0) !!!
    How can I "freeze" the new position (+AnchorPoint offset) altogether?
    Owltwins. Smart design and creative code.
    »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
  • @jack0088 For the centering I think is a good option what posted @techdojo .
    But I think you want change the die point (center registration point) , and in that case maybe better to use the Matrix. In this case you can use also the rotation well.

    I don't know well if in Gideros , we van all things what in as .
    Look at here, maybe it is useful info for you:
    http://docs.brajeshwar.com/as2/flash/geom/Matrix.html

    @atilim how will be manage the setAnchorPoint for Sprite (setY, getX, etc...)?

    @atilim, in the further will have be to possibility implement Matrix classs in Gideros similar to as3 , obviously not with all methods :P
    Whit this you can creat in same situations 2D , and for fake 3D and also for color.
    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Matrix.html
    swf
    swf
    transform_methods.swf
    2K
  • ar2rsawseenar2rsawseen Maintainer
    @gaboritaly I think sprite anchorpoint would behave similarly to Bitmap, when you set for x and y if two arguments are passed and for both if there is only one argument.

    And about matrix, you can check one of my classes, I used for basic matrix abstraction, when migrating from Javascript Canvas to Gideros

    http://appcodingeasy.com/Gideros-Mobile/Abstracting-matrix-transformation-in-Gideros-Mobile
  • jack0088jack0088 Member
    edited July 2012
    @gaboritaly I use @ar2rsawseen Transform class. I tried to do the same as above but with matrix transformation, but I always get the
    print(circle:getPosition()) -- (-95.95, 0), where it should be (0,0) !!!
    wrong coordinates. They just wont "freeze" and null themself out :(( don't know what else to try..

    Btw. this was my matrix transformation try:
    local matrix = Transform.new()
          matrix:translate( (self:getX()-self:getWidth()*ax)+x, (self:getY()-self:getHeight()*ay)+y )
          matrix:apply(self)
    Owltwins. Smart design and creative code.
    »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
  • ar2rsawseenar2rsawseen Maintainer
    edited July 2012
    Well what translation does is moving position, so it works right. But now I understood what you want.

    Can't you just rewrite methods to return what you want?
    function circle:getPosition()
    return 0, 0
    end
    Well you can create even more sophisticated, so you can set up internal x and y, etc, and return them.

    Likes: jack0088

    +1 -1 (+1 / -0 )Share on Facebook
  • Well what translation does is moving position, so it works right. But now I understood what you want.

    Can't you just rewrite methods to return what you want?
    function circle:getPosition()
    return 0, 0
    end
    Well you can create even more sophisticated, so you can set up internal x and y, etc, and return them.
    Damn! You got exactly what I wanted! Now I just have to figure out how I want that all to be implemented :)

    Owltwins. Smart design and creative code.
    »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
  • @ar2rsawseen thank you for the link ;)
  • MellsMells Guru
    edited September 2012
    For reference, @jack0088 (thank you for sharing) submitted a library to help with setting Sprite:setAnchorPoint()
    [ Link ]

    However, in my case, I couldn't make it work yet.
    I get a few errors when I'm trying to use it, at project's startup :
    sources/lib/AnchorPoints.lua:136: bad argument #2 to '__setScaleY' (number expected, got nil)
    Any idea on how to fix it?

    Also, it would be great if it was supported by Gideros Studio.

    @atilim is an official Sprite:setAnchorPoint() coming in a soon to be released version of Gideros?
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • @Mells: It should work out if the box.. Did you changed anything; maybe forgot to pass any parameters..?
    Owltwins. Smart design and creative code.
    »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
  • @jack0088
    This is strange, I use
    obj:setAnchorPoint(.5, .5)
    obj is a simple Sprite.

    I didn't change anything, I included AnchorPoints.lua, Helpers.lua (event Primitives.lua) in my project and checked the code execution order. Mmm..

    Will check again when I have more time, I was wondering if you had already faced this issue.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • One thing to keep in mind, if you add a child to a sprite or anything, you will need to set the anchor point again it looks like to make this work, otherwise the anchor point will still be where it was originally. Although I see you did what I was considering for rotation fixes ;)
  • john26john26 Maintainer
    I think the reason its not implemented is because a sprite can have children (which have children of their own add infinitum...) in which case its hard to specify exactly what you mean by an anchor point. There are several different interpretations of what this might mean and users might get so confused it would negate any benefits... Well, just a thought as to why it currently isn't supported.
  • I'm curious performance wise if it's better to make it a pure mathematical solution. I saw some solutions with ActionScript 3 where basically it was better to generally do what's going on under the hood here. I'm fine with how it's implemented, I'm just not sure it's consistent with the built-in anchor point on Bitmaps is, so I thought it was worth mentioning.
  • jack0088jack0088 Member
    edited September 2012
    @zvardin: I also wish there was a native way of doing it... This is only a temporary solution)
    @Mells: no didn't expirience it. But I'll look into it a little later.
    Owltwins. Smart design and creative code.
    »Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
  • I wonder, its now Feb 2013, and idea on ETA for this?
    REAL programmers type copy con filename.exe
    ---------------------------------------
  • Why hasn't this been completed? it is over a year since it was entered as an issue?
  • MauMauMauMau Member
    I just started working with Gideros (migrated from Corona) and one of the first things I noticed is that you cannot set the anchor point of a sprite. This thread was posted more than one year ago -and still no implementation..?? :-(
  • MellsMells Guru
    edited July 2013
    @MauMau
    You give me the opportunity to close the loop : Gideros Coding Easy is a lib that provides anchor points for Sprites but also other useful things.

    At some point, this library will (or will not) be included in Gideros. I think it has not been clearly defined yet.

    Note : I have updated the comment at the top so that users will all be informed from now and in the future.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • MauMauMauMau Member
    Hm, where can we download that GiderosCodingEasy lib? I can't find any download button on that page. Did I miss something?
  • SinisterSoftSinisterSoft Maintainer
    You have to be careful as some of the values don't seem to be updated after you set the other values - the order of setting scale and the origin seem important.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
Sign In or Register to comment.