Centering Images Perfectly

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
darkroom
Prole
Posts: 3
Joined: Sat Apr 27, 2013 1:43 pm

Centering Images Perfectly

Post by darkroom »

So I have an image that needs to change in scale but stay perfectly in the center my first thought of this would be:

Code: Select all

self.x = math.floor(love.graphics.getWidth()/2) - math.floor(image:getWidth()/2)
but that doesn't look centered and it doesn't take into account scaling. Thank you for your time .love below
Attachments
Archive.love
(5.01 KiB) Downloaded 126 times
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Centering Images Perfectly

Post by Sheepolution »

Code: Select all

love.graphics.draw(image,x-position,y-position,rotation,xscale,yscale,width,height)
So let's say you have a screen of 200 by 200, and you want to put it in the center:

Code: Select all

img = love.graphics.newImage("image")
x = 100
y = 100
w = img:getWidth()/2
h = img:getHeight()/2

love.graphics.draw(img,x,y,0,1,1,w,h)
This way it's drawn in the center of the screen.
darkroom
Prole
Posts: 3
Joined: Sat Apr 27, 2013 1:43 pm

Re: Centering Images Perfectly

Post by darkroom »

how do you change the scale then?
Edit: Nevermind but new problem now the fish isn't in sync with the camera am i doing something wrong with the camera? New code below.
Attachments
Archive.love
(5 KiB) Downloaded 120 times
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Centering Images Perfectly

Post by Sheepolution »

I have no idea what you want to achieve. Can you explain?
User avatar
vitaminx
Citizen
Posts: 95
Joined: Fri Oct 19, 2012 7:16 am
Location: international
Contact:

Re: Centering Images Perfectly

Post by vitaminx »

what about using the offset? when image is scaled, it stays centered.

Code: Select all

function player.load(self,image)                   
    self.x = love.graphics.getWidth()/2
    self.y = love.graphics.getHeight()/2
    self.xscale = 1 
    self.yscale = 1 
[...]                      
end                                                
                                                   
function player:draw()                             
    love.graphics.rectangle("fill",100,100,110,110)
    love.graphics.draw(self.image,self.x,self.y,0,self.xscale,self.yscale,self.image:getWidth()/2,self.image:getHeight()/2)
end
I've updated your boundary checks too, see attached love archive.

greets,
vitaminx
Attachments
archive_new.love
(4.5 KiB) Downloaded 141 times
Post Reply

Who is online

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