Page 1 of 1
Rotation of image pointing towards mouse?
Posted: Mon Apr 18, 2011 4:16 am
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.
Re: Rotation of image pointing towards mouse?
Posted: Mon Apr 18, 2011 4:33 am
by TechnoCat
Re: Rotation of image pointing towards mouse?
Posted: Mon Apr 18, 2011 4:45 am
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..
Re: Rotation of image pointing towards mouse?
Posted: Mon Apr 18, 2011 6:22 am
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.
Re: Rotation of image pointing towards mouse?
Posted: Mon Apr 18, 2011 6:48 am
by BarnD
BlackBulletIV wrote:I usually wrap that into a functions,
It makes things easier to remember.
Yeah, also makes the code much cleaner.
Re: Rotation of image pointing towards mouse?
Posted: Mon Apr 18, 2011 6:52 am
by BlackBulletIV
Indeed.
Re: Rotation of image pointing towards mouse?
Posted: Mon Apr 18, 2011 7:03 am
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.
Re: Rotation of image pointing towards mouse?
Posted: Mon Apr 18, 2011 7:09 am
by Jasoco
I just name my function getAngle(). Or distanceFrom() for distance.
Re: Rotation of image pointing towards mouse?
Posted: Mon Apr 18, 2011 7:27 am
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.
Re: Rotation of image pointing towards mouse?
Posted: Mon Apr 18, 2011 7:32 am
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