Page 1 of 1

Rotating Character

Posted: Fri Sep 17, 2010 9:43 pm
by teh n00bian
I am new at this so be nice ya'll. ;)

I found love on the internet a few days ago and I really like it. But i have 1 question that I need some help with. I want to be able to rotate a character in position to my mouse. From a top down perspective, I want my character to always face the mouse. Is there some kind of function that can do this? Or is there a workaround?

Thanks, get back to me when you can! :awesome:

Re: Rotating Character

Posted: Fri Sep 17, 2010 9:57 pm
by thelinx
You'll want something like this:

Code: Select all

-- replace object_x and object_y with the x and y of the player
-- replace sprite_width and sprite_height with the actual values
local angle = math.atan2(love.mouse.getY()-object_y, love.mouse.getX()-object_x)
love.graphics.draw(sprite, object_x, object_y, angle, 1, 1, sprite_width/2, sprite_height/2)
Good luck!

Re: Rotating Character

Posted: Fri Sep 17, 2010 9:59 pm
by Robin
Welcome!

What you want to do requires some basic knowledge of trigonometry, but all-together it's rather simple. I'll provide some pointers, I hope you can do it yourself then (because you learn better that way than pre-written code ;)):

love.mouse.getPosition()
love.graphics.draw() (especially, take look at the arguments)
math.atan2()

If this doesn't help you, you can ask for more information. :)


... or you could take thelinx's example.

http://en.wikipedia.org/wiki/%27s#Menti ... yle_guides

Re: Rotating Character

Posted: Fri Sep 17, 2010 10:17 pm
by thelinx
:3

Re: Rotating Character

Posted: Fri Sep 17, 2010 11:36 pm
by teh n00bian
Didn't work. :? I'm pretty sure its something I'm doing. Can you take a look at my code and see what I'm doing wrong? :oops:

EDIT: I knew it had something to do with trig!

Re: Rotating Character

Posted: Sat Sep 18, 2010 7:06 am
by Robin
What if you replace

Code: Select all

	mousex = love.mouse.getX( )
	mousey = love.mouse.getY( )
	
	local angle = math.atan2(mousey - playery, mousex - playerx)
with

Code: Select all

	local mousex = love.mouse.getX( )
	local mousey = love.mouse.getY( )
	
	angle = math.atan2(mousey - playery, mousex - playerx)
?

Re: Rotating Character

Posted: Sat Sep 18, 2010 7:07 am
by kikito
I remember having answered this already. Let me search ...

(searches through his posts)

Yes, here it is: http://love2d.org/forums/viewtopic.php?f=4&t=1396. But unfortunately the attached file is lost and I don't have it any more (I wonder if the forum admins can do anything on that regard?) However, the explanation of why and how it works is still there.

It was difficult to find since the forum search ignores "look" and "mouse" as it thinks they are "too common".

Re: Rotating Character

Posted: Sat Sep 18, 2010 2:11 pm
by teh n00bian
All that I needed to do was remove the local? I feel kinda stupid now.
Another problem though, He is facing exactly 90 degrees to the left of the mouse. I'm pretty sure that its a graphics thing though.
Thanks for all of you help!!
EDIT: rotated his 90 degrees clockwise as an image and it works now.