예제 #1
0
        public JObject B2n(b2Body body)
        {
            JObject bodyValue = new JObject();

            string bodyName = GetBodyName(body);
            if (null != bodyName)
                bodyValue["name"] = bodyName;

            switch (body.BodyType)
            {
                case b2BodyType.b2_staticBody:
                    bodyValue["type"] = 0;
                    break;
                case b2BodyType.b2_kinematicBody:
                    bodyValue["type"] = 1;
                    break;
                case b2BodyType.b2_dynamicBody:
                    bodyValue["type"] = 2;
                    break;
            }

            VecToJson("position", body.Position, bodyValue);
            FloatToJson("angle", body.Angle, bodyValue);

            VecToJson("linearVelocity", body.LinearVelocity, bodyValue);
            FloatToJson("angularVelocity", body.AngularVelocity, bodyValue);

            if (body.LinearDamping != 0)
                FloatToJson("linearDamping", body.LinearDamping, bodyValue);
            if (body.AngularDamping != 0)
                FloatToJson("angularDamping", body.AngularDamping, bodyValue);
            if (body.GravityScale != 1)
                FloatToJson("gravityScale", body.GravityScale, bodyValue);

            if (body.IsBullet())
                bodyValue["bullet"] = true;
            if (!body.IsSleepingAllowed())
                bodyValue["allowSleep"] = false;
            if (body.IsAwake())
                bodyValue["awake"] = true;
            if (!body.IsActive())
                bodyValue["active"] = false;
            if (body.IsFixedRotation())
                bodyValue["fixedRotation"] = true;

            b2MassData massData = new b2MassData();
            massData = body.GetMassData();

            if (massData.mass != 0)
                FloatToJson("massData-mass", massData.mass, bodyValue);
            if (massData.center.x != 0 || massData.center.y != 0)
                VecToJson("massData-center", body.LocalCenter, bodyValue);
            if (massData.I != 0)
            {
                FloatToJson("massData-I", massData.I, bodyValue);
            }

            //int i = 0;
            JArray arr = new JArray();
            b2Body tmp = body;
            while (tmp != null)
            {
                bodyValue.Add("fixture", B2n(tmp));
                tmp = body.Next;
            }

            bodyValue["fixture"] = arr;

            JArray customPropertyValue = WriteCustomPropertiesToJson(body);
            if (customPropertyValue.Count > 0)
                bodyValue["customProperties"] = customPropertyValue;

            return bodyValue;
        }