Destroying and recreating shape

Hi guys,

I've just started playing around with Box2D in my HTML5 and JavaScript game and everything was fine until I got to implementing crouch. I understand that you can't resize bodies in Box2D and from what I gathered to change the size of my player entity to match the size of the crouch I'd need to destroy the shape, change the size attributes, and then recreate the shape. Is this correct?

Creating shapes seems to have worked...but I'm totally lost as to how to destroy. As in, I don't know what to actually reference in the DestroyShape() function. Currently my code looks like this:

Code:
            // Crouch
            if (ig.input.state('crouch') && this.standing) {
                if (!this.crouching && !this.crouched){
                    console.log('destroy shape here');
                    this.body.DestroyShape(I DONT KNOW WTF TO PUT HERE);

                    this.crouching = true;
                    this.crouched = false;
                }
               
                if (this.crouching){
                    this.size.y = this.crouch;
                    console.log('set shape here');
                    this.setShapeDef();
                    this.crouching = false;
                    this.crouched = true;
                }
                this.currentAnim = this.anims.defaultCrouchIdle;
            }


How do I reference the shape I want to destroy before creating the new shape? If I remove the DestroShape() completely and run this with "this.setShapeDef()" only new shapes of the right size do seem to get created each time I press the crouch key. But they're created on top of my original full-size shape and presumably that needs to be destroyed before the new one is created...Or am I completely off base here?
Don't know much about the javascript port but I guess you would have to pass in the fixture you want to destroy.

If the crouch is purely an all or nothing state then you could have both fixtures on the body in the same place all the time, and turn them on/off as necessary by say making them sensors, or making them ignore collisions by disabling the contact in BeginContact.

If you want a gradually changing crouch state, perhaps you could make the player from two bodies joined by a prismatic joint. Continuously changing the fixtures themselves is not impossible but is kinda swimming upstream, as I see you have already discovered - though I wouldn't quite call it torture 🙂

Original thread: https://box2d.org/forum/viewtopic.php?t=8355