physics.addbody() does not work inside a loop

I am trying to add multiple rectangles in one hit through a loop and here is my code:

 for i=1,7,1 do  
 rec = rectangles.createRoundedRect(left, top, 100, 18, 6)  
 physics.addBody(rec , "static", { density = 1.0, friction = 0.0, bounce = 0.2 } )  
 left = left + 50  
 top = top - 35  
 end  

The rectangles are added successfully however they are not treated as bodies (i.e other bodies do not collide with them)

What’s wrong with the code?
[import]uid: 167063 topic_id: 29452 reply_id: 329452[/import]

Hi there,

What kind of object does your [lua]rectangles.createRoundedRect[/lua] function return? You take the return value and assign it to a variable [lua]rec[/lua], which is then passed as the first argument to [lua]physics.addBody[/lua]. Since the first argument should be a display object, if [lua]rectangles.createRoundedRect[/lua] returns something else, or nothing at all, that would explain your issue.

  • Andrew [import]uid: 109711 topic_id: 29452 reply_id: 118260[/import]

rectangles.createRoundedRect is a custom function based on display.newRoundedRect.

I figured out the issue, apparently grouping the rectangles disabled their physical collision (they are treated as a group rather than individual object)
[import]uid: 167063 topic_id: 29452 reply_id: 118269[/import]

Any physics body you want to interact with another physics body must be in the same display group.
[import]uid: 19626 topic_id: 29452 reply_id: 118317[/import]