How do I stop a draggable physic object from going through a wall?

Hey guys.

So I have a physics object that is draggable. Problem is, I can drag it through static objects.

I’ve tried getting it to stop moving at the static objects for hours. How do I fix this problem?

Any help would be much apprecited!

Code is beow:

[lua]
local centerX = display.contentCenterX
local centerY = display.contentCenterY

– A basic function for dragging physics objects
local function startDrag( event )
    local t = event.target

    local phase = event.phase
    if “began” == phase then
        display.getCurrentStage():setFocus( t )
        t.isFocus = true

        – Store initial position
        t.x0 = event.x - t.x
        t.y0 = event.y - t.y
        
        – Make body type temporarily “kinematic” (to avoid gravitional forces)
        event.target.bodyType = “kinematic”
        
        – Stop current motion, if any
        event.target:setLinearVelocity( 0, 0 )
        event.target.angularVelocity = 0

    elseif t.isFocus then
        if “moved” == phase then
            t.x = event.x - t.x0
            t.y = event.y - t.y0

        elseif “ended” == phase or “cancelled” == phase then
            display.getCurrentStage():setFocus( nil )
            t.isFocus = false
            
            – Switch body type back to “dynamic”, unless we’ve marked this sprite as a platform
            if ( not event.target.isPlatform ) then
                event.target.bodyType = “dynamic”
            end

        end
    end

    – Stop further propagation of touch event!
    return true
end

local crate = display.newImage( “crate.png”, 90, 90 )
physics.addBody( crate, { density=3.0, friction=0.4, bounce=0.2 } )
crate.isPlatform = true – custom flag, used in drag function above

crate:addEventListener( “touch”, startDrag )

[/lua]

Don’t drag it directly, use a touch joint instead and play with the damping and frequency values till you get the behavior you want: 

https://docs.coronalabs.com/daily/guide/physics/physicsJoints/index.html#touch

The problem is kinematic objects do not collide with static objects.  At least one of the two objects in a collision has to be “dynamic” to trigger a collision event.  See: https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Since you are not supposed to drag dynamic objects (since they are under the control of the physics engine), turning them kinematic is the right thing to do.  You might have to use non-physics collision detection (https://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/) or you could try zeroing out the objects physics values and set it’s individual gravity to 0 and try leaving it dynamic.

Rob

Don’t drag it directly, use a touch joint instead and play with the damping and frequency values till you get the behavior you want: 

https://docs.coronalabs.com/daily/guide/physics/physicsJoints/index.html#touch

The problem is kinematic objects do not collide with static objects.  At least one of the two objects in a collision has to be “dynamic” to trigger a collision event.  See: https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Since you are not supposed to drag dynamic objects (since they are under the control of the physics engine), turning them kinematic is the right thing to do.  You might have to use non-physics collision detection (https://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/) or you could try zeroing out the objects physics values and set it’s individual gravity to 0 and try leaving it dynamic.

Rob