[Resolved] Stop dynamic items from overlapping when they collide

Currently I have some crates that are falling from the sky and are hitting the ground and stopping, which is correct. But when another crate falls exactly on top it is overlapping a bit and then easing back up to where it is supposed to.

The code is:

[lua]function newLetter()

letter = display.newImage(“crate.png”);

letter.x = (col * 61) + 30;
letter.y = -100;
physics.addBody( letter, { density=1, friction=0.4, bounce=0} );

col = col + 1;
if(col > 4) then
col = 0;
end
end

local dropLetter = timer.performWithDelay( 1000, newLetter, 100 );[/lua]

I had an event listener in there that converted the crate to static once it hit the ground/item below it

[lua]local function onCollision( event )
if ( event.phase == “began” ) then
event.object2:addEventListener(“touch”, onTapBox )
event.object2.bodyType = “static”;
end
end

Runtime:addEventListener( “collision”, onCollision )[/lua]

But when I then tap a crate to remove it the crates above aren’t moving down like they should.

Any ideas? [import]uid: 132533 topic_id: 22905 reply_id: 322905[/import]

Check out isBullet=true, I think this adds extra over head but will mkae collision calculations look ahead. Mostly used to stop bullets flying through walls.

physics.addBody( letter, { density=1, friction=0.4, bounce=0, isBullet=true} );
Let me know what happens. :slight_smile: [import]uid: 118379 topic_id: 22905 reply_id: 91471[/import]

Unfortunately it is still doing it :frowning: [import]uid: 132533 topic_id: 22905 reply_id: 91475[/import]

Should this type of system be done using physics? or is there a better way of doing it. It has falling blocks that fall in columns and then you can destroy items and the columns should call down to settle at the bottom. No item should ever move left to right, only down. [import]uid: 132533 topic_id: 22905 reply_id: 91476[/import]

Well it can be done with physics but by the sounds of it you could do it without. Your relying on collision detection to hold your objects in place rather then telling your objects to just go to a particular position.
To stop the left and right movement just use a conditional on enterFrame event.

If object.x > 100 or object.x < 100 then

object.x = 100

end

or

If object.x ~= 100 then

object.x = 100

end [import]uid: 118379 topic_id: 22905 reply_id: 91482[/import]

also is your physics body exactly the same shape as the box, e.g. perfect edge.

Add this in to check

physics.setDrawMode(“hybrid”)

turn off when you dont need. [import]uid: 118379 topic_id: 22905 reply_id: 91485[/import]

I seem to be able to get them to stay put by constantly setting their x and rotation in the enterFrame event.

But they still seem to “squish” each other. It can be seen where there is alot of them stacked as well as they start to compress. [import]uid: 132533 topic_id: 22905 reply_id: 91493[/import]

oh yeah and the body is exactly the same… [import]uid: 132533 topic_id: 22905 reply_id: 91494[/import]

seems that setting the frame rate to 60 and upping the physics PositionIterations and VelocityIterations fixes it… [import]uid: 132533 topic_id: 22905 reply_id: 91500[/import]

what about setting gravity to 0,0. Density to 1 and then use a impulse to fire them down. As they have no density / gravity and bounce is at 0 they may just stop dead on collision without overlap.

Food for thought. I my game has gravity zero and it seems to give you more control over certain aspects.

[code]

physics.setGravity(0,0)

function newLetter()

letter = display.newImage(“crate.png”);

letter.x = (col * 61) + 30;
letter.y = -100;
physics.addBody( letter, { density=1, friction=1, bounce=0} );

letter:applyLinearImpulse( 0, 20, letter.x, letter.y )

col = col + 1;
if(col > 4) then
col = 0;
end
end

local dropLetter = timer.performWithDelay( 1000, newLetter, 100 );

[/code] [import]uid: 118379 topic_id: 22905 reply_id: 91502[/import]

good stuff, glad you sussed it i’ll remember for future. [import]uid: 118379 topic_id: 22905 reply_id: 91503[/import]