Jump to content

Make sprites / images ignore camera zoom


Bonsaiheldin
 Share

Recommended Posts

Hello there.

I have some images which have name tags on them, displaying names or quantities. The name tags are Text objects and i want them to stay of the same size independently of the camera's zoom.

Something like that exists for camera scrolling: Sprite.setScrollFactor(0) makes sprites stay at the same position on the screen, no matter where the camera's position is or where it scrolls to.

Is there something like that for zooming, too? I'm basically searching for a Sprite.setZoomFactor.

At the moment i have my own camera zoom function which fires camera.setZoom and then iterates all name tags in the game and fires Text.setScale(1 / Camera.zoom). It works, but only as long as the zoom happens suddenly, not smooth.

I've tried tweening the camera's zoom value and Camera.zoomTo. Both ways enable smooth zooming, but the name tags get distorted / pixelated when i use scaleX / scaleY tweens on them.

Is there a possibility to make them just ignore the zoom or at least a zoomTo for sprites / images?

Link to comment
Share on other sites

There's no way to stop a Game Object responding to a Camera's zoom or rotation because the display list works by taking the transform matrix for the camera and then applying it down the whole list. It can't selectively only take part of it for just some of the children.

The easiest solution I think would be to have a parallel Scene on the top of the game scene, which holds just your name tags, and position them relative to the sprites in the Scene below. Alternatively, you could use a second Camera that ignores everything other than name tags, and have the main Scene camera ignore all name tags. Then zooming the main camera won't impact the tags, because the 'tag' camera can stay as-is.

Link to comment
Share on other sites

7 hours ago, rich said:

Alternatively, you could use a second Camera that ignores everything other than name tags, and have the main Scene camera ignore all name tags. Then zooming the main camera won't impact the tags, because the 'tag' camera can stay as-is.

Thanks rich. I'm going for the approach with the second camera, it's the most straight forward one to me because i haven't worked with scenes yet. It works. ?

I now just have to calculate the image's positions on screen to properly position the name tags as they have to take the main camera's zoom into consideration. They are now only placed where they should be when the zoom is 1 of course. What's the formula to get an object's position on the canvas, taking zoom into consideration?

Link to comment
Share on other sites

  • 1 year later...
On 17.09.2018 at 21:24, Bonsaiheldin said:

Спасибо, богатые. Я собираюсь приблизиться ко второй камере, она самая прямолинейная для меня, потому что я еще не работал со сценами. Оно работает. ?

Теперь мне просто нужно рассчитать положение изображения на экране, чтобы правильно расположить теги имен, поскольку они должны учитывать масштаб основной камеры. Теперь они размещаются только там, где они должны быть, когда масштаб 1, конечно. Какова формула для определения положения объекта на холсте с учетом увеличения?

Hello!

Did you found a solution?

Link to comment
Share on other sites

  • 1 month later...

Yes, i found a solution to the original thread's problem. To make a sprite "ignore" the camera's zoom, you just scale it by 1 / zoom. It will stay of the same size, regardless of the zoom level of the camera. Keep in mind though that this approach actually makes the sprite bigger when you zoom out and smaller when you zoom in, so it could potentially mess up your game's mechanics. If you don't want that you might be better off with a separate scene.

sprite.setScale( 1 / game.camera.zoom, 1 / game.camera.zoom );

 

Edited by Bonsaiheldin
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...