Page 1 of 1

Check if object is touched by mouse?

Posted: Wed Jun 19, 2013 5:55 am
by PloXyZeRO
Is there a function that returns a boolean if it is "touched" by the mouse? The only way I can think of doing this is by checking whether the mouse position is within the same space that the object is in by subtracting the height/width of the object from the origin...but that would only work with squares and rectangles. If I wanted to do this with a circle it wouldn't exactly work. Any help? Thanks!

Edit: On a somewhat related note, is there an easy way to make an object "point" towards the mouse? The only thing I can think of is calculating the angle of the mouse from the center of the screen by getting the position of the mouse, center of the screen, and using a sin/cosine/tangent function or something, then apply that to the object's orientation. Is there some other function that does this easily?

Re: Check if object is touched by mouse?

Posted: Wed Jun 19, 2013 6:26 am
by Plu
Not really, no. If you need really exact collissions for some reason, that'll be hard. It might work to create some bodies and shapes using Hardon Collider and then assigning one to the tip of the mouse, and check with collisions with that, but it'll still be somewhat rough. If you need them pixel perfect, the only thing I can come up with is making another picture of the object in a single, solid color, drawing that to an empty canvas during each draw() step, and then checking the mouse coördinates for the pixel color of the canvas. That'd get you very accurate collisions but a pretty big drain on resources probably.

As to the pointing to the mouse, I think you should be able to use this:
local angle = math.atan2(y2 - y1, x2 - x1)

Put the coördinates of the center of the object in one and the location of the mouse in the other. I'm not sure which is which because I'm terrible with angles myself :|

Re: Check if object is touched by mouse?

Posted: Wed Jun 19, 2013 6:34 am
by substitute541
For checking if the cursor touches the circle, try this snippet: https://www.love2d.org/wiki/DistanceBasedCollision

Re: Check if object is touched by mouse?

Posted: Wed Jun 19, 2013 6:51 am
by PloXyZeRO
Oh yeah, I could just check for the distance! Thanks

So it seems like it would be pretty hard for more complex shapes

The reason I was asking is I wanted to make a simple mouse maze gteleporting game with teleporting since I found the love.mouse.setPosition function!

Edit: The angle function worked too, thanks! I completely forgot arctan
This is what I have

Code: Select all

angle = math.atan2(origin_y-mouse_y, origin_x-mouse_x) - (pi/2)
origin variables are the center of the screen, mouse variables are the mouse coordinates, and pi is math.pi
For some reason the image was 90 degrees too far clockwise so I subtracted that from the arctan function so that it is oriented correctly

Re: Check if object is touched by mouse?

Posted: Wed Jun 19, 2013 7:22 am
by Plu
If it's oriented too far, that's probably a case of not having the base image in the correct rotation. If you maze consists mostly of rectangular blocks, you could just use a whole bunch of rectangle based collisions, which are fairly simple to do. Or you could still use Hardon Collider.

If it's an irregular but non-changing maze, you can just draw it to a canvas once and use that as a hardness map. I was assuming moving objects, but if they're stationary it's actually fairly easy. Especially if it's a single picture.

Re: Check if object is touched by mouse?

Posted: Wed Jun 19, 2013 7:37 am
by PloXyZeRO
Okay, I'll try drawing everything to a canvas (although I don't know how to do that yet! I'll check the wiki)

I also don't know how to use the hardon collider....in fact I barely know how to do anything in LÖVE! I'll figure more things out eventually..thanks for the help!