1
votes

I have a problem with draggable objects. When I drag an object fast through another object, it sticks inside the other object. Then when I release it, it shoots away.

Here is the my code, which I wrote using gameUI.lua. Could someone please help me?

display.setStatusBar(display.HiddenStatusBar)

local physics = require("physics")
physics.start()
physics.setDrawMode("normal")
physics.setGravity(0,5)

local gameUI = require("gameUI")

local floor = display.newImage("floor.jpg")
physics.addBody(floor, "static", {friction = 999, bounce = 0})
floor.y = 900

local box = display.newImage("box.png")
physics.addBody(box, {friction = 99, bounce = 0})
box.x = 100

function localDrag( event )
    gameUI.dragBody( event, { maxForce=100000, frequency=1000, dampingRatio=1,                              center=true })
end

function box:touch( event )
if event.phase == "began" then

    self.markX = self.x
    self.markY = self.y

elseif event.phase == "moved" then

    local x = (event.x - event.xStart) + self.markX
    local y = (event.y - event.yStart) + self.markY

    self.x, self.y = x, y

    if (self.x > 595) then
     self.x = 595
    end
    if (self.x < 46 ) then
    self.x = 46
end
    if (self.y > 795) then
    self.y = 795
    end
end

return true
end

box:addEventListener( "touch", localDrag)

local triangle = display.newImage("triangle.png")
triangle.x = 200
triangleShape = {   -45, 45  ,  -45, -45  ,  45, 45  }
physics.addBody(triangle, {friction = 99, bounce = 0, shape=triangleShape})
function localDrag( event )
gameUI.dragBody( event, { maxForce=1000000, frequency=1000, dampingRatio=1,                 center=true })
end

function triangle:touch( event )
if event.phase == "began" then

    self.markX = self.x
    self.markY = self.y

elseif event.phase == "moved" then

    local x = (event.x - event.xStart) + self.markX
    local y = (event.y - event.yStart) + self.markY

    self.x, self.y = x, y

    if (self.x > 595) then
     self.x = 595
    end
    if (self.x < 46 ) then
    self.x = 46
end
    if (self.y > 795) then
    self.y = 795
end
end

return true
end
triangle:addEventListener( "touch", localDrag)

local wall = display.newImage("wall.jpg")
physics.addBody(wall, "static", {friction = 0, bounce = 0})
wall.x = 665

local wall1 = display.newImage("wall.jpg")
physics.addBody(wall1, "static", {friction = 0, bounce = 0})
wall1.x = -25    
1

1 Answers

1
votes

Your friction is very, very high and I would suggest decreasing it a lot. (It might not hurt to read up on body properties in the Corona SDK docs about that, there's far more detail to it than I could go into in one post here.)

For your bodies getting stuck, that could be related to the friction although if they are actually overlapping at all I would suspect it was because of sleeping. You can set obj.isSleepingAllowed = false to on the bodies causing the problems and test that, I believe you will likely see a marked improvement.