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
love.graphics.polygon.isInside(x, y)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
love.graphics.polygon.isInside(x, y)
... la vita e estrany ...
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: love.graphics.polygon.isInside(x, y)
You mean like Shape:testPoint?
Help us help you: attach a .love.
Re: love.graphics.polygon.isInside(x, y)
Robin wrote:You mean like Shape:testPoint?
well that's a downer (not to mention you have to drag love.physics into it)There's a bug in 0.8.0 preventing this function from working.
- dreadkillz
- Party member
- Posts: 223
- Joined: Sun Mar 04, 2012 2:04 pm
- Location: USA
Re: love.graphics.polygon.isInside(x, y)
Why not use hardon collider shapes? It's pretty good.
- 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)
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:
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.
When I write def I mean function.
Re: love.graphics.polygon.isInside(x, y)
Alternatively to that pixel checking system, you can also check if the mouse is between ShapeX and ShapeX + shapeWidth; and ShapeY and ShapeY + ShapeHeight.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:But yeah, use either love.physics or HardonCollider. You will be much better off.
- 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.
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.
Re: love.graphics.polygon.isInside(x, y)
To find a point in a polygon (which can be build up from triangles) you could use:
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.
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
Who is online
Users browsing this forum: Google [Bot] and 216 guests