How can I achieve this drag and drop effect?

Hi,

I’m making a game that there are two regions on left and right side of the screen and there are some objects floating around and cannot enter those regions by themselves. I achieved it by category bitmask and mask bits.

What I want to achieve is when player touches and drags any of those floating objects, he or she can drag them into those regions that were previously blocking floating objects.

I achieved this in Unity with changing mask filter of object on drag event but apparently it’s not possible to change category and bitmask after creation of a physic object.

So how can I achieve this?

Thanks. [import]uid: 206803 topic_id: 34707 reply_id: 334707[/import]

Now that corona provides the physics contact object you could implement this a lot more simply by checking (when a collision occurs) if the object should be allowed to pass through a wall or not.

http://www.coronalabs.com/blog/2012/11/27/introducing-physics-event-contact/
http://docs.coronalabs.com/api/event/collision/contact.html [import]uid: 8271 topic_id: 34707 reply_id: 137904[/import]

as horacebury said you could check when a collision occour and then if the object is allowed to pass through.

Or if you are lazy and want a simple/ugglier sulotion you could simply remove the physics body when touched and create a new one with the new mask.

like so:

[code]
local firstFilter = { categoryBits = 2, maskBits = 3 }
local secondFilter = { categoryBits = 4, maskBits = 5 }

local function touch (event)
if event.phase == “began” then
physics.removeBody( ground )
physics.addBody( ground, “static”, { friction=0.5, bounce=0.3 , filter=secondFilter })
end

–then reset the filter when event ended

if event.phase == “ended” then
physics.removeBody( ground )
physics.addBody( ground, “static”, { friction=0.5, bounce=0.3 , filter=firstFilter })
end
end
[/code] [import]uid: 134049 topic_id: 34707 reply_id: 138278[/import]

I’m afraid there is a serious problem created by that approach. Considering that we don’t know where the body is, calling removeBody does not fire a collision ended phase, but calling addBody does fire a collision began phase.

To be honest, I would definitely recommend going with a clean solution - it’s not much code and it will be very reliable. [import]uid: 8271 topic_id: 34707 reply_id: 138292[/import]

there doesnt have to occur any collisions using my method, or rather you wont have to use collisions at all using that approach.

But as said thats a “uggly” way of solving the problem and i do not reccomend it.

(unless that is the only way you can understand it and want a qiuckfix until you get some more meat on your bones, so to say ) [import]uid: 134049 topic_id: 34707 reply_id: 138293[/import]

Yes I’ve read that removing and adding physic bodies is not a good practice. I’m afraid that I haven’t been able to incorporate that preCollision idea into my game but I’m trying and I think it’s the way. [import]uid: 206803 topic_id: 34707 reply_id: 138317[/import]

Now that corona provides the physics contact object you could implement this a lot more simply by checking (when a collision occurs) if the object should be allowed to pass through a wall or not.

http://www.coronalabs.com/blog/2012/11/27/introducing-physics-event-contact/
http://docs.coronalabs.com/api/event/collision/contact.html [import]uid: 8271 topic_id: 34707 reply_id: 137904[/import]

as horacebury said you could check when a collision occour and then if the object is allowed to pass through.

Or if you are lazy and want a simple/ugglier sulotion you could simply remove the physics body when touched and create a new one with the new mask.

like so:

[code]
local firstFilter = { categoryBits = 2, maskBits = 3 }
local secondFilter = { categoryBits = 4, maskBits = 5 }

local function touch (event)
if event.phase == “began” then
physics.removeBody( ground )
physics.addBody( ground, “static”, { friction=0.5, bounce=0.3 , filter=secondFilter })
end

–then reset the filter when event ended

if event.phase == “ended” then
physics.removeBody( ground )
physics.addBody( ground, “static”, { friction=0.5, bounce=0.3 , filter=firstFilter })
end
end
[/code] [import]uid: 134049 topic_id: 34707 reply_id: 138278[/import]

I’m afraid there is a serious problem created by that approach. Considering that we don’t know where the body is, calling removeBody does not fire a collision ended phase, but calling addBody does fire a collision began phase.

To be honest, I would definitely recommend going with a clean solution - it’s not much code and it will be very reliable. [import]uid: 8271 topic_id: 34707 reply_id: 138292[/import]

there doesnt have to occur any collisions using my method, or rather you wont have to use collisions at all using that approach.

But as said thats a “uggly” way of solving the problem and i do not reccomend it.

(unless that is the only way you can understand it and want a qiuckfix until you get some more meat on your bones, so to say ) [import]uid: 134049 topic_id: 34707 reply_id: 138293[/import]

Yes I’ve read that removing and adding physic bodies is not a good practice. I’m afraid that I haven’t been able to incorporate that preCollision idea into my game but I’m trying and I think it’s the way. [import]uid: 206803 topic_id: 34707 reply_id: 138317[/import]