Drag a physical object to corona sdk

I am trying to drag a dynamic body with gravity = 0.0 in my scene, I have a square with a dynamic body type, and the image with the body type is static, but dragging the square over the image is a little force, but can exceed the image and go to another side as pictures:

enter image description here

enter image description here

This is my code for dragging a square:

  local function dragBody( event )
                  local body = event.target
        local phase = event.phase
        local stage = display.getCurrentStage()

        if "began" == phase then
                stage:setFocus( body, event.id )
                body.isFocus = true
                body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )

        elseif body.isFocus then
                if "moved" == phase then
                        body.tempJoint:setTarget( event.x, event.y )

                elseif "ended" == phase or "cancelled" == phase then
                        stage:setFocus( body, nil )
                        body.isFocus = false
                        body.tempJoint:removeSelf()

                end
        end
        return true
end

And this is the code for creating objects:

function scene:createScene( event )
    local group = self.view
    my_square = display.newImage("square.png")
    my_square.x = 60
    my_square.y = 60
    physics.addBody(my_square, "dynamic" )
    group:insert(my_square)

    floor = display.newImage("piso.png")
    floor.x = 160
    floor.y = 240
    physics.addBody(floor, "static" )
    group:insert(floor)   
end

thanks for the help.

+4
source share
3 answers

When you manually move an object that you are doing out of control of physics, and basically you can make the object move past a static body.

, , , , , . , , .

+2

-, :

physics.setContinuous( false )

:

2D 3 . "". , . , .

, , . : https://itunes.apple.com/tr/app/bricks-world/id602065172?mt=8

, - , ^^ .

P.S: , .

:

local lastX, lastY
local function dragBody( event )
                  local body = event.target
        local phase = event.phase
        local stage = display.getCurrentStage()

        if "began" == phase then
                stage:setFocus( body, event.id )
                body.isFocus = true
                lastX, lastY = body.x, body.y
        elseif body.isFocus then
                if "moved" == phase then
                        -- You can change 1 with another value.
                        if(event.x > lastX) body.x = body.x + 1
                        else body.x = body.x - 1

                        if(event.y > lastY) body.y = body.y + 1
                        else body.y = body.y - 1

                        lastX, lastY = body.x, body.y

                elseif "ended" == phase or "cancelled" == phase then
                        stage:setFocus( body, nil )
                        body.isFocus = false
                end
        end
        return true
end
+2

  physical.setContinuous(false) " Box2D , " ". , , , ".

+2

All Articles