Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
setClip on negative coordinate sprite — Gideros Forum

setClip on negative coordinate sprite

totebototebo Member
edited January 2017 in General questions
Guys, what am I doing wrong here? I'm trying to copy the pixels of a sprite, setClip the new sprite and nest it within the first (phew!). When doing this, the setClip'ped sprite doesn't inherit rotations. Probably because it IS nested, so they're cancelled out? If so, what's the proper way of doing this?

Code:
local shape = Shape.new()
shape:setFillStyle(Shape.SOLID, 0xff0000, 1)
shape:beginPath()
shape:moveTo(-50,-50)
shape:lineTo(50, -50)
shape:lineTo(50, 50)
shape:lineTo(-50, 50)
shape:lineTo(-50, -50)
shape:endPath()
shape:setPosition(150, 150)
stage:addChild(shape)
 
local rt = RenderTarget.new( shape:getWidth(), shape:getHeight() )
local x,y,w,h = shape:getBounds(stage)
rt:draw( shape, -x,-y )
local bitmap = Bitmap.new(rt)
bitmap:setPosition(-x/2,-y/2)
bitmap:setColorTransform( 0,0,0,1 )
shape:addChild(bitmap)
 
bitmap:setClip( 0,0,shape:getWidth(),shape:getHeight()/2 )
 
stage:addEventListener( Event.ENTER_FRAME,
	function()
		shape:setRotation(shape:getRotation()+1)
	end
)
My Gideros games: www.totebo.com

Comments

  • hgy29hgy29 Maintainer
    edited January 2017
    Shouldn't you redraw your RenderTarget on each enter frame for this to work ? Or I misunderstood what you are trying to achieve ?

    EDIT: Ah, your bitmap is a child of shape, so it should be rotated...
    EDIT2: Ok, got it: rotation is applied before setClip(), so your clipped version of bitmap is always square!
  • hgy29hgy29 Maintainer
    Accepted Answer
    To be more precise, clipping is applied late in the rendering pipeline, in screen coordinates, so it is always screen axis aligned, and can't be rotated.
  • Gotcha! Have to figure out a workaround for this.
    My Gideros games: www.totebo.com
  • The only thing I can think of is to use RenderTarget instead of setClip. Does that sound right?
    My Gideros games: www.totebo.com
  • hgy29hgy29 Maintainer
    Yes, you can adjust your render target size to grab only the pixels you need.
  • Yeah, nice. I hoped to use setClip so I don't have to create rendertargets every frame. Would it be tricky/impossible for setClip to support rotation?
    My Gideros games: www.totebo.com
  • Turns out RenderTarget is too slow to use for this. I have to create a new RenderTarget every frame for all players (it's a multiplayer game) and the game grinds to a halt.

    Is there another way of doing this? See what I'd like to achieve below.
    Artboard.png
    589 x 323 - 24K
    My Gideros games: www.totebo.com
  • Solved it! Instead of creating a new RenderTarget each frame I create one and re-use it when needed.

    Likes: antix

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • Yep, RenderTarget:clear() is magic ;)

    Likes: totebo

    +1 -1 (+1 / -0 )Share on Facebook
  • totebototebo Member
    Back to this one! So what I want to do now is the OPPOSITE of the original post; I want to clip a sprite which is rotated. The clipping changes every frame of the rotation. Why is this?
    local shape = Shape.new()
    shape:setFillStyle(Shape.SOLID, 0xff0000, 1)
    shape:beginPath()
    shape:moveTo(-50,-50)
    shape:lineTo(50, -50)
    shape:lineTo(50, 50)
    shape:lineTo(-50, 50)
    shape:lineTo(-50, -50)
    shape:endPath()
    shape:setPosition(150, 150)
    stage:addChild(shape)
     
    shape:setClip( 0,0,shape:getWidth(),shape:getHeight()/2 )
     
    stage:addEventListener( Event.ENTER_FRAME,
    	function()
    		shape:setRotation(shape:getRotation()+1)
    	end
    )
    My Gideros games: www.totebo.com
  • totebototebo Member
    I found a workaround for this one; add the rotated sprite to a non-rotated sprite and clip the non-rotated sprite instead:
    local sprite = Sprite.new()
     
    local shape = Shape.new()
    shape:setFillStyle(Shape.SOLID, 0xff0000, 1)
    shape:beginPath()
    shape:moveTo(-50,-50)
    shape:lineTo(50, -50)
    shape:lineTo(50, 50)
    shape:lineTo(-50, 50)
    shape:lineTo(-50, -50)
    shape:endPath()
    shape:setPosition(150, 150)
     
    sprite:addChild(shape)
     
    stage:addChild(sprite)
     
    local x,y,w,h = sprite:getBounds( sprite )
    sprite:setClip( x,y,w,h )
     
    stage:addEventListener( Event.ENTER_FRAME,
    	function()
    		shape:setRotation(shape:getRotation()+1)
    	end
    )

    Likes: antix, Apollo14

    My Gideros games: www.totebo.com
    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.