限制绘图到一个区域

Restrict drawing to an area

问题

我想在 love2d 内有一个区域,可以在其中绘制可移动的对象。对象的移动不受区域边界的限制,但绘图是。可以把它想象成通过 window 向外看。例如:一个区域中的蓝色矩形,如果它移动到一边,它的绘图应该被截断到区域的边界。

搬家前:

移动后(错误):

移动后(右):

限制和假设

  1. 你可以假设这个区域是矩形的。
  2. 要在内部绘制的对象可以是任何东西:多边形、图像或文本。
  3. 该区域覆盖它后面的任何东西(好像它有自己的背景)
  4. 未 'belonging' 区域的对象应照常绘制。

尝试的解决方案

我知道我可以在对象 'touch' 到达区域边界时立即停止绘制对象,但这会导致它们突然消失,然后在它们完全位于区域内时出现。我猜它需要某种分层系统,但我不知道如何将其包含在 love2d.

我想你正在寻找 love.graphics.setScissor

The scissor limits the drawing area to a specified rectangle.

调用不带任何参数的函数(即 love.graphics.setScissor())会禁用 scissor。

示例:

function love.draw ()
  -- sets the drawing area to the top left quarter of the screen
  local width, height = love.graphics.getDimensions()
  love.graphics.setScissor(0, 0, width / 2, height / 2)
  -- code to draw things
  love.graphics.setScissor()
end