Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Sprite:getBounds(targetSprite) — Gideros Forum

Sprite:getBounds(targetSprite)

GregBUGGregBUG Guru
edited September 2012 in Bugs and issues
Hi! i've restarted at coding my collision system... for simple axis oriented collision
Sprite:getBounds(targetSprite) is very useful but i found a problem... (POSSIBLE BUG?)

The Problem
my sprite is 32x32px

if i scale x=2 and y=2 getBounds return width and height = 64 -> and is correct,
but
if i scale x=2 and y=1 getBounds return width = 64 (correct) and height (64) -> ERROR
it should be height = 32px?

i'm missing something or is a bug ?

@atilim i'm wrong ?

LUA CODE:

local ax, ay, ah, aw = self:getBounds(self.parent)
local bx, by, bh, bw = spriteB:getBounds(self.parent)


where (in my test code) self.parent is = "stage"

thanks,
Gianluca.
TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
www.tntengine.com

Comments

  • atilimatilim Maintainer
    Accepted Answer
    Hi @GregBUG,

    This simple example works as expected:
    local sprite = Bitmap.new(Texture.new("box.png"))
    stage:addChild(sprite)
    print(sprite:getBounds(stage))
    sprite:setScale(2, 2)
    print(sprite:getBounds(stage))
    sprite:setScale(2, 1)
    print(sprite:getBounds(stage))
    Here the output is:
    0	0	80	80
    0	0	160	160
    0	0	160	80
    Which version are you using? And can you post an example?
  • GregBUGGregBUG Guru
    edited September 2012
    @atilim

    yes... my fault!!!

    as you can see i swapped width and height

    local ax, ay, ah, aw = self:getBounds(self.parent)
    ----------^^^^^^
    local bx, by, bh, bw = spriteB:getBounds(self.parent)
    ------------^^^^^^
    so my code not work!.

    sorry... :\">


    ahhh!!! 8-X

    Dislikes: GregBUG

    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
    +1 -1 (+0 / -1 )Share on Facebook
  • atilimatilim Maintainer
    edited September 2012
    hahaha.. Even though I've looked at your code to see "aw, aw" and "ah, ah", I've also missed that you swapped width and height :)

    Likes: ar2rsawseen

    +1 -1 (+1 / -0 )Share on Facebook
  • atilimatilim Maintainer
    edited September 2012
    As a side note, you can always use sprite:getBounds(sprite) to get untransformed bounds and transform as you wish.
  • GregBUGGregBUG Guru
    edited September 2012
    @atilim
    yes sprite:getBounds(sprite) return the box that contain the sprite...
    but if the sprite is rotated bound box size is increased... so if i transform transformation is not correct... :(

    i mean... (getBounds in RED)



    test.png
    290 x 158 - 4K
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • atilimatilim Maintainer
    edited September 2012
    sprite:getBounds(sprite) always returns the untransformed bounds:
    local sprite = Bitmap.new(Texture.new("box.png"))
    print(sprite:getBounds(sprite))
    sprite:setRotation(45)
    print(sprite:getBounds(sprite))
    Output is
    0	0	80	80
    0	0	80	80
    But you're correct about getWidth()/getHeight(). Because they return the transformed bounds. If the sprite is attached to a parent, getWidth()/getHeight() always return the same result with sprite:getBounds(sprite:getParent())
  • ok.

    but x and y are always 0

    so the problem is when

    local sprite = Bitmap.new(Texture.new("tntBox2.png"))
    sprite:setPosition(160, 120)
    sprite:setAnchorPoint(0,0)
    stage:addChild(sprite)

    print(sprite:getBounds(stage))
    sprite:setRotation(45)
    print(sprite:getBounds(stage))

    now i get x and y coords, but w and h are wrong... :(
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • atilimatilim Maintainer
    edited September 2012
    Here x and y is 0 because I'm getting the bounds according to the sprite. But you're getting the bounds according to the stage. For example I've attached the screenshot of this example:
    local sprite = Bitmap.new(Texture.new("box.png"))
    sprite:setPosition(160, 120)
    sprite:setScale(2, 1)
    sprite:setAnchorPoint(0.3, 0.5)
    sprite:setRotation(45)
    stage:addChild(sprite)
     
    local x, y, w, h = sprite:getBounds(stage)
     
    local shape = Shape.new()
    shape:setLineStyle(1, 0x000000)
    shape:moveTo(x, y)
    shape:lineTo(x + w, y)
    shape:lineTo(x + w, y + h)
    shape:lineTo(x, y + h)
    shape:closePath()
    shape:endPath()
     
    stage:addChild(shape)
    hmm.. Still I can't understand your point? ;;)
    scr.png
    349 x 290 - 46K
    scr.png 46.5K
  • GregBUGGregBUG Guru
    edited September 2012
    no.. no.. you are right...

    getBounds work fine...

    but is not what i need.... :)


    i mean
    when spirte is not rotated bBox returned is perfect (coincide with the sprites) so collision is perfect....

    but when i rotate the sprite bbox is not what i need...

    I thought that "getBounds" returned original bbox also when the sprite is rotated...
    (i need the bbox of sprite angle = 0 then i rotate it according to the angle sprites so i get perfect oriented bBox) ...


    sorry but my english is poor! and i don't know how to explain better :(

    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • @GregBUG
    are u trying to say this
    local bg = Bitmap.new(Texture.new("gfx/myBg.png"))
    	bg:setPosition(512,384)
    	self:addChild(bg)
    	bg:setRotation(45)
     
    	print(bg:getBounds(bg))--needs width and height from this
    	print(bg:getBounds(stage)) -- needs x,y from this 
     
    	bg:setRotation(0)
    	print(bg:getBounds(bg))
    print(bg:getBounds(stage))
    output

    0 0 1024 768 ----- width and height is perfect
    -31.057983398438 384 1267.1353759766 1267.1352539062 ---x and y are perfect
    0 0 1024 768
    512 384 1024 768
  • atilimatilim Maintainer
    I think I understand now. Then you need to get untransformed bounds with sprite:getBounds(sprite) and transform as you wish. For example:
    local x, y, w, h = sprite:getBounds(sprite)
    x = x + sprite:getX()
    y = y + sprite:getY()
    w = w * sprite:getScaleX()
    h = h * sprite:getScaleY()
    So that you can get bounds independent of rotation. Maybe this is what you want?

    Or get the untransformed bounds and move it to stage's coordinate system:
    local x, y, w, h = sprite:getBounds(sprite)
    local x1, y1 = sprite:localToGlobal(x, y)
    local x2, y2 = sprite:localToGlobal(x + w, y)
    local x3, y3 = sprite:localToGlobal(x + w, y + h)
    local x4, y4 = sprite:localToGlobal(x, y + h)
  • atilimatilim Maintainer
    And one more question? Do you want to get oriented bounding box according to the stage? (OBBs are usually defined with 5 parameters: center x, center y, half width, half height and rotation)
  • @hgvyas123 @atilim
    thanks guys for help!
    local x, y, w, h = sprite:getBounds(sprite)
    x = x + sprite:getX()
    y = y + sprite:getY()
    w = w * sprite:getScaleX()
    h = h * sprite:getScaleY()
    this is what i need ! :)

    thanks!
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
Sign In or Register to comment.