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!
Rotating Character
Re: Rotating Character
You'll want something like this:
Good luck!
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)
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Rotating Character
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
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
Last edited by Robin on Fri Sep 17, 2010 10:23 pm, edited 2 times in total.
Help us help you: attach a .love.
-
- Prole
- Posts: 3
- Joined: Thu Sep 16, 2010 6:21 pm
Re: Rotating Character
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?
EDIT: I knew it had something to do with trig!
EDIT: I knew it had something to do with trig!
- Attachments
-
- main.lua
- (1.21 KiB) Downloaded 173 times
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Rotating Character
What if you replace
with
?
Code: Select all
mousex = love.mouse.getX( )
mousey = love.mouse.getY( )
local angle = math.atan2(mousey - playery, mousex - playerx)
Code: Select all
local mousex = love.mouse.getX( )
local mousey = love.mouse.getY( )
angle = math.atan2(mousey - playery, mousex - playerx)
Last edited by Robin on Sat Sep 18, 2010 7:08 am, edited 1 time in total.
Help us help you: attach a .love.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Rotating Character
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".
(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".
When I write def I mean function.
-
- Prole
- Posts: 3
- Joined: Thu Sep 16, 2010 6:21 pm
Re: Rotating Character
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.
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.
Who is online
Users browsing this forum: No registered users and 1 guest