Page 1 of 2

Rotating image to follow mouse.

Posted: Mon Apr 06, 2009 2:04 pm
by zac199
Hi :)

I was wondering how I could get my image to follow my mouse.

Like have the front of the image always looking at the mouse on the screen.

Just a quick question :) thanks ahead of time.

Re: Rotating image to follow mouse.

Posted: Mon Apr 06, 2009 2:20 pm
by mike
You mean that you want a fixed image to rotate to always be "pointing" at the mouse?

Re: Rotating image to follow mouse.

Posted: Mon Apr 06, 2009 2:21 pm
by zac199
Sort of... I have a image that moves with W,A,S,D, and I want the... err front so to say to always look at the mouse.

If you dont know what i mean, Im making a top down shooter, and I need to be able to aim haha.

Re: Rotating image to follow mouse.

Posted: Mon Apr 06, 2009 3:13 pm
by mikembley
I guess you mean like a top down shooter huh?

I was gonna throw my functions your way and let you figure it out, But im feeling kind today so i made you a demo instead :)

Have fun

-Forgot to mention: Controls are Arrow keys to move character and mouse click for flamethrower and i apologise for using a jpg, as i stole the image from Google images :)

Re: Rotating image to follow mouse.

Posted: Mon Apr 06, 2009 3:27 pm
by bartbes
@mikembley: Nice example, though there are some things that could be done better (game-wise, but you're not making a game anyway).

Re: Rotating image to follow mouse.

Posted: Mon Apr 06, 2009 3:33 pm
by zac199
Haha that is a pretty nifty example, and yes thats exactly what I want to do :P. But mind telling me how to look at the script haha

Re: Rotating image to follow mouse.

Posted: Mon Apr 06, 2009 3:53 pm
by bartbes
A .love is a renamed zip, so you can just open it with your zip program, or you can rename it and then open it.

Re: Rotating image to follow mouse.

Posted: Mon Apr 06, 2009 3:59 pm
by zac199
Ah yes. Thanks haha. Now I just need to find were the code is that rotates the img.

EDIT:
would this be it

Code: Select all

player.direction = point_direction( player.x, player.y, love.mouse.getX(), love.mouse.getY() )

Re: Rotating image to follow mouse.

Posted: Mon Apr 06, 2009 6:15 pm
by mikembley
That indeed would be it. point_direction is a function that returns the angle between two points

Re: Rotating image to follow mouse.

Posted: Tue Apr 07, 2009 12:09 am
by zac199
So would I use this same method to make the guy shoot? haha. Like I want a actual bullet that travels in a fixed direction, not your flamethrower example :P