Ejemplo n.º 1
0
        public virtual Body AddBodyInstanceToRuntime()
        {
            // box2D body
            if (this.polygons.Count > 0)
            {
                BodyDef bodyDef = new BodyDef();
                Vector2 pos = GetGlobalOffset(Vector2.Zero);
                bodyDef.position = new Vector2(pos.X / V2DScreen.WorldScale, pos.Y / V2DScreen.WorldScale);
                bodyDef.angle = GetGlobalRotation(0);
                bodyDef.fixedRotation = this.fixedRotation;
                bodyDef.angularDamping = this.angularDamping;
                bodyDef.linearDamping = this.linearDamping;

                IsStatic = isStatic;
                // todo: add kinematic
                if (isStatic)
                {
                    bodyDef.type = BodyType.Static;
                }
                else
                {
                    bodyDef.type = BodyType.Dynamic;
                }

                //// todo: this needs to allow for nested levels
                //if (!fixedRotation)
                //{
                //    for (int i = 0; i < transforms.Length; i++)
                //    {
                //        transforms[i].Position = transforms[i].Position - pos + State.Position;
                //        transforms[i].Rotation = transforms[i].Rotation - bodyDef.Angle + State.Rotation;
                //        //this.transforms[i].Scale /= bodyDef.Scale;
                //    }
                //}

                if (attributeProperties != null)
                {
                    attributeProperties.ApplyAttribtues(bodyDef);
                }

                body = v2dScreen.CreateBody(bodyDef);
                body.SetUserData(this);

                for (int i = 0; i < this.polygons.Count; i++)
                {
                    AddPoly(body, this.polygons[i]);
                }

                if (attributeProperties != null)
                {
                    if (attributeProperties.mass != 0f)
                    {
                        MassData md;
                        body.GetMassData(out md);
                        md.mass = attributeProperties.mass;
                        body.SetMassData(ref md);
                    }

                    if (attributeProperties.centerOfMassX != 0 || attributeProperties.centerOfMassY != 0)
                    {
                        MassData md;
                        body.GetMassData(out md);
                        md.center = new Vector2(attributeProperties.centerOfMassX, attributeProperties.centerOfMassY);
                        body.SetMassData(ref md);
                    }
                }
            }
            return body;
        }