Hi there
Just discovered Love a few hours ago and am having a play.
Anyway I wanted to make a simple penalty shoot out game and would like to detect when the user clicks on the ball.
Is there a way to detect a mouse click on a particular image or do I have to use the position of the mouse and the position of the image and do some maths to work out if the mouse is within the image bounds??
Thanks
Paul
Newbie question
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Newbie question
Yeah, an image is not an object to love, it's just an image, so it has no properties except for being drawable (and having size, that kind of stuff), you will have to do the maths yourself, they shouldn't be too hard though. You can just check whether the distance from the mouse to ball center is less than (or equal to) the radius.
-
- Prole
- Posts: 8
- Joined: Fri Oct 08, 2010 5:39 am
Re: Newbie question
Wow thanks for the swift reply.
I think I have sussed it. I have another question about how the basic updating works but will ask that in a separate thread.
I think I have sussed it. I have another question about how the basic updating works but will ask that in a separate thread.
Code: Select all
function love.mousepressed(x, y, button)
-- Calculate if mouse over ball
-- i.e distance squared less than radius squared
local ballRadiusSquared = 900 -- 30 x 30
if button == 'l' then
local distanceSquared = distanceSquaredFrom(x,y,ballx + 30,bally + 30)
if distanceSquared <= ballRadiusSquared then
arrowx = ballx + 30 -- move arrow to center of ball clicked
arrowy = bally + 30 - 7
end
end
end
function distanceSquaredFrom(x1,y1,x2,y2)
return (x2 - x1) ^ 2 + (y2 - y1) ^ 2
end
Re: Newbie question
Code: Select all
function love.mousepressed(x, y, button)
if button == 1 then
if math.abs(x - ball.x) <= ball.radius and math.abs(y - ball.y) <= ball.radius then
print("Ball clicked")
end
end
end
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Newbie question
Which creates a bounding box, i.e. not a circle.
Re: Newbie question
Ah, good catch! Still much to learn
Re: Newbie question
Only one little change to make it a cricle:
Code: Select all
function love.mousepressed(x, y, button)
if button == 1 then
if (x - ball.x) ^ 2 + (y - ball.y) ^ 2 <= ball.radius ^ 2 then
print("Ball clicked")
end
end
end
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Newbie question
Well, that is pretty much the code mediakitchen posted, just formatted differently.
-
- Prole
- Posts: 8
- Joined: Fri Oct 08, 2010 5:39 am
Re: Newbie question
Thanks guys - great to see such a helpful bunch here especially as I have another question relating to ball to ball collision. I have spent the entire day trying to work this out and I am still getting erratic behaviour:(
Will post shortly. Just going to try one more thing.
Will post shortly. Just going to try one more thing.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Newbie question
Distance between the centers <= radius of circle 1 + radius of circle 2.
Who is online
Users browsing this forum: Google [Bot] and 5 guests