Pointing an image

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Pointing an image

Post by Anxiety »

How can i point an image to where the mouse is?

'Nuff said.

Thanks!
I can't come up with a good signature!
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Pointing an image

Post 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.
Help us help you: attach a .love.
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Re: Pointing an image

Post by Anxiety »

Woops, i meant to not place but POINT the image where the mouse is using the rotation.

Sorry about that.
I can't come up with a good signature!
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Re: Pointing an image

Post 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!)
I can't come up with a good signature!
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Pointing an image

Post 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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Pointing an image

Post 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.
Help us help you: attach a .love.
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Re: Pointing an image

Post by Anxiety »

Thanks!

Why does people always give me code snippets in dont understand how to use?
I can't come up with a good signature!
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Re: Pointing an image

Post 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.
I can't come up with a good signature!
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Pointing an image

Post 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.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Pointing an image

Post by Taehl »

You mean, math.pi*.5, right? Rotating by 90 radians would be like rotating by 5156 degrees...
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests