Page 1 of 1

X and Y Positions of an Image

Posted: Fri Jan 01, 2010 8:36 pm
by burnheidl
Hi Löve Community!

I'm new in here and I just started messing arround with Löve, so I'll just come straight to my question:

How can I get the x- and y-position of an Image object.

do I have to convert this to an ImageData to get the positions, or is there a function with the Image Type

the actual thing I want to do is get my player facing the mouse position.
I know, that I have to use the atan2 function to do that - also the search couldnt solve my problem so far

so what I'm doing:

First I'm loading the image and get the start positions:

Code: Select all

function love.load()
	player = love.graphics.newImage("gfx/player.png")
	x = 400
	y = 300
end
in the update function, I try to get the angle, but of course I can't without the positions and I'm doing the moving of the player.

Code: Select all

function love.update(dt)
	mousex = love.mouse.getX()
	mousey = love.mouse.getY()
	-- x = current x position of the Image
	-- y = current y position of the Image
	angle = math.atan2(mousey-y,mousex-x)
	if love.keyboard.isDown("w") then
		y = y - 5
	end
	if love.keyboard.isDown("s") then
		y = y + 5
	end
	if love.keyboard.isDown("d") then
		x = x + 5
	end
	if love.keyboard.isDown("a") then
		x = x - 5
	end
end

Re: X and Y Positions of an Image

Posted: Fri Jan 01, 2010 8:45 pm
by Robin
Image objects don't have a x or y -- you can draw them wherever you want. In most games you want to put the image and where you want to draw it in a table:

Code: Select all

player = {img = love.graphics.newImage("gfx/player.png"), x = 300, y = 200} -- something like that

Re: X and Y Positions of an Image

Posted: Fri Jan 01, 2010 8:57 pm
by burnheidl
actually, that was not my problem then.

i guess the problem is my trigonometry then ;)

because the atan2 thing cant be right

the x and y is changing anyway by pressing w,a,s,d

for some reason, a screenshot does not take the mouse with it.
the image is turning, but its not facing the mouse, so i guess something is wrong with my atan2

thx for your help so far - maybe somebody here knows whats wrong with my atan2

Re: X and Y Positions of an Image

Posted: Fri Jan 01, 2010 9:24 pm
by Robin
In what way is it facing then?

Does changing it to

Code: Select all

angle = math.atan2(y-mousey,mousex-x)
help?

I think using atan2 is the good thing, but something went wrong with either the arguments, or you need to add .5*math.pi to angle or something like that.

Re: X and Y Positions of an Image

Posted: Fri Jan 01, 2010 9:31 pm
by burnheidl
well.. im using this image for testing purposes, and the green line is not facing the mouse.

switching the arguments did not help..

Re: X and Y Positions of an Image

Posted: Fri Jan 01, 2010 9:48 pm
by Robin
I nearly figured it out back there, but here is the solution:
main.lua
(631 Bytes) Downloaded 176 times

Code: Select all

angle = math.atan2(mousex-x,y-mousey)
Lemme 'slpain:
You see, your image faces up. So if the image is rotated 0 degrees, it points up. But the coordinate system works differently -- in math, 0 degrees points to the right. Plus, in math, a higher y is up, but on your screen a higher y is down.

Re: X and Y Positions of an Image

Posted: Fri Jan 01, 2010 9:54 pm
by burnheidl
thank you very much!
i got you!

Re: X and Y Positions of an Image

Posted: Sat Jan 02, 2010 9:16 am
by bartbes
Just wanted to add that math.atan2 is defined as math.atan2(y, x), so y comes before x (which is not what robin just said)

Re: X and Y Positions of an Image

Posted: Sat Jan 02, 2010 2:22 pm
by Robin
bartbes wrote:Just wanted to add that math.atan2 is defined as math.atan2(y, x), so y comes before x (which is not what robin just said)
I know it is, (I didn't want to give the impression that it was anything other) however the mathematical system works differently from the screen system, which is why math.atan2(-x, y) works.