Drag physic object in corona sdk

2019-05-03 17:14发布

问题:

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:

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.

回答1:

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


回答2:

When you manually move an object you are doing so out of control of Physics and basically you can force the object to move past the static body.

What you can do is setup collision detection that will give you an event when you move the square that will tell you when to stop moving. Of course if you don't honor that in your move code you can keep moving your object.



回答3:

Try putting physics.setContinuous( false ) "By default, Box2D performs continuous collision detection, which prevents objects from "tunneling." If it were turned off, an object that moves quickly enough could potentially pass through a thin wall."