[Solved]love.graphics.translate

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
Dr.Tyler O.
Citizen
Posts: 58
Joined: Tue Jul 29, 2014 9:17 am
Location: United States

[Solved]love.graphics.translate

Post by Dr.Tyler O. »

I have a project that I am working in which the camera follows the center of the player. But, in attempting to debug the game by displaying some variables on the screen, I have ran into a problem. In the following code you can assume that the player and the world are drawn in the function draw_world() and all of the variables I need to display are drawn in draw_debug(). Originally I had the debug values drawn before the world and I noticed that the world overlapped them so I made the debug values be drawn last. However, this still doesn't work. The variables don't stick to the 0, 0 that is in the top left of the window anymore, it is now drawn at 0, 0 in the world. I don't feel like a .love file is necessary for this issue so please don't ask me to provide one. I feel as though I have supplied all of the necessary code to understand the issue at hand.

Code: Select all

function love.draw()
    love.graphics.translate(-player.x + 400 - (player.w/2), -player.y + 300 - (player.h/2))
    draw_world()
    love.graphics.translate(0, 0)
    draw_debug()
end
Last edited by Dr.Tyler O. on Sun Jun 21, 2015 10:51 am, edited 1 time in total.
Any topic I have ever posted that I felt did not require a .love file, although they were requested, never required one so I would appreciate if you did not ask me to supply one unless you think that I am ignorant for not doing so.
Joe Black
Prole
Posts: 39
Joined: Wed Jan 21, 2015 1:57 pm

Re: love.graphics.translate

Post by Joe Black »

the wiki say :
When this function is called with two numbers, dx, and dy, all the following drawing operations take effect as if their x and y coordinates were x+dx and y+dy.
This change lasts until love.draw() exits or else a love.graphics.pop reverts to a previous love.graphics.push.
so you must translate back like

Code: Select all

    
function love.draw()
   love.graphics.translate(-player.x + 400 - (player.w/2), -player.y + 300 - (player.h/2))
   draw_world()
   love.graphics.translate(-(-player.x + 400 - (player.w/2)),-( -player.y + 300 - (player.h/2)))
   draw_debug()
end
or more convenient if translate is not the only one transformation

Code: Select all

    
function love.draw()
   love.graphics.push()
   love.graphics.translate(-player.x + 400 - (player.w/2), -player.y + 300 - (player.h/2))
   draw_world()
   love.graphics.pop()
   draw_debug()
end
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: love.graphics.translate

Post by Positive07 »

Use [wiki]love.graphics.push[/wiki] and [wiki]love.graphics.pop[/wiki] to have a stack of all your transformations and move through them or alternatively use [wiki]love.graphics.origin[/wiki] to reset to the default
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: love.graphics.translate

Post by kikito »

If you use my library, gamera, you will be able to do this:

Code: Select all

local gamera = require 'gamera'

local player, camera, world

function love.load()
  player = ...
  world = ...
  camera = gamera.new(0,0,world.width, world.height)
end

function love.update(dt)
  camera:setPosition(player.x, player.y)
  ...
end

function love.draw()
  -- you could also do cam:draw(draw_world)
  camera:draw(function()
    draw_world()
  end) 
  draw_debug()
end

Last edited by kikito on Fri Jun 19, 2015 10:25 am, edited 1 time in total.
When I write def I mean function.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: love.graphics.translate

Post by s-ol »

kikito wrote:If you use my library, gamera, you will be able to do this:

Code: Select all

local gamera = require 'gamera'

local player, camera, world

function love.load()
  player = ...
  world = ...
  camera = gamera.new(0,0,world.width, world.height)
end

function love.update(dt)
  cam:setPosition(player.x, player.y)
  ...
end

function love.draw()
  -- you could also do cam:draw(draw_world)
  cam:draw(function()
    draw_world()
  end) 
  draw_debug()
end

Actually you could even DL

Code: Select all

cam:draw(draw_world)
draw_debug()

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: love.graphics.translate

Post by kikito »

Yep, it's in the comment :neko:
When I write def I mean function.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: love.graphics.translate

Post by s-ol »

kikito wrote:Yep, it's in the comment :neko:
whoops :P

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 8 guests