Need help with rotating on my top down shooter project

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
AtomCastDev
Prole
Posts: 37
Joined: Fri Aug 02, 2013 1:44 pm

Need help with rotating on my top down shooter project

Post by AtomCastDev »

I have been working on a top down zombie shooter for quite a while.It has a few features into it like a cool futuristic yet post apocalyptic environment, cool gun sprites and a mouse aiming system.
But what has gotten me stumped for the last 2 days is a code to make the character rotate into the direction my mouse is, if that is too complicated (I am a newbie at love2-D mouse code) then if possible can it just be so that the mouse will aim somewhere and shoot using the lmb,when the lmb is pressed it will also make the character will rotate to the direction the mouse button was pressed.
can anyone help a stumped newbie like me?
Sharing the one world

Also check out my friend's game!
http://love2d.org/forums/viewtopic.php?f=5&t=43276
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Need help with rotating on my top down shooter project

Post by raidho36 »

You would need to use some trigonometry, but it's quite easy. All you need is math.atan2 ( y, x ). It would compute a radian angle between coordinates origin and X-Y point. To compute an angle between two points, for each axis you subtract coordinates from target point to origin point:

Code: Select all

angle = -math.atan2 ( mouse.y - player.y, mouse.x - player.x )
Note that this function has first argument Y and second X, and since LÖVE have inverted Y-axis and therefore angles go clockwise rather than counter-clockwise, you would need negative of the returned angle.

I assume you've already managed to get it shooting with the mouse button, so all it takes now is to use the function described.
AtomCastDev
Prole
Posts: 37
Joined: Fri Aug 02, 2013 1:44 pm

Re: Need help with rotating on my top down shooter project

Post by AtomCastDev »

I dont quite understand, can you show a demonstration in love?
Sharing the one world

Also check out my friend's game!
http://love2d.org/forums/viewtopic.php?f=5&t=43276
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Need help with rotating on my top down shooter project

Post by raidho36 »

Kinda like this. Note the mousepressed function.
Attachments
datafile.love
(1.01 KiB) Downloaded 161 times
AtomCastDev
Prole
Posts: 37
Joined: Fri Aug 02, 2013 1:44 pm

Re: Need help with rotating on my top down shooter project

Post by AtomCastDev »

Do you think it is possible to apply your code into my project without changing anything?

http://www.mediafire.com/download/kbc6d ... le(2).love
Sharing the one world

Also check out my friend's game!
http://love2d.org/forums/viewtopic.php?f=5&t=43276
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Need help with rotating on my top down shooter project

Post by raidho36 »

It's a one-liner function, of course it's possible.

Also, posting multimegabyte project full of unrelated to the problem stuff and with probably complex structure with no explanations given and expecting people to do everything for you isn't exactly following online etiquette of asking for help IMO.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests