X and Y Positions of an Image

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
burnheidl
Prole
Posts: 4
Joined: Fri Jan 01, 2010 7:50 pm

X and Y Positions of an Image

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: X and Y Positions of an Image

Post 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
Help us help you: attach a .love.
burnheidl
Prole
Posts: 4
Joined: Fri Jan 01, 2010 7:50 pm

Re: X and Y Positions of an Image

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: X and Y Positions of an Image

Post 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.
Help us help you: attach a .love.
burnheidl
Prole
Posts: 4
Joined: Fri Jan 01, 2010 7:50 pm

Re: X and Y Positions of an Image

Post 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..
Attachments
player.png
player.png (2.06 KiB) Viewed 4135 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: X and Y Positions of an Image

Post 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.
Help us help you: attach a .love.
burnheidl
Prole
Posts: 4
Joined: Fri Jan 01, 2010 7:50 pm

Re: X and Y Positions of an Image

Post by burnheidl »

thank you very much!
i got you!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: X and Y Positions of an Image

Post 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)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: X and Y Positions of an Image

Post 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.
Help us help you: attach a .love.
Post Reply

Who is online

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