Page 1 of 1

[Help needed] Math.atan2()

Posted: Mon Jul 29, 2013 11:29 pm
by Raxe88
Hi! This is my first post in this forum, so, yeah happy to be with you. So, MY problem is this:
I want to do a top down shooter, but I don't really know how. I already searched this and I found 2 topics. One redirected me to math.atan and math.atan2 and the other one had even an example. BUT, sadly, and becouse of how I am and how I think, I can't get it to work. It actually works but it's really weird. This is the code:

Code: Select all

local function main()
	function love.load()
		--// Load sprites and tiles
		player = love.graphics.newImage("/images/pj.png")
		
		--//Declaring variables  
		--player = {}
		playerx = love.graphics.getWidth()/2
		playery = love.graphics.getHeight()/2
	end
	function love.update(dt)
		mousex, mousey = love.mouse.getPosition()		
		pointRadians = math.atan2((mousey - playery), (mousex - playery))
		--pointRadians = math.angle(playerx,playery,mousex,mousey)
	end
	function love.draw()
		--love.graphics.setColor( 0, 0, 0, 255 )
		love.graphics.print("This is a fail",0,0)
		love.graphics.draw(player, playerx, playery, pointRadians)
		love.graphics.setColor(255,255,255,255)
		love.graphics.line(playerx+8,playery+8,mousex,mousey)
	end
end
main()
The pj.png is a 16x16 square by the way. As said, works but it's really weird. Help would be appreciated.

Thanks for reading,

Raxe88 :)

Re: [Help needed] Math.atan2()

Posted: Mon Jul 29, 2013 11:45 pm
by seanmd
the second argument should probably be mousex - playerx rather than playery

Re: [Help needed] Math.atan2()

Posted: Tue Jul 30, 2013 12:30 am
by Raxe88
The fact is that the function is math.atan2 (y, x): http://www.lua.org/manual/5.1/manual.html#pdf-math.atan

Re: [Help needed] Math.atan2()

Posted: Tue Jul 30, 2013 1:59 am
by Selenaut
I'd bet the Y-movement is inverted, right?

The problem is that radians are measured clockwise in LOVE2D (because the origin is in the top left, thus the y-axis is pointing down), so you have to make the angle negative in order to counteract this.

Also, seanmd was correct:
seanmd wrote:the second argument should probably be mousex - playerx rather than playery
If my guess is wrong, could you perhaps upload a full .love file or explain what you mean by "weird?"

Good luck,

Selenaut

Re: [Help needed] Math.atan2()

Posted: Tue Jul 30, 2013 1:28 pm
by Raxe88
The y-axis and everything is ok, I rotates the way it should, but as you said it rotates at the top left. That's where my problem comes in, I want it to rotate in the middle of the player.

Re: [Help needed] Math.atan2()

Posted: Tue Jul 30, 2013 1:45 pm
by mickeyjm
Raxe88 wrote:The y-axis and everything is ok, I rotates the way it should, but as you said it rotates at the top left. That's where my problem comes in, I want it to rotate in the middle of the player.
You need to use the offset value of love.graphics.draw

if you offset it by half the width and height it will center, like this:

Code: Select all

love.graphics.draw(player, playerx, playery, pointRadians,1,1,player:getWidth()/2,player:getHeight()/2)

Re: [Help needed] Math.atan2()

Posted: Tue Jul 30, 2013 1:50 pm
by Raxe88
Ok, added this:

Code: Select all

love.graphics.draw(player, playerx, playery, pointRadians,1,1,player:getWidth()/2,player:getHeight()/2)
It works, rotates in the middle but...

http://puu.sh/3PoRK.png

What If I do pointRadians = pointRadians -30?

Edit: This works ^ but it's not perfect. There should be another way of solving this.