collision removal

Hi
I’m trying to create an app where you create object and if they are hit with enoungh force they are deleted.
This is my code it works fine exept that it doesn’t delete objects of collision

local physics = require(“physics”)
local gameUI = require(“gameUI”)
physics.start()
physics.setGravity( 0, 0 )
–the sky
local sky = display.newImage( “sky.png” )

–the boundries
–bottom
local ground = display.newImage( “ground.png” )
ground.x = 300
ground.y = 1060
physics.addBody( ground )
ground.bodyType = “static”
–top
local ground4 = display.newImage( “ground.png” )
ground4.x = 300
ground4.y = -40
physics.addBody( ground4 )
ground4.bodyType = “static”
–left side
local ground2 = display.newImage( “ground.png” )
ground2.x = 40
ground2.y = 500
physics.addBody( ground2 )
ground2.bodyType = “static”
ground2.rotation = 90
–right side
local ground3 = display.newImage( “ground.png” )
ground3.x = 720
ground3.y = 500
physics.addBody( ground3 )
ground3.bodyType = “static”
ground3.rotation = 90

local launch = display.newImage( “down.png” )
launch.x = 150
launch.y = 100

local launch2 = display.newImage( “up.png” )
launch2.x = 150
launch2.y = 300

local boxGenerator = display.newImage( “crate.png”, 100, 960 )
local wallmaker = display.newImage( “wall.png”, 125, 760 )
local bird = display.newImage( “bird.png”, 525, 100 )

local function dragBody( event )
gameUI.dragBody( event, { maxForce=20000, frequency=1000, dampingRatio=1.0, center=true } )
end

function moveCircle( event )
local phase = event.phase

if “ended” == phase then

local crate = display.newImage( “crate.png” )
physics.addBody( crate, { density = 5,friction=0.5, bounce=0.3, filter=finish1CollisionFilter } )
crate.x = event.x
crate.y = event.y
crate:addEventListener( “touch”, dragBody )
crate.postCollision = onLocalPostCollision
crate:addEventListener( “postCollision”, crate )

end
end
boxGenerator:addEventListener( “touch”, moveCircle )

function wallMaker( event )
local phase = event.phase

if “ended” == phase then

local wall = display.newImage( “wall.png” )
physics.addBody( wall, { density=3.0, friction=0.5, bounce=0.3 } )
wall.x = event.x
wall.y = event.y
wall:addEventListener( “touch”, dragBody )
end
end
wallmaker:addEventListener( “touch”, wallMaker )

function birdMaker( event )
local phase = event.phase

if “ended” == phase then

local bird = display.newImage( “bird.png” )
physics.addBody( bird, { density=3.0, friction=0.5, bounce=0.3 } )
bird.x = event.x
bird.y = event.y
bird:addEventListener( “touch”, dragBody )
bird:applyForce( -500, 1000, bird.x, bird.y )
localGroup:insert(bird)

end
end
bird:addEventListener( “touch”, birdMaker )

function gravity( event )
local phase = event.phase

if “ended” == phase then
physics.setGravity ( 10, 0 )

end
end
launch:addEventListener( “touch”, gravity )
function gravityup( event )
local phase = event.phase

if “ended” == phase then
physics.setGravity ( -10, 0 )

end
end
launch2:addEventListener( “touch”, gravityup )
local function onLocalPostCollision( self, event )
if ( event.force > 5 ) then
crate:removeSelf()
print( "postCollision force: " … event.force … ", friction: " … event.friction )
end

end
[import]uid: 28068 topic_id: 6290 reply_id: 306290[/import]

Hey Komail,

Can you put the code in lua so it’s easier to ready. [import]uid: 12455 topic_id: 6290 reply_id: 21776[/import]

thanks in advance :slight_smile: [import]uid: 28068 topic_id: 6290 reply_id: 21661[/import]

sure no problem
[lua]local physics = require(“physics”)
local gameUI = require(“gameUI”)
physics.start()
physics.setGravity( 0, 0 )

–the sky
local sky = display.newImage( “sky.png” )

–the boundries
–bottom
local ground = display.newImage( “ground.png” )
ground.x = 300
ground.y = 1060
physics.addBody( ground )
ground.bodyType = “static”
–top
local ground4 = display.newImage( “ground.png” )
ground4.x = 300
ground4.y = -40
physics.addBody( ground4 )
ground4.bodyType = “static”
–left side
local ground2 = display.newImage( “ground.png” )
ground2.x = 40
ground2.y = 500
physics.addBody( ground2 )
ground2.bodyType = “static”
ground2.rotation = 90
–right side
local ground3 = display.newImage( “ground.png” )
ground3.x = 720
ground3.y = 500
physics.addBody( ground3 )
ground3.bodyType = “static”
ground3.rotation = 90

local launch = display.newImage( “down.png” )
launch.x = 150
launch.y = 100

local launch2 = display.newImage( “up.png” )
launch2.x = 150
launch2.y = 300

local boxGenerator = display.newImage( “crate.png”, 100, 960 )
local wallmaker = display.newImage( “wall.png”, 125, 760 )
local bird = display.newImage( “bird.png”, 525, 100 )

local function dragBody( event )
gameUI.dragBody( event, { maxForce=20000, frequency=1000, dampingRatio=1.0, center=true } )
end

function moveCircle( event )
local phase = event.phase

if “ended” == phase then

local crate = display.newImage( “crate.png” )
physics.addBody( crate, { density = 5,friction=0.5, bounce=0.3, filter=finish1CollisionFilter } )
crate.x = event.x
crate.y = event.y
crate:addEventListener( “touch”, dragBody )
crate.postCollision = onLocalPostCollision
crate:addEventListener( “postCollision”, crate )

end
end
boxGenerator:addEventListener( “touch”, moveCircle )

function wallMaker( event )
local phase = event.phase

if “ended” == phase then

local wall = display.newImage( “wall.png” )
physics.addBody( wall, { density=3.0, friction=0.5, bounce=0.3 } )
wall.x = event.x
wall.y = event.y
wall:addEventListener( “touch”, dragBody )
end
end
wallmaker:addEventListener( “touch”, wallMaker )

function birdMaker( event )
local phase = event.phase

if “ended” == phase then

local bird = display.newImage( “bird.png” )
physics.addBody( bird, { density=3.0, friction=0.5, bounce=0.3 } )
bird.x = event.x
bird.y = event.y
bird:addEventListener( “touch”, dragBody )
bird:applyForce( -500, 1000, bird.x, bird.y )
localGroup:insert(bird)

end
end
bird:addEventListener( “touch”, birdMaker )

function gravity( event )
local phase = event.phase

if “ended” == phase then
physics.setGravity ( 10, 0 )

end
end
launch:addEventListener( “touch”, gravity )
function gravityup( event )
local phase = event.phase

if “ended” == phase then
physics.setGravity ( -10, 0 )

end
end
launch2:addEventListener( “touch”, gravityup )

local function onLocalPostCollision( self, event )
if ( event.force > 5 ) then
crate:removeSelf()
print( "postCollision force: " … event.force … ", friction: " … event.friction )
end

end[/lua] [import]uid: 28068 topic_id: 6290 reply_id: 21842[/import]

Put the collision function on top of the object.

example:

[lua]

local function onLocalPostCollision(self, event)
if ( event.force > 5 ) then
self:removeSelf()
print( "postCollision force: " … event.force … ", friction: " … event.friction )
end
end

local crate = display.newImage( “crate.png” )
physics.addBody( crate, { density = 5,friction=0.5, bounce=0.3, filter=finish1CollisionFilter } )
crate.x = event.x
crate.y = event.y
crate:addEventListener( “touch”, dragBody )
crate.postCollision = onLocalPostCollision
crate:addEventListener( “postCollision”, crate )
[import]uid: 12455 topic_id: 6290 reply_id: 21896[/import]

thank you so much you wonderful person [import]uid: 28068 topic_id: 6290 reply_id: 21906[/import]

No problem, but did it worked?

[import]uid: 12455 topic_id: 6290 reply_id: 21914[/import]

yes [import]uid: 28068 topic_id: 6290 reply_id: 22285[/import]