Page 1 of 3

Pointing an image

Posted: Sun Apr 17, 2011 3:54 pm
by Anxiety
How can i point an image to where the mouse is?

'Nuff said.

Thanks!

Re: Pointing an image

Posted: Sun Apr 17, 2011 3:57 pm
by Robin
love.mouse.getPosition gives you the position of the mouse, you can use that to draw an image at that location. Note that the image will always lag behind the cursor somewhat, that's just the way it is.

Re: Pointing an image

Posted: Sun Apr 17, 2011 3:58 pm
by Anxiety
Woops, i meant to not place but POINT the image where the mouse is using the rotation.

Sorry about that.

Re: Pointing an image

Posted: Sun Apr 17, 2011 4:12 pm
by Anxiety
I was thinking of something like calculating the radian direction between the image and the mouse and i know some versions of Lua use (pos1-pos2).unit but is that valid in Löve?

(Thats one long sentence!)

Re: Pointing an image

Posted: Sun Apr 17, 2011 4:46 pm
by Taehl

Code: Select all

-- Returns the angle between two points.
function math.getAngle(x1,y1, x2,y2) return math.atan2(x2-x1, y2-y1) end
Plug the image's and the mouse's coordinates into there, and it'll give you the rotation you want.

Re: Pointing an image

Posted: Sun Apr 17, 2011 4:58 pm
by Robin
Taehl wrote:

Code: Select all

-- Returns the angle between two points.
function math.getAngle(x1,y1, x2,y2) return math.atan2(x2-x1, y2-y1) end
Plug the image's and the mouse's coordinates into there, and it'll give you the rotation you want.
Something like that, but note that you might have to play with it a bit to get it right. Things you might need to compensate for:
  • In LÖVE, the y value increases towards the bottom of the screen. In mathematical terms, y would decrease.
  • LÖVE considers upright to be rotation = 0, while mathematics see 0 as pointing to the right.

Re: Pointing an image

Posted: Sun Apr 17, 2011 5:00 pm
by Anxiety
Thanks!

Why does people always give me code snippets in dont understand how to use?

Re: Pointing an image

Posted: Sun Apr 17, 2011 5:08 pm
by Anxiety
Ohwait now i got it working... Theres just one problem. The character i use is normally pointing up. But when i move the mouse around, the character looks in odd directions.

Re: Pointing an image

Posted: Sun Apr 17, 2011 6:20 pm
by Jasoco
Anxiety wrote:Ohwait now i got it working... Theres just one problem. The character i use is normally pointing up. But when i move the mouse around, the character looks in odd directions.
Then subtract the amount of rotation. Like since 0 is actually to the right, you'll need to either add or subtract 90. I forget which. I used this method yesterday to get an enemy to move towards the player and will use it for aiming at the player for static enemies. You just need to play around.

Re: Pointing an image

Posted: Sun Apr 17, 2011 6:41 pm
by Taehl
You mean, math.pi*.5, right? Rotating by 90 radians would be like rotating by 5156 degrees...