Search code examples
luacoronasdkgame-enginegame-physics

Drag physic object in corona sdk


I'm try to drag a dynamic body with gravity = 0,0 in my scene, I have a square with body type dynamic, and an image with body type static, but when drag the square over the image this have a little force but can exceed the image and pass to the other side like the images:

enter image description here

enter image description here

This is my code to drag the 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 to create the 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 help.


Solution

  • First, I recommed you to try:

    physics.setContinuous( false )
    

    If you did that already:

    There are 3 different physics type in Physics2D engine. For drag purposes you can use "Kinematic" object type. But if its an obligation to use a Dynamic object as a dragable object, there may be bugs in collisions. But if your static object will be same every time, you can control it in drag function.

    I have implemented a little mobile game using same thing that you want to achieve. Here is the link: https://itunes.apple.com/tr/app/bricks-world/id602065172?mt=8

    If you think that you want something similar in this game, just leave a comment^^ I can help further.

    P.S : In the game, the controller paddle is dynamic and walls around the screen are static.

    Another Solution:

    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's 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