/* VERSIONE OBSOLETA DA AGGIORNARE
        /// <summary>
        /// Posiziona la parte vicino a quella a cui deve unirsi e crea il giunto.
        /// </summary>
        /// <param name="otherPart">Altra parte a cui unire questa</param>
        /// <param name="thisPartPosition">Posizione del centro del giunto rispetto al baricentro di questa parte</param>
        /// <param name="otherPartPosition">Posizione del centro del giunto rispetto al baricentro dell'altra parte</param>
        /// <param name="jointColor">Colore del giunto</param>
        /// <returns>false se thisPartPosizion è all'interno della parte, true altrimenti</returns>
        public bool Join(FParte otherPart, Vector2 thisPartPosition, Vector2 otherPartPosition, Color jointColor)
        {
            Body.Position = Utils.TranslateAndRotate((otherPartPosition - thisPartPosition), otherPart.Body.Position, otherPart.Body.Rotation);
            Body.Rotation = otherPart.Body.Rotation;
            Joint = JointFactory.CreateRevoluteJoint(World, otherPart.Body, this.Body, thisPartPosition);
            JointOffset = new Vector2(thisPartPosition.X, thisPartPosition.Y);
            Joint.MotorEnabled = true;
            Joint.MaxMotorTorque = Const.MaxMotorTorquePerAreaUnit * this.BodySize.X * this.BodySize.Y;

            //determina la dimensione del giunto in base alla posizione rispetto al centro del corpo e alla dimensione del corpo stesso
            if ((thisPartPosition.X > BodySize.X / 2) && (Math.Abs(thisPartPosition.Y) <= BodySize.Y / 2))
            {
                JointRadius = thisPartPosition.X - BodySize.X / 2;
                ConnectionSide = Side.Right;
            }
            else if ((thisPartPosition.X < -BodySize.X / 2) && (Math.Abs(thisPartPosition.Y) <= BodySize.Y / 2))
            {
                JointRadius = -thisPartPosition.X - BodySize.X / 2;
                ConnectionSide = Side.Left;
            }
            else if ((thisPartPosition.Y > BodySize.Y / 2) && (Math.Abs(thisPartPosition.X) <= BodySize.X / 2))
            {
                JointRadius = thisPartPosition.Y - BodySize.Y / 2;
                ConnectionSide = Side.Bottom;
            }
            else if ((thisPartPosition.Y < -BodySize.Y / 2) && (Math.Abs(thisPartPosition.X) <= BodySize.X / 2))
            {
                JointRadius = -thisPartPosition.Y - BodySize.Y / 2;
                ConnectionSide = Side.Top;
            }
            else
                return false;

            JointFixture = FixtureFactory.CreateCircle(JointRadius, Density, Body, JointOffset);
            if(JointColor != null)
                JointColor = jointColor;
            Joint.CollideConnected = true;      //abilita la collisione dei due body connessi dal giunto
            PartActuator = new Actuator(this, otherPart, Const.MaxForcePerAreaUnit * BodyArea);
            return true;
        }
        */
        /// <summary>
        /// Posiziona la parte vicino a quella a cui deve unirsi e crea il giunto.
        /// </summary>
        /// <param name="otherPart">Altra parte a cui unire questa</param>
        /// <param name="thisPartSidePosition">Posizione del punto di contatto del giunto con il bordo di questa parte rispetto al centro di questa parte</param>
        /// <param name="otherPartSidePosition">Posizione del punto di contatto del giunto con il bordo dell'altra parte rispetto al centro dell'altra parte</param>
        /// <param name="jointColor">Colore del giunto</param>
        /// <param name="jointRadius">Raggio del giunto</param>
        /// <returns>false se thisPartSidePosition non è sul bordo della parte, true altrimenti</returns>
        public bool Join(FParte otherPart, Vector2 thisPartSidePosition, Vector2 otherPartSidePosition, Color jointColor, float jointRadius)
        {
            Vector2 jointOffset;
            if (thisPartSidePosition.X == -(BodySize.X / 2))
            {
                jointOffset = new Vector2(-jointRadius * 2, 0);
                ConnectionSide = Side.Left;
            }
            else if (thisPartSidePosition.X == (BodySize.X / 2))
            {
                jointOffset = new Vector2(jointRadius * 2, 0);
                ConnectionSide = Side.Right;
            }
            else if (thisPartSidePosition.Y == -(BodySize.Y / 2))
            {
                jointOffset = new Vector2(0, -jointRadius * 2);
                ConnectionSide = Side.Top;
            }
            else if (thisPartSidePosition.Y == (BodySize.Y / 2))
            {
                jointOffset = new Vector2(0, jointRadius * 2);
                ConnectionSide = Side.Bottom;
            }
            else
                return false;

            Body.Position = Utils.TranslateAndRotate(otherPartSidePosition - thisPartSidePosition - jointOffset, otherPart.Body.Position, otherPart.Body.Rotation);
            Body.Rotation = otherPart.Body.Rotation;
            if (Joint != null)
                World.RemoveJoint(Joint);
            Joint = JointFactory.CreateRevoluteJoint(World, otherPart.Body, this.Body, thisPartSidePosition + jointOffset/2);
            //Joint.MotorEnabled = true;
            //Joint.MaxMotorTorque = Const.MaxMotorTorquePerAreaUnit * this.BodySize.X * this.BodySize.Y;

            JointRadius = jointRadius;
            JointFixture = FixtureFactory.CreateCircle(JointRadius, Density, Body, thisPartSidePosition + jointOffset/2);
            if (JointColor != null)
                JointColor = jointColor;
            JointOffset = thisPartSidePosition + jointOffset / 2;
            Joint.CollideConnected = true;
            otherPart.AddChild(this);
            return true;
        }
Beispiel #2
0
        private void resetCart()
        {
            if (pole1 != null)
            {
                pole1.Body.SleepingAllowed = true;
                pole1.Joint.Enabled = false;

                pole1.RemoveCollidesCategory(Category.Cat4);
                pole1.CollisionCategory = Category.None;

            }
            if (pole2 != null)
            {
                pole2.Body.SleepingAllowed = true;
                pole2.Joint.Enabled = false;

                pole2.RemoveCollidesCategory(Category.Cat4);
                pole2.CollisionCategory = Category.None;
            }

            if (cart != null)
            {
                cart.Body.SleepingAllowed = true;
                cart.CollisionCategory = Category.None;
            }
            /*
            if (pole1 != null && pole1.Joint != null && pole1.Body!= null)
            {
                world.RemoveJoint(pole1.Joint);
                world.RemoveBody(pole1.Body);
            }
            if (pole2 != null && pole2.Joint != null && pole2.Body != null)
            {
                world.RemoveJoint(pole2.Joint);
                world.RemoveBody(pole2.Body);
            }
            if(cart != null && cart.Body != null)
                world.RemoveBody(cart.Body);
            */

            cart = new FParte(cartSize, cartPosition, Const.PartDensity * 3, world);
            cart.CollisionCategory = Category.Cat4;

            pole1 = new FParte(pole1Size, Vector2.Zero, Const.PartDensity, world, Color.Blue, Color.Yellow);
            pole1.Join(cart, new Vector2(0, pole1Size.Y / 2), new Vector2(0, -cartSize.Y / 2), Color.Yellow, 0.1f);
            pole1.CollisionCategory = Category.Cat1;
            pole1.AddCollidesCategory(Category.Cat4);

            pole2 = new FParte(pole2Size, Vector2.Zero, Const.PartDensity, world, Color.Blue, Color.Yellow);
            pole2.Join(cart, new Vector2(0, pole2Size.Y / 2), new Vector2(0, -cartSize.Y / 2), Color.Yellow, 0.1f);
            pole2.CollisionCategory = Category.Cat2;
            pole2.AddCollidesCategory(Category.Cat4);
        }
 public void AddChild(FParte part)
 {
     ChildParts.Add(part);
 }