简体   繁体   中英

Collision of a drawed line and objects(Corona SDK)

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM