How do people change their physics body?

I’ve seen tons of games where you can, grow for a certain period of time and the collision detection still works perfectly. How do people do it? I have searched for a long time now but have not found a solution… [import]uid: 14018 topic_id: 5551 reply_id: 305551[/import]

i presume you mean for something like this
http://www.youtube.com/watch?v=lfRYpx01qhw

either dont use physics (use maths instead) or removeSelf your body in your collision event and add a new body in the same location (but bigger) in the enterFrame event

as I explained in your other post
http://developer.anscamobile.com/forum/2011/01/24/how-grow-physics-body
[import]uid: 6645 topic_id: 5551 reply_id: 18846[/import]

    -

@NahirC, try this:
http://developer.anscamobile.com/forum/2011/01/23/cant-add-body-new-spawn-image [import]uid: 7856 topic_id: 5551 reply_id: 18881[/import]

you cannot remove or add bodies to the physics world inside the box2D simulation time-“step”. as far as i understand ansca’s documentation/comments, when you call removeSelf() it flags the object for removal and this will be removed before the next enterFrame event. It can’t remove it immediately as this will cause a crash.

therefore since you are removing your object at the end of one frame and adding a new one again in the next (but bigger), it will just look like the object has grown from one frame to the next

so the process overall is like this…

enter physics step function -> collisions -> removeSelf -> exit physics step function -> remove flagged bodies -> enterFrame -> add new body

j

[import]uid: 6645 topic_id: 5551 reply_id: 18896[/import]

by “in the same location” i meant at the same position at the object you removed. to the physics world you’ve destroyed one body and added another in its place. to the user it just looks like the object has grown
[import]uid: 6645 topic_id: 5551 reply_id: 18897[/import]

Something like this?

-- ballSize  
ballSize = 6  
  
-- Position  
xPos = 240  
yPos = 280  
  
function createBall()  
 ballCollisionFilter = { categoryBits=1, maskBits=3 }  
  
 -- Load ball  
 local ball = display.newImage("ball.png", xPos, yPos)  
 localGroup:insert(ball)  
  
 -- Opacy  
 ball.alpha = 0.8  
  
 -- Class  
 ball.class = "ball"  
  
 -- Physics  
 physics.addBody(ball, "dynamic", { density=0.5, friction=2.0, bounce=0,   
 radius=ballSize, filter=ballCollisionFilter })  
  
 -- No rotation  
 ball.isFixedRotation = true  
  
 -- Speed limit  
 ball.linearDamping = 10  
end  
createBall()  

And on the collision:

local function onCollisionBall(self, event)  
 if(event.phase == "began" and event.other.class == "1") then  
  
 ballSize = ballSize + 10  
  
 xPos = ball.x  
 yPos = ball.y  
  
 ball:removeSelf()  
 ball = nil  
  
 createBall()  
 end  
  
end  
ball.collision = onCollisionBall  
ball:addEventListener("collision", ball)  

This isn’t working for me though I get some error right after it loads up the ball [import]uid: 14018 topic_id: 5551 reply_id: 18906[/import]

no you cant add a body inside your collision, it’ll crash, as mentioned here http://developer.anscamobile.com/forum/2011/01/24/how-grow-physics-body#comment-18425

add your new body in your enterFrame. I’ve explained why it needs to happen in that link and here etc http://developer.anscamobile.com/forum/2010/12/03/how-grow-body

[import]uid: 6645 topic_id: 5551 reply_id: 18916[/import]