rotate image in polygon

Hello everybody,

I am trying to rotate an image filled within a polygon. 

Here is my code :

-- create a 400 x 100 polygon and fill it with an image of same size local polygon = display.newPolygon (200, 50, {0, 0, 400, 0, 400, 100, 0, 100}) polygon.fill = {type="image", filename="image\_400\_100.png"} local rotatePolygonImage = function () polygon.fill.rotation = polygon.fill.rotation + 1 end timer.performWithDelay (500, rotatePolygonImage, 360)

But the when the rotation is applied, the whole image is scaled to fit in the polygon.

Is there a way to make the rotation within the polygon without scaling the image? That is if part of the image is outside the polygon boundary, then it will not be visible.

I am playing with the scaleX and scaleY properties but with no success.

Can someone help?

 

Greetings!

I belive that by using the .fill method is not possible because of the textureWrap mode. By default is set to clampToEdge. I’ve made some tests and it does lot more than only scaling! 

Nevertheless here are some alternatives i came up with. For this particular case, if the container it can only be a rectangle i would use the display.newContainer:

[lua]local container = display.newContainer( 400, 100 )

container.x, container.y = 300, 200

local bk = display.newImageRect( “img.png”, 400, 100)

container:insert( bk )

local rotatePolygonImage = function ()

    bk.rotation = bk.rotation + 1

end

timer.performWithDelay (500, rotatePolygonImage, 360)[/lua]

But since you are using .newPolygon i guess that you want to create heptagonal or more complex polygons. For that you have to play with masks, a start point could be:

[lua]local bk = display.newImageRect(“img.png”, 400, 100 )

bk.x, bk.y = 300, 200

local mask = graphics.newMask( “mask.png” )

bk:setMask(mask)

local rotatePolygonImage = function ()

    bk.rotation = bk.rotation + 1

    bk.maskRotation = bk.maskRotation - 1

end

timer.performWithDelay (100, rotatePolygonImage, 360)[/lua]

I hope this shines some light on the matter

Greetings!

I belive that by using the .fill method is not possible because of the textureWrap mode. By default is set to clampToEdge. I’ve made some tests and it does lot more than only scaling! 

Nevertheless here are some alternatives i came up with. For this particular case, if the container it can only be a rectangle i would use the display.newContainer:

[lua]local container = display.newContainer( 400, 100 )

container.x, container.y = 300, 200

local bk = display.newImageRect( “img.png”, 400, 100)

container:insert( bk )

local rotatePolygonImage = function ()

    bk.rotation = bk.rotation + 1

end

timer.performWithDelay (500, rotatePolygonImage, 360)[/lua]

But since you are using .newPolygon i guess that you want to create heptagonal or more complex polygons. For that you have to play with masks, a start point could be:

[lua]local bk = display.newImageRect(“img.png”, 400, 100 )

bk.x, bk.y = 300, 200

local mask = graphics.newMask( “mask.png” )

bk:setMask(mask)

local rotatePolygonImage = function ()

    bk.rotation = bk.rotation + 1

    bk.maskRotation = bk.maskRotation - 1

end

timer.performWithDelay (100, rotatePolygonImage, 360)[/lua]

I hope this shines some light on the matter