Changing collision shape at runtime with WCK and Box2D

Greetings,

I'm currently working on a game project with WCK and wondered if anyone had insight into how I might be able to change/toggle my character's collision shape during certain states of the character's animation. I have a tall rectangle for the character normally, but when the character jumps, the character takes up less space vertically speaking. I'd like to reflect that change in the visual appearance in Box2d as closely as is reasonably possible. The character, as of now, has the same tall collision and it looks a bit weird when the character misses platforms due to the collision being lower than is reflected in the character animation.

I've tinkered around a bit and tried a few things, but I'm mostly stabbing in the dark trying to find the right combination of function calls to set up a different shape at runtime.

The last thing I've thought about doing is getting the information during PRE_SOLVE and trying to compare the overlap there and make particular exceptions based on what the character is doing. I'm worried about the accuracy of this with respect to custom shapes that undulate a bit.

Thank you very much for the help. Hopefully I can share a bit with some of the things I'm working on as I continue to use this engine over the next 4 months on the project I'm on.

-Burton
Trying to change the polygon vertices on the fly MIGHT work, but I think it's easier just to manage two independent shapes. You could create two separate boxes then use the "active" flag or category/mask bits to toggle which one is currently being used.

How are you defining your box now? By overriding the "shape" function? By extending shapes.Box? Or on the timeline? Probably the approach I would take is to have your player extend BodyShape, then have two child symbols that extend shapes.Box that just have visible = false so they define the boxes but aren't displayed. Then when the player jumps set the "active" flags on each box to true / false.
Right now I have it extending Box.

So I make subshapes for the different states and depending on what state I'm on, I toggle the shapes active == true or false? Correct me if I misunderstood.

Sounds simple enough. Thanks!
I have tried a couple of other ways to do it, namely:
1. having the upper part (torso?) only as a body, and use raycasts/sensor for the legs
2. use a 'roller and piston' approach, as outlined at viewtopic.php?f=8&t=1272&p=7393&hilit=characterjumping#p7393

They both have their pros and cons which I will write out here later when I have time, but for now this might give you some ideas: http://www.youtube.com/user/iforce2d
burtonposey wrote:
So I make subshapes for the different states and depending on what state I'm on, I toggle the shapes active == true or false? Correct me if I misunderstood.


Yep, that's it!
I achieved this by just making a new keyframe in my character movieclip. For example the top layer has the animations in movieclips, the lower layers then have shapes which make up the hitboxes. To change i just use this.gotoAndPlay(2) from inside my character class (which extends BodyShapes).
hey mayo,

Would you mind confirming that your wiki instructions are correct for the following?
Quote:
If you want the guide / child shape to use the component properties of its parent, set the isGuideShape property on the child to true. Using the box-with-arms example, setting isGuideShape on the child box object will allow you to have box-with-arms instances with different restitution, density, and other shape-specific properties. These properties would normally be locked to the value set within the box-with-arms symbol on the child.


I've been toying with this all day and it seems that setting the parent to isGuideShape works, not the child object. I was tipped off to this by seeing that in the code for BodyShape.findGuideOwner(), you check to see if the parent of an object has isGuideShape, not a child of the current object.
Code:
public function findGuideOwner():BodyShape {
   for(var p:DisplayObject = this; p; p = p.parent) {
      var b:BodyShape = p as BodyShape;
           if(b && !b.isGuideShape) {
         return b;
      }
   }
   return this;
}


This seems to have gotten the system working that engiebenjy has described. Maybe what you described in the quote above isn't even quite the same thing?

Thanks for all of your help guys.
Just noticed that engiebenjy's method doesn't work if each instance of the collision box on the timeline shares the same name. The engine treats it as if it already knows it, and doesn't change the dimensions as a result. This could be problematic if I have to listenWhileVisible on a bunch of different objects.

Having it on one layer does help because the shapes don't compound the mass when the parent is getting is getting sized up.

I think I'm going to have to put this endeavor on hold for a week or so until I have more time to investigate on the best way to implement this. engiebenjy, did your implementation involve listeners on those collision objects?
burtonposey wrote:
did your implementation involve listeners on those collision objects?


Nope I only listen on my main character class, not on the collision shapes under it.
engiebenjy's method (2 keyframes) is actually the first solution I thought of. Why can't you give them different names?

If you're listening for contact events, just listen for contact events off of your parent BodyShape. Contact events bubble, so you'll get them. Just make sure reportBeginContact, etc. is set on the child shapes.

I don't think you want to use isGuideShape here. Hopefully this will explain it better:

PARENT: restitution = 2, isGuideShape = false

CHILD: restitution = 1, isGuideShape = true

The Child will have a restitution of 2, because it is just a guide shape and takes everything from it's parent. But then keyframing the shapes wont work, not will setting active = true/false work.

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