Lua 在corona sdk中拖动物理对象

Lua 在corona sdk中拖动物理对象,lua,coronasdk,game-engine,game-physics,Lua,Coronasdk,Game Engine,Game Physics,在我的场景中,我尝试拖动重力为0,0的动态物体,我有一个身体类型为动态的正方形,还有一个身体类型为静态的图像,但是当将正方形拖动到图像上时,这会产生一点力,但会超过图像,并像图像一样传递到另一侧: 这是我拖动正方形的代码: local function dragBody( event ) local body = event.target local phase = event.phase local stage = d

在我的场景中,我尝试拖动重力为0,0的动态物体,我有一个身体类型为动态的正方形,还有一个身体类型为静态的图像,但是当将正方形拖动到图像上时,这会产生一点力,但会超过图像,并像图像一样传递到另一侧:

这是我拖动正方形的代码:

  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
这是创建对象的代码:

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

谢谢你的帮助。

当你手动移动一个物体时,你是在不受物理控制的情况下移动的,基本上你可以强迫这个物体通过静止物体


你可以做的是设置碰撞检测,当你移动正方形时,它会给你一个事件,告诉你什么时候停止移动。当然,如果您在移动代码中不遵守这一点,您可以继续移动对象。

首先,我建议您尝试:

physics.setContinuous( false )
如果您已经这样做了:

Physics2D引擎中有3种不同的物理类型。出于拖动目的,可以使用“运动学”对象类型。但是,如果有义务将动态对象用作可拖动对象,那么冲突中可能会出现错误。但若你们的静态对象每次都是一样的,你们可以通过拖动功能来控制它

我已经实现了一个小的手机游戏,使用相同的东西,你想实现。以下是链接:

如果你认为你想在这个游戏中得到类似的东西,只需留下评论^^^我可以进一步帮助你

注:在游戏中,控制器的拨片是动态的,屏幕周围的墙壁是静态的

另一个解决方案:

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
试一试推杆 物理.设置连续(假)
“默认情况下,Box2D执行连续碰撞检测,以防止对象“隧穿”。如果关闭该功能,移动速度足够快的对象可能会穿过薄壁。”

感谢您的回复,我正在用物理移动正方形,现在我将使用更多的对象,不会只为了移动而听所有对象的碰撞谢谢你的回答,但是“physics.setContinuous(false)”不起作用,我尝试组合不同类型的实体,但不起作用,就是为了这个,我提出了这个问题我添加了另一个解决方案。