Issue removing objects during collision

I’ve scanned through all the threads I can on this and I’m still confused.  It seems that you can’t change physics properties of an object during a collision.  

If I set isBodyActive to false on my “block” during a collision,  I’d expect my ball to pass straight through.  However it seems to take a few seconds to respond.

If I do display.remove(block)  then it works as expected, but now I have a problem since I’m doing object pooling and can’t get my block back into my scene.

What is the difference between removing an object using sceneGroup:remove vs display.remove?

I can’t seem to get my code to indent here =/  sorry

I create my blocks during scene creation

[lua]

local color = ‘red’

for i = 1, 10 do 

local block = display.newRect( 0, 0, 60, 2 )

block.index = i

block.name = ‘block’

– block.isVisible = false

block.isAlive = false

physics.addBody( block, ‘static’ )

block.collision = onCollision

block:addEventListener( ‘collision’, block )

blocks[i] = block

sceneGroup:insert( block )

block.color = color

end

[/lua]

block removal during enter frame

[lua]

for k, block in pairs(blocks) do

if block.isAlive then

block.y = block.y - 1

if block.y < 0 then

removeBlock(block)

end

end

end

[/lua]

add block function

[lua]

function addBlock()

for k, block in pairs(blocks) do

if block.isAlive ~= true then

block.contentWidth = 60

block.contentHeight = 2

block.isAlive = true

block.x = math.random( 

block.contentWidth/2 + 20, 

display.contentWidth - block.contentWidth/2 - 20 

)

block.y = display.contentHeight

break

end

end

end

[/lua]

removeblock function, being called during collision as well

[lua]

function removeBlock(block)

display.remove( block )

block.isAlive = false

block.isBodyActive = false

end

[/lua]

Removing the block using display.remove works instantly, but then I can’t find a way to add it back inside of addBlock… once it’s gone it seems like it’s really gone.  I’d figure just setting isBodyActive to false would be enough to turn off the physics object, but it doesnt happen instantly the way I’d like.  

Are you trying to make an object detect collision but pass through the other physics object? If so you can use “isSensor = true” and that will work.

https://docs.coronalabs.com/api/type/Body/isSensor.html

physics.addBody( block, "static", {isSensor = true} )

And if your trying to remove something use display.remove()

Good Luck!

I’m trying to remove something and display.remove() works great… but I’m trying to recycle the objects and I can’t get them back after that point.  I’ve settled on just moving these blocks out of the way giving them x = -1000, y=-1000…

is this the correct approach?

Well just moving them off the screen can work but it will use memory… Why don’t you make a function that spawns a object when you need it? Like you spawn an object and then remove it and then when you need it you call that function and it spawn that object again… 

-SonicX278

Thats what my whole removeBlock and addBlock function are for.  but how do I spawn and remove objects.  I’d guess you’d remove objects using display.remove()…  but then if i need to spawn a new one I’d have to do display.newRect() which defeats the whole purpose of object pooling… right?

Hi @tameem50,

There are some operations which can not be called in the same frame (time step) as a collision, because Box2D is figuring out various internal math/processes. The complete list of calls which apply to this rule can be found in this guide in the big yellow box about half through:

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

As for making an object “pass through” another object at the moment of collision (but the objects maintain typical behavior in other regards), that can be accomplished with a “preCollision” and physics contact. See “Physics Contact” in the same guide for details.

Take care,

Brent

What exactly is it that you are trying to accomplish? Can you describe please? 

From my understanding, I might have had a similar problem and I found work around. So please explain in detail what your objective is here (give a scenario)

Are you trying to make an object detect collision but pass through the other physics object? If so you can use “isSensor = true” and that will work.

https://docs.coronalabs.com/api/type/Body/isSensor.html

physics.addBody( block, "static", {isSensor = true} )

And if your trying to remove something use display.remove()

Good Luck!

I’m trying to remove something and display.remove() works great… but I’m trying to recycle the objects and I can’t get them back after that point.  I’ve settled on just moving these blocks out of the way giving them x = -1000, y=-1000…

is this the correct approach?

Well just moving them off the screen can work but it will use memory… Why don’t you make a function that spawns a object when you need it? Like you spawn an object and then remove it and then when you need it you call that function and it spawn that object again… 

-SonicX278

Thats what my whole removeBlock and addBlock function are for.  but how do I spawn and remove objects.  I’d guess you’d remove objects using display.remove()…  but then if i need to spawn a new one I’d have to do display.newRect() which defeats the whole purpose of object pooling… right?

Hi @tameem50,

There are some operations which can not be called in the same frame (time step) as a collision, because Box2D is figuring out various internal math/processes. The complete list of calls which apply to this rule can be found in this guide in the big yellow box about half through:

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

As for making an object “pass through” another object at the moment of collision (but the objects maintain typical behavior in other regards), that can be accomplished with a “preCollision” and physics contact. See “Physics Contact” in the same guide for details.

Take care,

Brent

What exactly is it that you are trying to accomplish? Can you describe please? 

From my understanding, I might have had a similar problem and I found work around. So please explain in detail what your objective is here (give a scenario)