Halp. How do I remove a physics body on a touch event??

Im new to Lua and under a big time crunch. Im trying to get the object defined by the physics engine to delete itself when touched. Ive tried the documentation but it hasn’t worked. At all.

How do I go about doing this?

Thank you! [import]uid: 10355 topic_id: 6265 reply_id: 306265[/import]

hi wrong forum.

try the Developer Forum or the Game Edition.

this is about the Lua language itself, rather than Corona API specifics

anyway, in your touch event do

[lua]if(event.phase==“began”) then
event.target:removeSelf()
end[/lua] [import]uid: 6645 topic_id: 6265 reply_id: 21589[/import]

This didnt work.

local removeBody = function( event )  
 local t = event.target  
 local phase = event.phase  
  
 if "began" == phase then  
 t:removeSelf()   
 end  
  
  
 return true  
end  
  

This is currently what im running and its still removing from the picture and not the physics body. That means you can hit tap about 2 mm out out it will still remove.

Surely there is a way to fix this?

Thank you for helping me too! [import]uid: 10355 topic_id: 6265 reply_id: 22838[/import]

local removeBody = function( event )  
 local t = event.physicsBody  
 local phase = event.phase  
  
if(event.phase=="began") then  
 event.physicsBody:removeSelf()  
end  
   
 return true  
end  
  

This is the other version im running and the same thing is happening. :confused: [import]uid: 10355 topic_id: 6265 reply_id: 22839[/import]

post your code. i cant see how it is set up. what are you actually trying to do? what do you mean “tap 2mm out”? hpw do you know the body isnt removing? removeSelf should delete the object entirely

[import]uid: 6645 topic_id: 6265 reply_id: 22844[/import]

http://img513.imageshack.us/i/triangle.png/

Look at that picture, when you tap the part thats not the triangle it will still delete. I need it to where when the only the actual triangle is hit will it delete.

  
------------------------------------------------  
  
display.setStatusBar(display.HiddenStatusBar)  
local physics = require("physics")  
physics.start()  
  
physics.setGravity(0, 9.7)  
------------------------------------------------  
  
rectangleShape = { -52.5,-10.5, -42.5,-20, 35.5,-20, 45.5,-9.5, 46,8.5, 36,16.5, -42.5,17, -52.5,8 }  
rectangle2Shape = { -18,-21, -8.5,-31, 9,-31, 18.5,-21.5, 18.5,21.5, 10,30, -7.5,30, -18,19.5 }  
rectangle3Shape = { -59.5,-6, -51,-12.5, 44,-12.5, 55.5,-7.5, 55.5,6, 44,13, -51.5,13, -59.5,6 }  
squareShape = { -29,-17, -19,-26.5, 11.5,-26.5, 21,-16.5, 21,12, 12,21, -19.5,21, -28.5,11.5 }  
square2Shape = { -19.5,-11.5, -13,-20.5, 8.5,-20.5, 16.5,-11.5, 15.5,7.5, 9,17, -12.5,17, -20,5.5 }  
triangleShape = { -5,-23, 2.5,-23, 24,15, 20,19.5, -22,19.5, -25.5,15.5 }  
starShape = { -19,-4.5, 0,-18, 19.5,-3.5, 12,18, -11,18 }  
goalShape = { -35,-6, 35,-6, 35,6, -35,6 }  
  
-----------------------------------------------  
local removeBody = function( event )  
 local t = event.target  
 local phase = event.phase  
  
if(event.phase=="began") then  
 event.target:removeSelf()  
end  
   
 return true  
end  
   
--------------------------------------------------  
  
local background = display.newImage("background.png")  
  
local ground = display.newImage("base.png")  
ground.x = 240  
ground.y = 317  
physics.addBody(ground, "static", { friction = .5, bounce = .2} )  
  
local reset = display.newImage("reset.png")  
reset.x = 20  
reset.y = 20  
  
local borderLeft = display.newImage("borderside.png")  
borderLeft.x = 0  
borderLeft.y = 161  
borderLeft.rotation = -90  
physics.addBody(borderLeft,"static", borderBodyElement )  
  
local borderRight = display.newImage("borderside.png")  
borderRight.x = 480  
borderRight.y = 161  
borderRight.rotation = -90  
physics.addBody(borderRight,"static", borderBodyElement )  
  
local star = display.newImage("star.png")  
star.x = 206  
star.y = 87  
physics.addBody(star, { friction = .3, bounce = .3, shape = starShape} )  
  
local goal = display.newImage("goal3.png")  
goal.x = 211  
goal.y = 269  
physics.addBody(goal, { friction = .4, bounce = .2, density = 0, shape = goalShape} )  
  
-----------------------------------------------  
  
local shapes  
  
 rectangle = display.newImage("rectanglenoshad.png")  
 rectangle.x = 164; rectangle.y = 298  
 physics.addBody(rectangle, { friction = .3, boucne = .1, density = .1, shape = rectangleShape} )  
 rectangle:addEventListener( "touch", removeBody )  
 rectangle.isSleepingAllowed = false   
  
 rectangleB = display.newImage("rectanglenoshad.png")  
 rectangleB.x = 264; rectangleB.y = 298  
 physics.addBody(rectangleB, { friction = .3, boucne = .1, density = .1, shape = rectangleShape} )  
 rectangleB:addEventListener( "touch", removeBody )  
  
 rectangle3 = display.newImage("rectangle3noshad.png")  
 rectangle3.x = 211; rectangle3.y = 213  
 physics.addBody(rectangle3, { friction = .3, boucne = .1, density = .1, shape = rectangle3Shape} )  
 rectangle3:addEventListener( "touch", removeBody)  
  
 rectangle2 = display.newImage("rectangle2noshad.png")  
 rectangle2.x = 185; rectangle2.y = 169  
 physics.addBody(rectangle2, { friction = .3, boucne = .1, density = .1, shape = rectangle2Shape} )  
 rectangle2:addEventListener( "touch", removeBody)  
  
 rectangle2b = display.newImage("rectangle2noshad.png")  
 rectangle2b.x = 232; rectangle2b.y = 167  
 physics.addBody(rectangle2b, { friction = .3, boucne = .1, density = .1, shape = rectangle2Shape} )  
 rectangle2b:addEventListener( "touch", removeBody )  
  
 square = display.newImage("squarenoshad.png")  
 square.x = 148; square.y = 251  
 physics.addBody(square, { friction = .3, boucne = .1, density = .1, shape = squareShape} )  
 square:addEventListener( "touch", removeBody )  
  
 square = display.newImage("squarenoshad.png")  
 square.x = 276; square.y = 253  
 physics.addBody(square, { friction = .3, boucne = .1, density = .1, shape = squareShape} )  
 square:addEventListener( "touch", removeBody )  
  
 triangle = display.newImage("triangle.png")  
 triangle.x = 208; triangle.y = 125  
 physics.addBody(triangle, { friction = .3, boucne = .1, density = 0, shape = triangleShape} )  
 triangle.rotation = -180  
 triangle:addEventListener( "touch", removeBody )  
  
  
-----------------------------------------------  
  

Again thank you for helping!!!
[import]uid: 10355 topic_id: 6265 reply_id: 22860[/import]

turn on hybrid mode and check your triangle shape is right? [import]uid: 6645 topic_id: 6265 reply_id: 22862[/import]

The triangle is right.

The Bridge sample code has this working properly. The rocks and coconuts are only removed at their radius boundary and not from the picture. [import]uid: 10355 topic_id: 6265 reply_id: 22863[/import]

ok, is this Windows simulator?

that appears to be a bug. posted here
http://developer.anscamobile.com/forum/2011/02/15/windows-touchable-shape-not-being-set-physics-body

physics shape really should define touch area. good find.

j

[import]uid: 6645 topic_id: 6265 reply_id: 23000[/import]

Its on both windows simulator and Mac. It also does it for circles.
Thank you for all your help this far, but I have one more question. Ive found a really inconvenient work around. Im making smaller transparent objects and welding them to the big ones so that the touch area is reduced. Now is there a way to remove one object by removing another?

Basically the transparent object has the removeSelf() command and the welded object removes when that one deletes? [import]uid: 10355 topic_id: 6265 reply_id: 23008[/import]

create a table called something like “children” and attach it to your middle object. then when you remove it, remove the children first

something like
[lua]function removeMe(event)

local obj=event.target

for child=#obj.children, 1, -1 do
obj.children[child]:removeSelf()
obj.children[child] = nil
end
obj.children = nil
obj:removeSelf()

end

mainObj.children = [childObj1, childObj2, childObj3}[/lua]

i don’t know if those "nil"s are needed. [import]uid: 6645 topic_id: 6265 reply_id: 23021[/import]

This works! and my work around works too! Thank you J!!! [import]uid: 10355 topic_id: 6265 reply_id: 23078[/import]

Is there a way to put the if “began” command in there? Other wise this works awesome. [import]uid: 10355 topic_id: 6265 reply_id: 23126[/import]

Bump? [import]uid: 10355 topic_id: 6265 reply_id: 28917[/import]

… [import]uid: 10355 topic_id: 6265 reply_id: 28799[/import]

… [import]uid: 10355 topic_id: 6265 reply_id: 28808[/import]