love.graphics.polygon.isInside(x, y)

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
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

love.graphics.polygon.isInside(x, y)

Post by spir »

What about adding a function to each shape provided by Löve to check whether a given (x,y) is inside? Would be tremenduously cool ! ;)

Denis
... la vita e estrany ...
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: love.graphics.polygon.isInside(x, y)

Post by Robin »

You mean like Shape:testPoint? ;)
Help us help you: attach a .love.
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: love.graphics.polygon.isInside(x, y)

Post by Xgoff »

Robin wrote:You mean like Shape:testPoint? ;)
There's a bug in 0.8.0 preventing this function from working.
well that's a downer (not to mention you have to drag love.physics into it)
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: love.graphics.polygon.isInside(x, y)

Post by dreadkillz »

Why not use hardon collider shapes? It's pretty good.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: love.graphics.polygon.isInside(x, y)

Post by kikito »

love.graphics is called love.graphics because it contains functions that draw stuff - they put pixels on the screen (or a canvas). Yes, there are some query functions, such as "what is the current color", but again their "reason to exist" is to enable the user to put pixels somewhere.

Although 'polygon.isInside' might seem related with "polygons", its end is not necessarily related with "drawing pixels". It's "resolving a geometric problem".

If LÖVE provided such a function, it should be inside a package called 'love.geometry' or something similar. But not inside love.graphics.

That said, it is actually possible to solve this problem (in a horribly inefficient way) using the graphics lib. But it would be a programming amusement, not something that you could consider seriously for a game. It basically consists on:
  • Creating a canvas with the appropiate size, filled with white
  • Drawing the polygon on the canvas, with a different color (i.e. red)
  • Getting the imagedata of the canvas
  • If point x,y in the imageData is white, then the point is not inside the polygon. Otherwise, it is inside.
But yeah, use either love.physics or HardonCollider. You will be much better off.
When I write def I mean function.
User avatar
Ubermann
Party member
Posts: 146
Joined: Mon Nov 05, 2012 4:00 pm

Re: love.graphics.polygon.isInside(x, y)

Post by Ubermann »

kikito wrote:love.graphics is called love.graphics because it contains functions that draw stuff - they put pixels on the screen (or a canvas). Yes, there are some query functions, such as "what is the current color", but again their "reason to exist" is to enable the user to put pixels somewhere.

Although 'polygon.isInside' might seem related with "polygons", its end is not necessarily related with "drawing pixels". It's "resolving a geometric problem".

If LÖVE provided such a function, it should be inside a package called 'love.geometry' or something similar. But not inside love.graphics.

That said, it is actually possible to solve this problem (in a horribly inefficient way) using the graphics lib. But it would be a programming amusement, not something that you could consider seriously for a game. It basically consists on:
  • Creating a canvas with the appropiate size, filled with white
  • Drawing the polygon on the canvas, with a different color (i.e. red)
  • Getting the imagedata of the canvas
  • If point x,y in the imageData is white, then the point is not inside the polygon. Otherwise, it is inside.
But yeah, use either love.physics or HardonCollider. You will be much better off.
Alternatively to that pixel checking system, you can also check if the mouse is between ShapeX and ShapeX + shapeWidth; and ShapeY and ShapeY + ShapeHeight.

The only problem with your method could be that if the shape has different colors such as a photo, where white may be used already as part of the photo itself, or you can't predict which color/s could be unused for you to check against it.
arundel
Prole
Posts: 31
Joined: Tue Sep 21, 2010 8:49 pm
Location: The Netherlands

Re: love.graphics.polygon.isInside(x, y)

Post by arundel »

To find a point in a polygon (which can be build up from triangles) you could use:

Code: Select all

dot_product=function(a, b)
local ret = 0
for i = 1, #a do
ret = ret + a[i] * b[i]
end
return ret
end

point_in_triangle=function(a,b,c,p)
a[1],a[2],a[3],b[1],b[2],b[3],p[1],p[2],p[3]=a[1] or 0,a[2] or 0,a[3] or 0,b[1] or 0,b[2] or 0,b[3] or 0,c[1] or 0,c[2] or 0,c[3] or 0,p[1] or 0,p[2] or 0,p[3] or 0
local dot=dot_product
local v0,v1,v2={c[1]-a[1],c[2]-a[2],c[3]-a[3]},{b[1]-a[1],b[2]-a[2],b[3]-a[3]},{p[1]-a[1],p[2]-a[2],p[3]-a[3]}
local dot00,dot01,dot02,dot11,dot12=dot(v0, v0),dot(v0, v1),dot(v0, v2),dot(v1, v1),dot(v1, v2)
local invDenom= 1 / (dot00 * dot11 - dot01 * dot01)
local u = (dot11 * dot02 - dot01 * dot12) * invDenom
local v = (dot00 * dot12 - dot01 * dot02) * invDenom
return (u >= 0) and (v >= 0) and (u + v < 1)
end
I found the theory on a site once and converted the code into lua. Paramater a, b and c are the vertices of a triangle and p is the point that you want to check wether it's inside.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 216 guests