Пример #1
0
        public void SetBodyType(cpBodyType type)
        {
            cpBodyType oldType = bodyType;

            if (oldType == type)
            {
                return;
            }

            // Static bodies have their idle timers set to infinity.
            // Non-static bodies should have their idle timer reset.
            nodeIdleTime = (type == cpBodyType.STATIC ? cp.Infinity : 0.0f);


            if (type == cpBodyType.DYNAMIC)
            {
                this.m     = this.i = 0.0f;
                this.m_inv = this.i_inv = cp.Infinity;

                AccumulateMassFromShapes();
            }
            else
            {
                this.m     = this.i = cp.Infinity;
                this.m_inv = this.i_inv = 0.0f;

                this.v = cpVect.Zero;
                this.w = 0.0f;
            }

            // If the body is added to a space already, we'll need to update some space data structures.

            if (space != null)
            {
                cp.AssertSpaceUnlocked(space);


                if (oldType == cpBodyType.STATIC)
                {
                    // TODO This is probably not necessary
                    //			cpBodyActivateStatic(body, NULL);
                }
                else
                {
                    Activate();
                }

                // Move the bodies to the correct array.
                List <cpBody> fromArray = space.ArrayForBodyType(oldType);
                List <cpBody> toArray   = space.ArrayForBodyType(type);

                if (fromArray != toArray)
                {
                    fromArray.Remove(this);
                    toArray.Add(this);
                }

                // Move the body's shapes to the correct spatial index.
                cpBBTree fromIndex = (oldType == cpBodyType.STATIC ? space.staticShapes : space.dynamicShapes);
                cpBBTree toIndex   = (type == cpBodyType.STATIC ? space.staticShapes : space.dynamicShapes);

                if (fromIndex != toIndex)
                {
                    eachShape((s, o) =>
                    {
                        fromIndex.Remove(s.hashid);
                        toIndex.Insert(s.hashid, s);
                    }, null);
                }
            }
        }