How to add container objects to physics

Trying to add container objects to physics crashes the game without any error logs. Here is what I am trying to do:

local headName=“images/expressions/01.png”

  local headOutline = graphics.newOutline( 2, headName )

  

  local headAnim = {

    “images/expressions/01.png”,

    “images/expressions/02.png”,

    “images/expressions/03.png”

}

local head = display.newContainer(80, 100);

  head.x=display.contentCenterX/2+100

  head.y=display.contentCenterY/2

for i = 1, #headAnim do

    local frame = display.newImageRect(head, headAnim[i], 80, 100);

    frame.isVisible = false;

end

head[1].isVisible = true;

head.currentFrame = 1;

ragdoll:insert (head)

– Code crashes when the below line executes

physics.addBody (head, {outline=headOutline, bounce = e, density=d, friction=0.4})

Hi @karan@mobieos.com,

It looks like you’re trying to insert various “parts” into one object, so that you can then use that object as part of a ragdoll. If so, have you considered using a snapshot instead of a container?

Take care,

Brent

Wao… Thanks Brent…I am working on a hit the boss kind of game and want to change the expressions as we hit the doll with different weapons. The body is under physics so cant add more elements at run time, couldnt add all expressions under physics etc.
 
I tried many things but did not know about snapshots till now. Had a read about it and looks promising, I will try it out.

Just a small puzzling behaviour that I am very curious to know about:
In the ragdoll example, we write:

local  doll1 = ragdoll.newRagDoll(0, 100, color1) 

print (doll1.x…doll1.y…“This is the ragdoll”)   – This returns 0,0 as x and y coordinates of the doll. The value never changes even if I drag the doll around. Why is it like this? This is not stopping but it will be good to understand this.

Hi @karan@mobieos.com,

Have you considered just using a sprite with multiple frames for expressions, and changing the frame when necessary?

As for the coordinates not updating, are you calling that repeatedly in a function? If you only call it once, it will execute once and never again (thus no change in the values).

Brent

Hey Brent,

Yes, using sprites was the way to go. Did that and it is working beautifully now. 

Regarding the coordinates not updating:  I did try calling it from different segments of the code making sure that the ragdoll is placed at different corners of the screen. Initially the ragdoll is placed in the center of the screen. But, it always returns 0,0 for me. The original sample also returns 0,0. The position of head or any component of the ragdoll gets returned properly but when I try to get the coordinates of the whole ragdoll which is a group, it gives me 0,0.

 

Hi again,

I imagine the group itself isn’t moving, just the ragdoll as an entire element within the group. That’s why you’d get 0,0 returned in all cases when checking the position of the group.

Brent

Hi @karan@mobieos.com,

It looks like you’re trying to insert various “parts” into one object, so that you can then use that object as part of a ragdoll. If so, have you considered using a snapshot instead of a container?

Take care,

Brent

Wao… Thanks Brent…I am working on a hit the boss kind of game and want to change the expressions as we hit the doll with different weapons. The body is under physics so cant add more elements at run time, couldnt add all expressions under physics etc.
 
I tried many things but did not know about snapshots till now. Had a read about it and looks promising, I will try it out.

Just a small puzzling behaviour that I am very curious to know about:
In the ragdoll example, we write:

local  doll1 = ragdoll.newRagDoll(0, 100, color1) 

print (doll1.x…doll1.y…“This is the ragdoll”)   – This returns 0,0 as x and y coordinates of the doll. The value never changes even if I drag the doll around. Why is it like this? This is not stopping but it will be good to understand this.

Hi @karan@mobieos.com,

Have you considered just using a sprite with multiple frames for expressions, and changing the frame when necessary?

As for the coordinates not updating, are you calling that repeatedly in a function? If you only call it once, it will execute once and never again (thus no change in the values).

Brent

Hey Brent,

Yes, using sprites was the way to go. Did that and it is working beautifully now. 

Regarding the coordinates not updating:  I did try calling it from different segments of the code making sure that the ragdoll is placed at different corners of the screen. Initially the ragdoll is placed in the center of the screen. But, it always returns 0,0 for me. The original sample also returns 0,0. The position of head or any component of the ragdoll gets returned properly but when I try to get the coordinates of the whole ragdoll which is a group, it gives me 0,0.

 

Hi again,

I imagine the group itself isn’t moving, just the ragdoll as an entire element within the group. That’s why you’d get 0,0 returned in all cases when checking the position of the group.

Brent