# translate doesn't affect clip()

Hi, I was busy playing around when I noticed that when I translated a shape, the clip() co-ordinates do not change.

That’s correct, and should be the expected behavior. The clip rectangle is in screen space. Under the hood, clip() is implemented with glScissor, which is not affected by the model matrix.

Sorry to re-open this post, but now I am using translate and clip at the same time, and my graphics go haywire. How do I get the current translate position? And is it possible for clip to be affected by translate, scale, and rotate in future versions?

@Jordan unfortunately that’s just not the way clip is designed to work — clip works in screen-space only. There’s no way for it to be sensibly affected by the transform because OpenGL won’t allow rotating of the clipping rectangle, not to mention what would happen to the clipping rect under a 3D transformation.

As a stop gap solution: you can extract the model matrix, which (according to the docs) is used for the 2d transformation stuff. You then use that to transform the vector (0, 0), the result is the translation. Also, you can transform the vector(1, 0), from which you can obtain the scale (as long as it is uniform, not tried non-uniform scales, but using (1, 0) for x scale and (0, 1) for y scale should work), which is the length of the resulting vector, and the rotation, which is math.atan2(p.p, p.x) when p is the result of said transformation. But, as you can’t rotate clip rectangles anyway, scale and translation should be obtainable by this.

as long as you don’t rotate(!), and only use translate() and scale(), this should do it:

``````    function draw()
translate(111,222)
scale(2, 3)

m = modelMatrix()
resetMatrix()

text("translate "..m[13]..","..m[14], 100, 80)
text("scale x "..m[1], 100, 64)
text("scale y "..m[6], 100, 48)
end
``````

if you rotate, things get a little more complex, and my method will only handle uniform scales.

Thanks for all the help! Here is my code (sorry for not posting it earlier, I forgot about it, and the code took me 3 minutes )

``````-- New clip function, affected by translate() and scale()
_clip = clip
function clip(x, y, w, h)
if x ~= nil then
local m = modelMatrix()
x = x * m[1] + m[13]
y = y * m[6] + m[14]
w = w * m[1]
h = h * m[6]
_clip(x, y, w, h)
else
_clip()
end
end
``````

You should paste that code into a tab of a library project, and when you select that project as a dependency, the clip() function will be updated automagically