[Help needed] Math.atan2()

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
Raxe88
Prole
Posts: 15
Joined: Mon Jul 29, 2013 11:14 pm

[Help needed] Math.atan2()

Post 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 :)
User avatar
seanmd
Prole
Posts: 35
Joined: Sat Jun 22, 2013 7:47 am

Re: [Help needed] Math.atan2()

Post by seanmd »

the second argument should probably be mousex - playerx rather than playery
Raxe88
Prole
Posts: 15
Joined: Mon Jul 29, 2013 11:14 pm

Re: [Help needed] Math.atan2()

Post 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
Selenaut
Prole
Posts: 6
Joined: Wed Jul 24, 2013 10:41 pm

Re: [Help needed] Math.atan2()

Post 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
Making a game in Lua without knowing Lua.
Raxe88
Prole
Posts: 15
Joined: Mon Jul 29, 2013 11:14 pm

Re: [Help needed] Math.atan2()

Post 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.
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: [Help needed] Math.atan2()

Post 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)
Your screen is very zoomed in...
Raxe88
Prole
Posts: 15
Joined: Mon Jul 29, 2013 11:14 pm

Re: [Help needed] Math.atan2()

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 5 guests