Page 1 of 2

How to print text that isn't affected by the camera.scale ?

Posted: Mon Jul 08, 2019 6:08 pm
by Harrylechienfou
Hi ! I use, for the first time in one of my projects, the camera module available here : https://ebens.me/post/cameras-in-love2d ... he-basics/ and I'm very happy with it so far. The only problem is that I don't know how to print or draw something on the screen that is not affected by the camera module.
I currently use the camera.scaleX and camera.scaleY thing with my 2D game so the player can zoom in and out, but the problem is that when he zooms in or out, everything zoom in or out including user interface (how many life the player has, etc), which, of course is a problem.
I can avoid that problem by using camera.scaleX and camera.scaleY as the scale arguments for my print function, but it does not avoid the fact that, when I zoom out, every text position changes and basically moves to the top left corner of the screen (I hope I'm clear).

I'm still a begginer and it's the first time I use this camera module (or any kind of camera really) so if someone can help me with that silly problem, that will be very cool, thanks :)

Edit : I forgot about the second problem. I want my player to be on the middle of the screen so I wrote that
camera.x = (player.x - (screen_width/2))
and
camera.y = (player.y - (screen_height/2))
but when I zoom out the player is not centered anymore as you guys can imagine. How can I correct that too ?

Thanks for your help :)

Re: How to print text that isn't affected by the camera.scale ?

Posted: Mon Jul 08, 2019 6:20 pm
by raidho36
You simply reset the camera before drawing it.

Re: How to print text that isn't affected by the camera.scale ?

Posted: Mon Jul 08, 2019 6:37 pm
by Harrylechienfou
What do you mean by that and how do I do that ? I'm sorry I'm sure it's a silly question but I'm a begginer :/

Re: How to print text that isn't affected by the camera.scale ?

Posted: Mon Jul 08, 2019 6:40 pm
by Harrylechienfou
Oh I think I understand sorry. I guess I have to do a camera:unset() before I print all the texts ?
But what about the second problem ?

Re: How to print text that isn't affected by the camera.scale ?

Posted: Tue Jul 09, 2019 7:08 am
by TheHUG
the camera origin is the upper left corner? If you can change that, then it would be best to set the origin at the center so that you can just set it to player.x player.y.
If not, then you'll just need to reset the camera's location after you change its scale (apply the same operation)

Re: How to print text that isn't affected by the camera.scale ?

Posted: Tue Jul 09, 2019 10:59 am
by PGUp
use love.graphics.push() before drawing the text and love.graphics.pop() after drawing the text

Re: How to print text that isn't affected by the camera.scale ?

Posted: Tue Jul 09, 2019 4:12 pm
by Harrylechienfou
Ok thanks for the help everybody. I knew I had to change the camera.x and camera.y settings to include the camera.scale in the formula, but I did not do it the right way. Now it seems to be fixed.
You were very helpfull, thanks a lot.

Re: How to print text that isn't affected by the camera.scale ?

Posted: Tue Jul 09, 2019 5:19 pm
by Harrylechienfou
I have another question since were're here : what if I want to use the current mouse position to be the center of the screen when I zoom in/out ? Like for example I put the mouse somewhere on the screen when there is a red dot drawn, and because the mouse is wher the red dot is, it will zoom in/out where the mouse is (at the red dot position), but if after that I move my mouse to a blue dot drawn somewhere on the screen, then it will zoom in/out with the camera centered on this blue dot instead.

I tried different things with love.mouse.getX() and love.mouse.getY() but nothing works. I suspect that is because the mouse position does depend on the camera.x and the camera.y (which is not che case of the player.x and the player.y) since the mouse is drawn seperately from the rest of the game. Not sure though.

What I did with the camera being centered around the character no matter what level of zoom is the following :
camera.x = (player.x) - ((screen_width/2)*camera.scaleX)
camera.y = (player.y) - ((screen_height/2)*camera.scaleY)
But I can't do the same with the camera being centered around the mouse like this :
camera.x = (love.mouse.getX()) - ((screen_width/2)*camera.scaleX)
camera.y = (love.mouse.getY()) - ((screen_height/2)*camera.scaleY)
The problem is that when I zoom in/out, the mouse position on the screen stay the same, which is not the case of everything else's position, including the red dot and the blue dot, I suppose ?
Is that hard to manage ? I have no clue how to fix that.

If you have the solution, I'll be very interested, as I think it will help me to understand lots of stuff.
Thanks :)

Re: How to print text that isn't affected by the camera.scale ?

Posted: Mon Jul 15, 2019 1:24 pm
by Harrylechienfou
Does somebody knows how to do such a thing ? :o:

Re: How to print text that isn't affected by the camera.scale ?

Posted: Mon Jul 15, 2019 5:05 pm
by Jeeper
Remember that you mouse coordinates are not affected by the camera. So it if your screen in 1920x1080, then your camera.x will be 0-1920, even if you have moved 10 000 pixels to the right.