public Branch(Path2D path, GameContent gameContent, World world, Branch nearestBranch)
        {
            this.gameContent = gameContent;
            dot = gameContent.dot;
            circle = gameContent.jointCircle;
            square = gameContent.jointSquare;
            cursor = gameContent.cursor;

            BodyDef bd = new BodyDef();
            bd.position = path.Keys[0];
            bd.type = BodyType.Dynamic;
            body = world.CreateBody(bd);

            fixtureCount = path.Keys.Count;
            for (int i = 0; i < path.Keys.Count; i++)
                CreateFixture(path.Keys[i] - path.Keys[0]);

            if (nearestBranch != null)
            {
                RevoluteJointDef revJd = new RevoluteJointDef();
                revJd.bodyA = body;
                revJd.bodyB = nearestBranch.body;

                revJd.localAnchorA = Vector2.Zero;

                AABB aabb; nearestBranch.nearestFixture.GetAABB(out aabb);
                Vector2 p = aabb.GetCenter();
                revJd.localAnchorB = nearestBranch.nearestFixture.GetBody().GetLocalPoint(p);

                revJd.enableMotor = true;
                revJd.referenceAngle = nearestBranch.nearestFixture.GetBody().Rotation;

                revoJoint = (RevoluteJoint)world.CreateJoint(revJd);
                revoJoint.SetUserData((Branch)nearestBranch);

                nearestBranch.nearestFixture.SetUserData((Branch)this);
            }

            newGrow = true;
        }
        private void CreateStem()
        {
            AddBranch = true;

            AABB aabb = new AABB(); body.GetFixtureList().GetAABB(out aabb);
            Vector2 v = aabb.GetCenter() + new Vector2(0, 15);

            Path2D p = new Path2D();
            p.AddPoint(v); p.AddPoint(v + new Vector2(0, -10));

            stem = new Branch(p, gameContent, body.GetWorld(), null);
            RevoluteJointDef revJd = new RevoluteJointDef();
            revJd.bodyA = stem.body;
            revJd.bodyB = ground;

            revJd.collideConnected = true;

            revJd.localAnchorA = Vector2.Zero;
            revJd.localAnchorB = stem.body.Position;
            revJd.enableMotor = true;

            stem.revoJoint = (RevoluteJoint)world.CreateJoint(revJd);

            // A Small rotation sets the body in motion
            stem.body.Rotation = (float)Math.PI / 360;
        }
        public void HandleInput(InputState input, int playerIndex)
        {
            MouseState prevMouseState = input.LastMouseState[0];
            MouseState mouseState = input.CurrentMouseState[0];
            mousePos = new Vector2(mouseState.X, mouseState.Y);

            if (prevMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released
                && path.Keys.Count != 0)
            {
                if (path.Keys.Count > 1)
                {
                    if (nearestBranch != null && nearestBranch.body.GetFixtureList() != null)
                    {
                        if (nearestBranch.body.GetFixtureList() == nearestBranch.nearestFixture)
                            nearestBranch.Grow(path);
                        else
                            branches.Add(new Branch(path, GameContent, world, nearestBranch));
                    }

                    //else branches.Add(new Branch(path, GameContent, world, null));
                }
                path = new Path2D();
            }

            if (prevMouseState.RightButton == ButtonState.Pressed && mouseState.RightButton == ButtonState.Released
                && nearestBranch != null && path.Keys.Count == 0)
            {
                nearestBranch.CutDown(nearestBranch.nearestFixture);
            }

            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                if (GameContent.viewport.TitleSafeArea.Contains(new Point(mouseState.X, mouseState.Y)))
                    path.AddPoint(mousePos);
            }
            else
            {
                nearestBranch = null;
                float nearestDist = 10;
                foreach (Branch b in branches)
                {
                    float d = b.NearestFixtureDistance(mousePos, nearestDist);
                    if (d < nearestDist)
                    {
                        if (nearestBranch != null) nearestBranch.nearestFixture = null;

                        nearestBranch = b;
                        nearestDist = d;
                    }
                }
            }
        }