Need help to move object in limited area of triangle

guys i am stucked i have one triangle image and in which i want to move one circle

circle will move with finger in both x and y direction and must be always inside the triangle any idea how to do that

here’s the image how my triangle will look

http://upload.wikimedia.org/wikipedia/commons/thumb/3/3c/Black_triangle.svg/220px-Black_triangle.svg.png
thanks in advance

:slight_smile: [import]uid: 12482 topic_id: 17752 reply_id: 317752[/import]

perhaps something like this?

http://www.blackpawn.com/texts/pointinpoly/default.html

here’s an untested solution for the Barycentric Technique in Lua:

[lua]-- triangle coordinates
local x1, y1 = 3,4
local x2, y2 = 6,0
local x3, y3 = 0,0

function isPointInsideTriangle(x, y)
if (fAB()*fBC()>0 and fBC()*fCA()>0) then
return true
else
return false
end
end

function fAB()
return ((y-y1) * (x2-x1) - (x-x1) * (y2-y1))
end

function fBC()
return ((y-y2) *(x3-x2) - (x-x2) * (y3-y2))
end

function fCA()
return ((y-y3) * (x1-x3) - (x-x3) * (y1-y3))
end

– onTouch listener for your circle
local function onTouch(event)
if event.phase == “began” then
display.getCurrentStage():setFocus( event.target, event.id )
event.target.isFocus = true
elseif event.phase == “moved” and event.target.isFocus then

– this is where you do the actual check
if(isPointInsideTriangle(event.x, event.y)then
event.target.x = event.x
event.target.y = event.y
end

elseif event.target.isFocus then
if event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil, event.id )
event.target.isFocus = false
end
end

return true

end[/lua]

If I wasn’t getting ready for work in a little bit, I’d throw together a more complete Lua example. But that should def. get you started. [import]uid: 49447 topic_id: 17752 reply_id: 67717[/import]

thanks dude i will try it and i am sure this technique will work
:slight_smile: [import]uid: 12482 topic_id: 17752 reply_id: 67720[/import]

checked and it has worked perfectly.
:slight_smile: [import]uid: 12482 topic_id: 17752 reply_id: 67925[/import]

great, glad it worked out for you! [import]uid: 49447 topic_id: 17752 reply_id: 67932[/import]