Copying Bodies/Geoms

Topics: Developer Forum
Sep 19, 2009 at 4:26 PM

in my game, i have the players made up of several bodies/geoms, so when they get shot up they can fall apart. for example, if one gets shot in the head enough, the neck joint will break and the head will fly off. however, it's not enough to just kill the joints, because i need to be able to respawn the players without removing their dead body parts. so i made a (very) simple Entrails class and added the following code to my player class...

 

 

if (HeadGeom.ArbitraryFloat1 <= 0)
            {
                Neck.Dispose();
                Stander.Dispose();
                HeadGeom.CollisionGroup = 0;
                HeadDeath = true;

                Entrail e = new Entrail();
                e.Body = HeadBody;
                e.Flip = LookFlip;
                e.Geom = HeadGeom;
                e.Origin = HeadOrigin;
                e.Sprite = HeadBox;
                e.Added = false;

                Entrails.Add(e);

                foreach(Entrail entrail in Entrails)
                {
                    if (entrail.Added == false)
                    {
                      physicsSimulator.Add(entrail.Body);
                      physicsSimulator.Add(entrail.Geom);

                      entrail.Added = true;
                    }
                }

                HeadBody.Dispose();
                HeadGeom.Dispose();
            }

 

not exactly the cleanest code, i know.

basically, it creates an entrail object and sets the geom/body to that of the original head (as well as some other traits). the next part is sorta weird. i figured i needed to add the stuff to the physics engine AFTER i added the entrail to the entrails list. this could be wrong.

anyways, this isn't working right and i can't quite figure out why. when the head gets shot off, the body/geom just seems to go away. the sprite hangs in midair. the only conclusion i can come to is that the entrail is not being added to the simulation, but if "physicsSimulator.Add(entrail.Body);" doesn't work, then idk what's going on.  could somebody give me a hand here?

Sep 19, 2009 at 6:43 PM

When you add the body and geom to the physics simulator, are they added already? Also, at the end of this method, is it true that for each entrail, entrail.Body == entrail.Geom.Body?

Also here's a nice bit of C# 3.0 syntax that you might like to use:

Entrail e = new Entrail() 
    {
        Body = HeadBody,
        Flip = LookFlip,
        Geom = HeadGeom,
        Origin = HeadOrigin,
        Sprite = HeadBox,
        Added = false
    };