Collision of a drawed line and objects(Corona SDK)

时光毁灭记忆、已成空白 提交于 2019-12-12 04:08:59

问题


For example i have a line that i can draw with a finger and i have a rectangle. i want my line to end drawing up when it collisions with the rectangle. How can i do it? For ex my function of a line:

local line = function()
if(e.phase == "began") then
--code for line
elseif(e.phase == "moved") then
--code for line to draw
elseif(e.phase == "ended") then
--code for line to stop draw
end

i guess that i can do that with collision smith like this

local function onCollision( event )
        if ( event.phase == "began" ) then


                if event.object1.myName == "top" and event.object2.myName == "line" then
                        line("ended")

                end

        end
end

    Runtime:addEventListener("collision", onCollision);

but it doesn't work...any ideas?


回答1:


I'd need to see more of your code, particularly how you're creating the line (or lines if you're creating/destroying them often), to give the answer you're probably hoping for. However, if I were doing this I would probably draw/redraw the line on every finger move (without adding a physics body) and manually check for intersections with the rectangle based on finger position. Ie, something like the following:

local line = function()
    ...
    elseif(e.phase == "moved") then
        local cb = rect.contentBounds
        if event.x > cb.xMin and event.x < cb.xMax and event.y > cb.yMin and event.y < cb.yMax) then
            line("ended")
        end
    else
    ....
end

The problem with collisions is that if you're creating and recreating the lines and they do happen to intersect you may not get an event due to their short lifecycle (and the fact that they aren't actually moving). If you REALLY want to use collisions, I would create an invisible proxy object on touch begin (a circle) and draw a line from the start point to it on movements. I'd then use a touch joint on the proxy object and detect collisions on that. That's probably more bother than it's worth.



来源:https://stackoverflow.com/questions/10283740/collision-of-a-drawed-line-and-objectscorona-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!