## Pointing an image

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

### Pointing an image

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

'Nuff said.

Thanks!
I can't come up with a good signature!

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

### Re: Pointing an image

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.

Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

### Re: Pointing an image

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

I can't come up with a good signature!

Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

### Re: Pointing an image

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!

Taehl
Dreaming in associative arrays
Posts: 1024
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

### Re: Pointing an image

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+.

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

### Re: Pointing an image

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.

Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

### Re: Pointing an image

Thanks!

Why does people always give me code snippets in dont understand how to use?
I can't come up with a good signature!

Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

### Re: Pointing an image

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!

Jasoco
Inner party member
Posts: 3651
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

### Re: Pointing an image

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.

Taehl
Dreaming in associative arrays
Posts: 1024
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

### Re: Pointing an image

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+.

### Who is online

Users browsing this forum: No registered users and 25 guests