Moving sprite inside its boundaries for smaller viewport

Hi guys, i have a quick question. So i have go object with sprite. Sprite is bigger than my viewport (it is mobile game), that’s why i need to pan sprite around. I calculated border positions and just move sprite via go.set_position until it hits border position and it won’t move in that direction. Viewport is kinda trapped inside sprite. It works fine, but when i change viewport size during the game, calculated border positions are no longer valid → viewport can “escape” sprite and i will see things behind sprite. I tried to find correlation in viewport size to adjust border positions but failed. I also tried to create a camera and move it instead of go object, but the result is similar.

Here is the sketch)

Right now it works like this:

-- Move Left if possible
if x < 0 and go.get_position(node).x < self.size.x/2 then
      -- Move sprite
end

-- Move Right if possible
if x > 0 and go.get_position(node).x > -(self.size.x/2 - width) then
      -- Move sprite
end

-- Move Up if possible
if y > 0 and go.get_position(node).y > -(self.size.y/2 - height) then
      -- Move sprite
 end

-- Move Down if possible
if y < 0 and go.get_position(node).y < self.size.y/2 then
      -- Move sprite
end

self.size is size of sprite and width\height is width\height of screen.

Is there are any ways to overcome this problem, maybe change something in the render or even other approaches to move sprite inside boundaries.

How have you set up your render script? I assume you have something other than the default stretch projection?

Yep, cause i need to maintain same aspect ratio for the sprite. So it’s use_fixed_fit_projection. Only modification i made to render script is to get and store somewhere current width and height.

Note that you can get the current screen size from anywhere with window.get_size() now.

1 Like