Newbie question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
mediakitchen
Prole
Posts: 8
Joined: Fri Oct 08, 2010 5:39 am

Newbie question

Post by mediakitchen »

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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Newbie question

Post by bartbes »

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.
mediakitchen
Prole
Posts: 8
Joined: Fri Oct 08, 2010 5:39 am

Re: Newbie question

Post by mediakitchen »

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.


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

User avatar
walesmd
Prole
Posts: 22
Joined: Fri Oct 08, 2010 3:11 am
Location: Augusta, GA
Contact:

Re: Newbie question

Post by walesmd »

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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Newbie question

Post by bartbes »

Which creates a bounding box, i.e. not a circle.
User avatar
walesmd
Prole
Posts: 22
Joined: Fri Oct 08, 2010 3:11 am
Location: Augusta, GA
Contact:

Re: Newbie question

Post by walesmd »

Ah, good catch! Still much to learn :)
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Newbie question

Post by vrld »

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
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Newbie question

Post by bartbes »

Well, that is pretty much the code mediakitchen posted, just formatted differently.
mediakitchen
Prole
Posts: 8
Joined: Fri Oct 08, 2010 5:39 am

Re: Newbie question

Post by mediakitchen »

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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Newbie question

Post by bartbes »

Distance between the centers <= radius of circle 1 + radius of circle 2.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests