Detecting weather the mouse is in a `quad`

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.
Post Reply
User avatar
schme16
Party member
Posts: 127
Joined: Thu Oct 02, 2008 2:46 am

Detecting weather the mouse is in a `quad`

Post by schme16 »

I'm trying to figure out how to detect weather or not the mouse is within the bounds of a `quad`

Can anyone help me figure this out... no doubt it will be something simple that will make me feel like a retard.


any and all help is met with thanks!
My Development Diary - http://shanegadsby.info
User avatar
Tabasco
Citizen
Posts: 72
Joined: Tue Dec 02, 2008 5:04 pm

Re: Detecting weather the mouse is in a `quad`

Post by Tabasco »

Code: Select all

function boxHit(x, y, left, top, width, height)
   if x <= left + width and x >= left and y <= top + height and y >= top then
      return true
   else
      return false
   end
end

function circleHit(x,y, cx, cy, r)
   local xd = math.abs(x - cx)
   local yd = math.abs(y - cy)

   local d = math.sqrt((xd * xd) + (yd * yd))

   if d <= (r/2) then
      return true
   else
      return false
   end
end
User avatar
schme16
Party member
Posts: 127
Joined: Thu Oct 02, 2008 2:46 am

Re: Detecting weather the mouse is in a `quad`

Post by schme16 »

Thanks for the reply; but the `quad` doesn't use height and width attributes.
It uses; x1 y1, x2 y2, x3 y3, x4 y4 as it's bounding limits. My application really can't use the rectangle function and the circle is completely out.

I'll see if I can adapt the function you referenced so thank you for the help; but I'll need to keep looking.
My Development Diary - http://shanegadsby.info
User avatar
S-Rave
Prole
Posts: 39
Joined: Wed Feb 25, 2009 4:41 pm

Re: Detecting weather the mouse is in a `quad`

Post by S-Rave »

Maybe http://www.gamedev.net/reference/progra ... /page2.asp ?
I know a quad isn't a rectangle but maybe it'll point you in the right direction? :)
srejv
User avatar
qubodup
Inner party member
Posts: 775
Joined: Sat Jun 21, 2008 9:21 pm
Location: Berlin, Germany
Contact:

Re: Detecting weather the mouse is in a `quad`

Post by qubodup »

I use physics.newPolygonShape and then PolygonShape:testPoint to check whether or not the mouse x/y values are inside the poly.
lg.newImage("cat.png") -- made possible by lg = love.graphics
-- Don't force fullscreen (it frustrates those who want to try your game real quick) -- Develop for 1280x720 (so people can make HD videos)
User avatar
Tabasco
Citizen
Posts: 72
Joined: Tue Dec 02, 2008 5:04 pm

Re: Detecting weather the mouse is in a `quad`

Post by Tabasco »

I completely misread your question, sorry about that.

In my 3d work I use the Moller / Trumbore ray/triangle intersection algorithm.
The attached file has a full demo that I whipped up inside my basic framework.

Here's a lua port for love:

Code: Select all

function quadHit(x, y, x1, y1, x2, y2, x3, y3, x4, y4)
   local tr1_1 = Vector.new(x1, y1, 0)
   local tr1_2 = Vector.new(x2, y2, 0)
   local tr1_3 = Vector.new(x3, y3, 0)

   local tr2_1 = Vector.new(x1, y1, 0)
   local tr2_2 = Vector.new(x3, y3, 0)
   local tr2_3 = Vector.new(x4, y4, 0)

   if triHit(x, y, tr1_1, tr1_2, tr1_3) then return true end
   if triHit(x, y, tr2_1, tr2_2, tr2_3) then return true end
   return false
end

function triHit(x, y, v1, v2, v3)
   local edge1 = Vector.new(0, 0, 0)
   local edge2 = Vector.new(0, 0, 0)
   local pvec = Vector.new(0, 0, 0)
   local tvec = Vector.new(0, 0, 0)
   local qvec = Vector.new(0, 0, 0)
   local dir = Vector.new(0, 0, 1)
   local orig = Vector.new(x, y, -1)
   local det, inv_det
   local t, u, v

   edge1.x = (v2.x - v1.x)
   edge1.y = (v2.y - v1.y)
   edge1.z = (v2.z - v1.z)

   edge2.x = (v3.x - v1.x)
   edge2.y = (v3.y - v1.y)
   edge2.z = (v3.z - v1.z)

   pvec = vector_cross(dir, edge2)
   det = vector_dot(edge1, pvec)
   if det < 0.000001 then return false end

   tvec.x = (orig.x - v1.x)
   tvec.y = (orig.y - v1.y)
   tvec.z = (orig.z - v1.z)

   u = vector_dot(tvec, pvec)
   if u < 0.0 or u > det then return false end

   qvec = vector_cross(tvec, edge1)

   v = vector_dot(dir, qvec)
   if v < 0.0 or  u + v > det then return false end

   t = vector_dot(edge2, qvec)
   inv_det = 1.0 / det
   t = t * inv_det
   u = u * inv_det
   v = v * inv_det

   return true
end

function vector_cross(v1, v2)
   local rv = Vector.new(0, 0, 0);

   rv.x = v1.y * v2.z - v1.z * v2.y
   rv.y = v1.z * v2.x - v1.x * v2.z
   rv.z = v1.x * v2.y - v1.y * v2.x

   return rv
end

function vector_dot(v1, v2)
 local rv = (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z)

 return rv
end

Vector = {}
Vector.__index = Vector

function Vector.new(x, y, z)
   local vec = {}
   setmetatable(vec, Vector)

   vec.x = x
   vec.y = y
   vec.z = z

   return vec
end
Attachments
trihit.love
(5.6 KiB) Downloaded 119 times
User avatar
Tabasco
Citizen
Posts: 72
Joined: Tue Dec 02, 2008 5:04 pm

Re: Detecting weather the mouse is in a `quad`

Post by Tabasco »

This should have much less overhead since we're not trying to turn the scene into 3d first.

Code: Select all

function draw()
   if(quadHit(mx, my, 200, 200, 150, 150, 100, 200, 150, 250)) then love.graphics.setColor(red)
   else love.graphics.setColor(white) end

   love.graphics.quad(love.draw_fill, 200, 200, 150, 150, 100, 200, 150, 250)
end

function quadHit(x, y, x1, y1, x2, y2, x3, y3, x4, y4)

   if triHit(x, y, x1, y1, x2, y2, x3, y3) then return true end
   if triHit(x, y, x1, y1, x3, y3, x4, y4) then return true end

   return false
end

function triHit(px, py, x1, y1, x2, y2, x3, y3)
   local or1, or2, or3

   or1 = orientation(x1, y1, x2, y2, px, py)
   or2 = orientation(x2, y2, x3, y3, px, py)
   or3 = orientation(x3, y3, x1, y1, px, py)

   if or1 == or2 and or2 == or3 then return true else return false end
end

function orientation(x1, y1, x2, y2, px, py)
   local orin

   orin = (x2 - x1) * (py - y1) - (px - x1) * (y2 - y1)

   if orin > 0 then return 1 end
   if orin < 0 then return -1 end
   return -1 --point is on a line
end
User avatar
schme16
Party member
Posts: 127
Joined: Thu Oct 02, 2008 2:46 am

Re: Detecting weather the mouse is in a `quad`

Post by schme16 »

Guys I cannot thank you all enough!!!

That has solved my problem 100%!

Please take these boots of +1 kickass-ness as thanks!


Image
My Development Diary - http://shanegadsby.info
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 49 guests