A good way to have text follow the camera?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

A good way to have text follow the camera?

Post by baconhawka7x »

I've been trying to fing this for a long time. So, I use this camera lib...

Code: Select all

camera = {}
camera.x = 0
camera.y = 0
camera.scaleX = 0.2
camera.scaleY = 0.2
camera.rotation = 0


function camera:set()
  love.graphics.push()
  love.graphics.rotate(-self.rotation)
  love.graphics.scale(1 / self.scaleX, 1 / self.scaleY)
  love.graphics.translate(-self.x, -self.y)
end

function camera:unset()
  love.graphics.pop()
end

function camera:move(dx, dy)
  self.x = self.x + (dx or 0)
  self.y = self.y + (dy or 0)
end

function camera:rotate(dr)
  self.rotation = self.rotation + dr
end

function camera:scale(sx, sy)
  sx = sx or 1
  self.scaleX = self.scaleX * sx
  self.scaleY = self.scaleY * (sy or sx)
end

function camera:setPosition(x, y)
  self.x = x or self.x
  self.y = y or self.y
end

function camera:setScale(sx, sy)
  self.scaleX = sx or self.scaleX
  self.scaleY = sy or self.scaleY
end
And I'm trying to display the players health in the top left corner of the screen.

Code: Select all

love.graphics.print(player.health,5,5)
But, sense the camera is moving, the "player's health" has to move too..

Code: Select all

love.graphics.print(player.health,camera.x + 5,camera.y + 5)
But in doing this. When the camera is moved, the text, although stays in the same general area, moves around a lot.
So how should I print text on the screen, that can move with the camera, and not get distorted or blurred(or shaken;)

~Thanks in advanced
User avatar
zipperipper
Prole
Posts: 45
Joined: Thu Jan 14, 2010 7:59 pm
Location: UK

Re: A good way to have text follow the camera?

Post by zipperipper »

I don't see any issue with the code you have presented. Are you updating the camera before or after its position and rotation has been applied to love.graphics?
the only reason I see it moving when it shouldn't is if you're apply the camera settings to love.graphics, updating the camera, and then drawing the text, which would cause it to be a little further ahead, otherwise it should be fine. :huh:
Error 404, consciousness not found.
User avatar
veethree
Inner party member
Posts: 874
Joined: Sat Dec 10, 2011 7:18 pm

Re: A good way to have text follow the camera?

Post by veethree »

In your love.draw you have something like this right?:

Code: Select all

function love.draw()
	camera:set()
	--Draw stuff
	camera:unset()
end
So what you want to do is have the health draw after the camera:unset(). That way it'll stay in the corner.

Like so:

Code: Select all

function love.draw()
	camera:set()
	--Draw stuff
	camera:unset()
	love.graphics.print(player.health,5,5)
end
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: A good way to have text follow the camera?

Post by coffee »

veethree wrote:In your love.draw you have something like this right?:

Code: Select all

function love.draw()
	camera:set()
	--Draw stuff
	camera:unset()
end
So what you want to do is have the health draw after the camera:unset(). That way it'll stay in the corner.

Like so:

Code: Select all

function love.draw()
	camera:set()
	--Draw stuff
	camera:unset()
	love.graphics.print(player.health,5,5)
end
This is same problem/issue I already told him at viewtopic.php?f=5&t=7292&p=48371#p48371 but he ignored. However same mistakes probably are still being made.
it's more logic to print it after the camera unset
with

Code: Select all

	camera:unset()
	love.graphics.setFont(large)
	love.graphics.print(steve.health, 400, 3)
	love.graphics.print(steve.money, 5, 450)
all works fine.
User avatar
tsturzl
Party member
Posts: 161
Joined: Fri Apr 08, 2011 3:24 am

Re: A good way to have text follow the camera?

Post by tsturzl »

Use love.graphics.push then set your camera and draw game area, then use love.graphics.pop now write gui/text to the screen.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 41 guests