Rotation of image pointing towards mouse?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
BarnD
Prole
Posts: 49
Joined: Wed Jun 16, 2010 1:23 pm
Location: Australia
Contact:

Rotation of image pointing towards mouse?

Post by BarnD »

I am currently having a blank moment, I'm trying to get an image, lets say the image is a cube and its in the middle of the screen, how would i go about making one face of the cube angle towards the cursor and rotate the cube to where the cursor is.

No need to write out a whole script, just say a good way of going about doing it, thanks. :)
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Rotation of image pointing towards mouse?

Post by TechnoCat »

User avatar
BarnD
Prole
Posts: 49
Joined: Wed Jun 16, 2010 1:23 pm
Location: Australia
Contact:

Re: Rotation of image pointing towards mouse?

Post by BarnD »

Woo, thanks.

Code: Select all

playerR = math.atan2(mouseY-playerY, mouseX-playerX)
That's basically what i have done to get it working for now.. :)
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Rotation of image pointing towards mouse?

Post by BlackBulletIV »

I usually wrap that into a function like this:

Code: Select all

function math.angle(x1, y1, x2, y2)
  return math.atan2(y2 - y1, x2 - x1)
end
It makes things easier to remember.
User avatar
BarnD
Prole
Posts: 49
Joined: Wed Jun 16, 2010 1:23 pm
Location: Australia
Contact:

Re: Rotation of image pointing towards mouse?

Post by BarnD »

BlackBulletIV wrote:I usually wrap that into a functions,
It makes things easier to remember.
Yeah, also makes the code much cleaner. :D
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Rotation of image pointing towards mouse?

Post by BlackBulletIV »

Indeed.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Rotation of image pointing towards mouse?

Post by Robin »

I would suggest not putting it in the math library, as that library is standard but that function is not. People reading your code would expect math.* to be one of the standard library functions.
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Rotation of image pointing towards mouse?

Post by Jasoco »

I just name my function getAngle(). Or distanceFrom() for distance.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Rotation of image pointing towards mouse?

Post by BlackBulletIV »

Robin wrote:I would suggest not putting it in the math library, as that library is standard but that function is not. People reading your code would expect math.* to be one of the standard library functions.
Each to their own I guess, but I find it nicer looking to put it in with the math library. It wouldn't take long for someone to figure out it's not part of the math library anyway.
User avatar
BarnD
Prole
Posts: 49
Joined: Wed Jun 16, 2010 1:23 pm
Location: Australia
Contact:

Re: Rotation of image pointing towards mouse?

Post by BarnD »

Robin wrote:People reading your code would expect math.* to be one of the standard library functions.
Unless you want them to be confused :crazy:
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests