Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to get Sprite:hitTestPoint to ignore transparent areas of image — Gideros Forum

How to get Sprite:hitTestPoint to ignore transparent areas of image

chipster123chipster123 Member
edited December 2011 in General questions
Is there any way to get the Sprite:hitTestPoint to be aware of the transparent areas of the image so the return value can be 'false' ?
Currently, I can grab Sprites by their transparent areas, which looks kind of bad.

Comments

  • I knew there was something I had meant to mention. I would also like the ability to ignore transparent areas of images.

    M
    What would you do for your other half?

    http://www.sharksoupstudios.com
  • atilimatilim Maintainer
    Currently no. I've added this to the requests list. (But per-pixel testing can be tricky because you should store one texture for GPU and one texture for CPU and this means twice as much memory consumption as before)

    But there may be some workarounds such as representing the bitmap with a coarse polygon and doing a "point in polygon" test. I can post a point in polygon test code in Lua here.
  • atilimatilim Maintainer
    edited December 2011
    hi chipster123,

    sorry for the late reply. here is the function:
    function pointInPolygon(vertices, x, y)
    	local intersectionCount = 0
     
    	local x0 = vertices[#vertices].x - x
    	local y0 = vertices[#vertices].y - y
     
    	for i=1,#vertices do
    		local x1 = vertices[i].x - x
    		local y1 = vertices[i].y - y
     
    		if y0 > 0 and y1 <= 0 and x1 * y0 > y1 * x0 then
    			intersectionCount = intersectionCount + 1
    		end
     
    		if y1 > 0 and y0 <= 0 and x0 * y1 > y0 * x1 then
    			intersectionCount = intersectionCount + 1
    		end
     
    		x0 = x1
    		y0 = y1	
    	end
     
    	return (intersectionCount % 2) == 1
    end
    You should create the vertices like:
    vertices = {
    	{x = 50, y = 20},
    	{x = 200, y = 200},
    	{x = 10, y = 160}
    }
    Also I recommend PhysicsEditor http://www.physicseditor.de/ to easily create polygons from your images.

    Best,

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • Atilim,
    Thanks for the info.
    I'll try that some time.

    Chip
  • Thanks Atilim, this was very helpful.
  • tkhnomantkhnoman Member
    edited April 2013
    Very helpful indeed. And using PhysicsEditor help much.

    Code for getting vertices:
     
    function getTouchVertices(filename)
    	local vertices = {}
    	local phydataGet = (loadfile "publishedFromPhysicsEditor.lua")().physicsData(1.0)
     
    	for fi,fixture in pairs(phydataGet.data[filename].fixtures) do
     
    		vertices[fi] = {}
    		if(fixture.shape) then
    			for i = 1,#fixture.shape do
    				local vnum = math.ceil(i / 2)
    				if i % 2 == 1 then 
    					vertices[fi][vnum] = {}
    					vertices[fi][vnum].x = fixture.shape[i]
    				else
    					vertices[fi][vnum].y = fixture.shape[i]
    				end
    			end
    		end
    	end	
     
     
    	return vertices
    end
    object.vertices = getTouchVertices("something")

    And in case of rotating object:
    local xo,yo = object:getPosition()
    local deltaX,deltaY = xMouse - xo, yMouse - yo
     
    local rotatione = -math.atan2(deltaX, deltaY)* 180 / math.pi
    local length= math.sqrt(deltaX* deltaX + deltaY * deltaY)
    local degree = -math.rad( rotatione - object:getRotation() )
    local xToCheck = math.sin(degree) * length 
    local yToCheck = math.cos(degree) * length 
     
    for i = 1,#object.vertices do
    	if pointInPolygon(object.vertices[i], xToCheck,yToCheck) then
    		--return true
    	end
    end
Sign In or Register to comment.