Is it possible to create boundaries for a draggable object?

I posted previously that I wanted to create walls that my draggable object could not pass through, sort of like creating a simple maze for a draggable object to navigate through. I previously thought that using physics would be the way to go, but have since learned it’s not that simple. Does anybody know how I would go about accomplishing this? Rather than an entire maze, I just want to make one wall for now. Any help or advice would be much appreciated.

Thank you,
Steven [import]uid: 79394 topic_id: 16502 reply_id: 316502[/import]

What I used in my game may not work for you as it may not be best to use this method for multiple walls, but here it is. This drag function is taken from the “dragable platforms” sample project. I just added the 3 lines under “–limit dragging of object”. Basically it will take the Y position of the wall, and if your dragged object tries to go past it, it will set the Y coordinate back to where the wall.y is. You can also change it to x if you want to limit the horizontal drag. Again this probably won’t work for many walls but it may give you an idea.

[code]
– 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

–limit dragging of object
if t.y > wall.y then
t.y = wall.y
end

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
[/code] [import]uid: 31262 topic_id: 16502 reply_id: 61638[/import]

Thanks aaaron,

I’m using something similar for making sure my object stays within the boundaries of the screen, but never thought about using the same principal for my barrier/wall. Your suggestion definitely got me on the right track.

Much thanks,
Steven [import]uid: 79394 topic_id: 16502 reply_id: 61803[/import]