예제 #1
0
        protected void LegsHandleBeforeStep()
        {
            float legLeftCoef  = AngleConversion.RadianToDegree(this.legLeft.Rotation) > -2 ? 1 : (float)System.Math.Pow(System.Math.Sin(AngleConversion.DegreeToRadian(92) + this.legLeft.Rotation), 15),
                  legRightCoef = AngleConversion.RadianToDegree(this.legRight.Rotation) < 2 ? 1 : (float)System.Math.Pow(System.Math.Sin(AngleConversion.DegreeToRadian(92) - this.legRight.Rotation), 15);

            this.legLeft.ApplyTorque(legLeftCoef);
            this.legRight.ApplyTorque(-legRightCoef);
            this.playerLeftSpeeds.BeforeStep  = this.playerLeft.LinearVelocity.X;
            this.playerRightSpeeds.BeforeStep = this.playerRight.LinearVelocity.X;
        }
예제 #2
0
        private void LegsHandleBeforeStep()
        {
            float legLeftCoef  = AngleConversion.RadianToDegree(this.legLeft.GetAngle()) > -2 ? 1 : (float)System.Math.Pow(System.Math.Sin(AngleConversion.DegreeToRadian(92) + this.legLeft.GetAngle()), 15),
                  legRightCoef = AngleConversion.RadianToDegree(this.legRight.GetAngle()) < 2 ? 1 : (float)System.Math.Pow(System.Math.Sin(AngleConversion.DegreeToRadian(92) - this.legRight.GetAngle()), 15);

            this.legLeft.ApplyTorque(1 * legLeftCoef);
            this.playerLeftSpeeds.BeforeStep = this.playerLeft.GetLinearVelocity().X;
            float legLeftAngleDegree  = AngleConversion.RadianToDegree(this.LegLeftAngle),
                  legRightAngleDegree = AngleConversion.RadianToDegree(this.LegRightAngle);

            if (legLeftAngleDegree < -9 && legLeftAngleDegree > -12)
            {
                this.legRight.ApplyTorque(legRightCoef);
            }
            else
            {
                this.legRight.ApplyTorque(-legRightCoef);
            }
            this.playerRightSpeeds.BeforeStep = this.playerRight.GetLinearVelocity().X;
        }
예제 #3
0
        protected float GetBallCollisionSpeed()
        {
            double alpha, beta, gamma, omega;

            alpha = System.Math.Atan2(this.ballPreviousSpeed.X, this.ballPreviousSpeed.Y);
            beta  = System.Math.Atan2(-this.ball.LinearVelocity.X, -this.ball.LinearVelocity.Y);
            if (System.Math.Abs(alpha - beta) > AngleConversion.DegreeToRadian(165))
            {
                beta = -beta;
            }

            gamma = (alpha + beta) / 2;
            omega = System.Math.Abs(gamma - beta);
            if (omega >= AngleConversion.DegreeToRadian(85))
            {
                return(0);
            }

            float previousFullSpeed = (float)System.Math.Sqrt(this.ballPreviousSpeed.X * this.ballPreviousSpeed.X + this.ballPreviousSpeed.Y * this.ballPreviousSpeed.Y);

            return((float)System.Math.Cos(omega) * previousFullSpeed);
        }
예제 #4
0
        public virtual void Draw(RenderWindow window)
        {
            this.playerLeft.Position = new Vector2f(
                this.objectsState.PlayerLeftPosition.X * this.scale - this.playerLeft.TextureRect.Width / 2f,
                this.objectsState.PlayerLeftPosition.Y * this.scale - 50);
            this.legLeft.Position = new Vector2f(
                this.objectsState.LegLeftPosition.X * this.scale - 7 * (float)Cos(this.objectsState.LegLeftAngle) + 16 * (float)Sin(this.objectsState.LegLeftAngle),
                this.objectsState.LegLeftPosition.Y * this.scale - 16 * (float)Cos(this.objectsState.LegLeftAngle) - 7 * (float)Sin(this.objectsState.LegLeftAngle));
            this.playerRight.Position = new Vector2f(
                this.objectsState.PlayerRightPosition.X * this.scale + this.playerRight.TextureRect.Width / 2f,
                this.objectsState.PlayerRightPosition.Y * this.scale - 50);
            this.legRight.Position = new Vector2f(
                this.objectsState.LegRightPosition.X * this.scale + 7 * (float)Cos(this.objectsState.LegRightAngle) + 16 * (float)Sin(this.objectsState.LegRightAngle),
                this.objectsState.LegRightPosition.Y * this.scale - 16 * (float)Cos(this.objectsState.LegRightAngle) + 7 * (float)Sin(this.objectsState.LegRightAngle));
            this.ball.Rotation = AngleConversion.RadianToDegree(this.objectsState.BallAngle);
            this.ball.Position = new Vector2f(
                this.objectsState.BallPosition.X * this.scale - (float)Sqrt(2) * this.ball.TextureRect.Width / 2f * (float)Sin(PI / 4 - this.objectsState.BallAngle),
                this.objectsState.BallPosition.Y * this.scale - (float)Sqrt(2) * this.ball.TextureRect.Width / 2f * (float)Cos(PI / 4 - this.objectsState.BallAngle));
            string remainTimeString = (this.remainTime / 60).ToString().PadLeft(2, '0') + ":" + (this.remainTime % 60).ToString().PadLeft(2, '0');

            this.remainTimeText.DisplayedString = remainTimeString;
            this.remainTimeText.Position        = new Vector2f(Major.Width / 2 - this.remainTimeText.GetGlobalBounds().Width / 2, 10);
            this.scoreText[0].DisplayedString   = this.score[0].ToString();
            this.scoreText[1].DisplayedString   = this.score[1].ToString();
            this.scoreText[1].Position          = new Vector2f(Major.Width - 20 - this.scoreText[1].GetGlobalBounds().Width, 20);
            window.Draw(this.background);
            window.Draw(this.playerLeft);
            window.Draw(this.legLeft);
            window.Draw(this.playerRight);
            window.Draw(this.legRight);
            window.Draw(this.ball);
            window.Draw(this.goalLeft);
            window.Draw(this.goalRight);
            window.Draw(this.remainTimeText);
            window.Draw(this.scoreText[0]);
            window.Draw(this.scoreText[1]);
        }
예제 #5
0
        public PhysicsProcess(BallRebound ballRebound, bool shouldAttack)
        {
            this.shouldAttack = shouldAttack;
            this.ball         = BodyFactory.CreateCircle(
                this.world,
                BallRadius,
                BallDensity,
                new Vector2(FieldWidth / 2, 2));
            this.ball.Friction    = BallFriction;
            this.ball.Restitution = ballRebound == BallRebound.High ? 0.87f :
                                    ballRebound == BallRebound.Medium ? 0.6f : 0.4f;
            this.ball.BodyType       = BodyType.Dynamic;
            this.ball.LinearVelocity = new Vector2(new Random().Next(-10, 11) / 8f, -2);
            this.ball.OnCollision   += this.BallContacting;
            this.ball.OnSeparation  += this.BallContacted;

            this.ground = BodyFactory.CreateRectangle(
                this.world,
                FieldWidth,
                GroundThickness,
                1,
                new Vector2(FieldWidth / 2, FieldHeight - 0.37f));
            this.ground.Friction = 1;

            this.leftWall = BodyFactory.CreateRectangle(
                this.world,
                WallThickness,
                FieldHeight,
                1,
                new Vector2(-WallThickness / 2, FieldHeight / 2));

            this.rightWall = BodyFactory.CreateRectangle(
                this.world,
                WallThickness,
                FieldHeight,
                1,
                new Vector2(FieldWidth + (WallThickness / 2), FieldHeight / 2));

            this.roof = BodyFactory.CreateRectangle(
                this.world,
                FieldWidth,
                WallThickness,
                1,
                new Vector2(FieldWidth / 2, -WallThickness / 2));

            PolygonShape playerBody = new PolygonShape(1.3f);

            this.playerLeft = BodyFactory.CreateRectangle(
                this.world,
                PlayerThickness,
                PlayerBodyHeight,
                1.3f,
                new Vector2(PlayerXPosition, PlayerYPosition));
            this.playerLeft.Friction      = 2;
            this.playerLeft.Restitution   = 0;
            this.playerLeft.FixedRotation = true;
            this.playerLeft.BodyType      = BodyType.Dynamic;
            CircleShape playerHead = new CircleShape(PlayerHeadRadius, 1);

            playerHead.Position = new Vector2(0, -0.35f);
            this.playerLeft.CreateFixture(playerHead, "Head");
            this.playerLeft.FixtureList[0].UserData    = "Body";
            this.playerLeft.FixtureList[0].Friction    = 2;
            this.playerLeft.FixtureList[0].Restitution = 0;
            this.playerLeft.FixtureList[1].Friction    = 2;
            this.playerLeft.FixtureList[1].Restitution = 0;
            this.playerLeft.OnCollision  += this.PlayerLeftContacting;
            this.playerLeft.OnSeparation += this.PlayerLeftContacted;
            this.playerLeft.OnCollision  += this.PlayersTackling;

            this.playerRight = BodyFactory.CreateRectangle(
                this.world,
                PlayerThickness,
                PlayerBodyHeight,
                1.3f,
                new Vector2(FieldWidth - PlayerXPosition, PlayerYPosition));
            this.playerRight.BodyType      = BodyType.Dynamic;
            this.playerRight.FixedRotation = true;
            this.playerRight.CreateFixture(playerHead, "Head");
            this.playerRight.FixtureList[0].UserData    = "Body";
            this.playerRight.FixtureList[0].Friction    = 2;
            this.playerRight.FixtureList[0].Restitution = 0;
            this.playerRight.FixtureList[1].Friction    = 10;
            this.playerRight.FixtureList[1].Restitution = 0;
            this.playerRight.OnCollision  += this.PlayerRightContacting;
            this.playerRight.OnSeparation += this.PlayerRightContacted;

            this.goalLeft = BodyFactory.CreateBody(this.world, new Vector2(0.5f, 5.035f));
            CircleShape goalLeftBar = new CircleShape(0.075f, 1);

            goalLeftBar.Position = new Vector2(0.425f, 0);
            this.goalLeft.CreateFixture(goalLeftBar);
            this.goalLeft.FixtureList[0].Friction    = 1;
            this.goalLeft.FixtureList[0].Restitution = 0;
            this.goalLeft.FixtureList[0].UserData    = "Post";
            Vector2[]    vertices    = new Vector2[] { new Vector2(-0.5f, -0.07f), new Vector2(0.42f, -0.07f), new Vector2(0.42f, -0.068f), new Vector2(-0.5f, -0.068f) };
            PolygonShape goalLeftNet = new PolygonShape(new Vertices(vertices), 1);

            this.goalLeft.CreateFixture(goalLeftNet);
            this.goalLeft.FixtureList[1].Friction    = 1;
            this.goalLeft.FixtureList[1].Restitution = 0;

            this.goalRight = BodyFactory.CreateBody(this.world, new Vector2(13.16f, 5.035f));
            CircleShape goalRightBar = new CircleShape(0.075f, 1);

            goalRightBar.Position = new Vector2(-0.425f, 0);
            this.goalRight.CreateFixture(goalRightBar);
            this.goalRight.FixtureList[0].Friction    = 1;
            this.goalRight.FixtureList[0].Restitution = 0;
            this.goalRight.FixtureList[0].UserData    = "Post";
            Vector2[]    vertices2    = new Vector2[] { new Vector2(-0.42f, -0.07f), new Vector2(0.5f, -0.07f), new Vector2(0.5f, -0.068f), new Vector2(-0.42f, -0.068f) };
            PolygonShape goalRightNet = new PolygonShape(new Vertices(vertices2), 1);

            this.goalRight.CreateFixture(goalLeftNet);
            this.goalRight.FixtureList[1].Friction    = 1;
            this.goalRight.FixtureList[1].Restitution = 0;

            this.legLeft          = BodyFactory.CreateBody(this.world, new Vector2(this.playerLeft.Position.X, this.playerLeft.Position.Y + 0.2f));
            this.legLeft.BodyType = BodyType.Dynamic;
            Vector2[]    vertices3 = new[] { new Vector2(0.12f, 0.03f), new Vector2(-0.07f, -0.06f), new Vector2(-0.03f, -0.15f) };
            PolygonShape leg       = new PolygonShape(new Vertices(vertices3), 1.4f);

            this.legLeft.CreateFixture(leg);
            this.legLeft.FixtureList[0].Friction    = 3;
            this.legLeft.FixtureList[0].Restitution = 0;
            this.jointLeft                = JointFactory.CreateRevoluteJoint(this.world, this.playerLeft, new Vector2(0.05f, -0.4f), this.legLeft, new Vector2(0, -0.64f));
            this.jointLeft.LowerLimit     = AngleConversion.DegreeToRadian(-18);
            this.jointLeft.UpperLimit     = AngleConversion.DegreeToRadian(13);
            this.jointLeft.LimitEnabled   = true;
            this.jointLeft.MotorEnabled   = true;
            this.jointLeft.MaxMotorTorque = 0.08f;

            this.legRight          = BodyFactory.CreateBody(this.world, new Vector2(this.playerRight.Position.X, this.playerRight.Position.Y + 0.2f));
            this.legRight.BodyType = BodyType.Dynamic;
            Vector2[]    vertices4 = new[] { new Vector2(-0.12f, 0.03f), new Vector2(0.03f, -0.15f), new Vector2(0.07f, -0.06f) };
            PolygonShape leg2      = new PolygonShape(new Vertices(vertices4), 1.4f);

            this.legRight.CreateFixture(leg2);
            this.legRight.FixtureList[0].Friction    = 3;
            this.legRight.FixtureList[0].Restitution = 0;
            this.jointRight                = JointFactory.CreateRevoluteJoint(this.world, this.playerRight, new Vector2(-0.05f, -0.4f), this.legRight, new Vector2(0, -0.64f));
            this.jointRight.LowerLimit     = AngleConversion.DegreeToRadian(-13);
            this.jointRight.UpperLimit     = AngleConversion.DegreeToRadian(18);
            this.jointRight.LimitEnabled   = true;
            this.jointRight.MotorEnabled   = true;
            this.jointRight.MaxMotorTorque = 0.08f;

            this.playerLeftTimers.Defend  = 12000;
            this.playerRightTimers.Defend = 12000;
        }
예제 #6
0
        public PhysicsProcessBox2D(Settings.BallReboundType ballRebound, bool shouldAttack)
        {
            this.shouldAttack = shouldAttack;

            BodyDef ballDef = new BodyDef
            {
                AngularDamping = 0.01f,
                Position       = new Vec2(FieldWidth / 2, 2)
            };

            this.ball = this.world.CreateBody(ballDef);
            this.ball.SetLinearVelocity(new Vec2(new Random().Next(-20, 21) / 8f, -2));
            CircleDef ballShapeDef = new CircleDef
            {
                Radius      = BallRadius,
                Density     = BallDensity,
                Friction    = BallFriction,
                Restitution = ballRebound == Settings.BallReboundType.High ? 0.87f :
                              ballRebound == Settings.BallReboundType.Medium ? 0.6f : 0.4f
            };

            this.ball.CreateShape(ballShapeDef);
            this.ball.SetMassFromShapes();

            BodyDef groundDef = new BodyDef
            {
                Position = new Vec2(FieldWidth / 2, FieldHeight - 0.37f)
            };

            this.ground = this.world.CreateBody(groundDef);
            PolygonDef groundShapeDef = new PolygonDef
            {
                Friction = 1f
            };

            groundShapeDef.SetAsBox(FieldWidth / 2, GroundThickness / 2);
            this.ground.CreateShape(groundShapeDef);

            BodyDef leftWallDef = new BodyDef
            {
                Position = new Vec2(-WallThickness / 2, FieldHeight / 2)
            };

            this.leftWall = this.world.CreateBody(leftWallDef);
            PolygonDef leftWallShapeDef = new PolygonDef();

            leftWallShapeDef.SetAsBox(WallThickness / 2, FieldHeight / 2);
            this.leftWall.CreateShape(leftWallShapeDef);

            BodyDef rightWallDef = new BodyDef
            {
                Position = new Vec2(FieldWidth + (WallThickness / 2), FieldHeight / 2)
            };

            this.rightWall = this.world.CreateBody(rightWallDef);
            PolygonDef rightWallShapeDef = new PolygonDef();

            rightWallShapeDef.SetAsBox(WallThickness / 2, FieldHeight / 2);
            this.rightWall.CreateShape(rightWallShapeDef);

            BodyDef roofDef = new BodyDef
            {
                Position = new Vec2(FieldWidth / 2, -WallThickness / 2)
            };

            this.roof = this.world.CreateBody(roofDef);
            PolygonDef roofShapeDef = new PolygonDef();

            roofShapeDef.SetAsBox(FieldWidth / 2, WallThickness / 2);
            this.roof.CreateShape(roofShapeDef);

            BodyDef playerLeftDef = new BodyDef
            {
                FixedRotation = true,
                Position      = new Vec2(PlayerXPosition, PlayerYPosition)
            };

            this.playerLeft = this.world.CreateBody(playerLeftDef);
            PolygonDef playerBody = new PolygonDef
            {
                Friction    = 2,
                Density     = 1.3f,
                Restitution = 0,
                UserData    = "Body"
            };

            playerBody.SetAsBox(PlayerThickness / 2, PlayerBodyHeight / 2);
            this.playerLeft.CreateShape(playerBody);
            CircleDef playerHead = new CircleDef
            {
                Friction      = 1,
                Density       = 1,
                Restitution   = 0,
                Radius        = PlayerHeadRadius,
                LocalPosition = new Vec2(0, -0.35f),
                UserData      = "Head"
            };

            this.playerLeft.CreateShape(playerHead);
            this.playerLeft.SetMassFromShapes();

            BodyDef playerRightDef = new BodyDef
            {
                FixedRotation = true,
                Position      = new Vec2(FieldWidth - PlayerXPosition, PlayerYPosition)
            };

            this.playerRight = this.world.CreateBody(playerRightDef);
            this.playerRight.CreateShape(playerBody);
            this.playerRight.CreateShape(playerHead);
            this.playerRight.SetMassFromShapes();

            BodyDef goalLeftDef = new BodyDef
            {
                Position = new Vec2(0.5f, 5.035f)
            };

            this.goalLeft = this.world.CreateBody(goalLeftDef);
            CircleDef goalLeftBarDef = new CircleDef
            {
                Radius        = 0.075f,
                LocalPosition = new Vec2(0.425f, 0),
                Friction      = 1,
                Restitution   = 0,
                UserData      = "Post"
            };
            PolygonDef goalLeftLineDef = new PolygonDef
            {
                Friction    = 1,
                Restitution = 0
            };

            goalLeftLineDef.SetAsBox(0.46f, 0.001f, new Vec2(-0.04f, -0.069f), 0);
            this.goalLeft.CreateShape(goalLeftBarDef);
            this.goalLeft.CreateShape(goalLeftLineDef);

            BodyDef goalRightDef = new BodyDef
            {
                Position = new Vec2(13.16f, 5.035f)
            };

            this.goalRight = this.world.CreateBody(goalRightDef);
            CircleDef goalRightBarDef = new CircleDef
            {
                Radius        = 0.075f,
                LocalPosition = new Vec2(-0.425f, 0),
                Friction      = 1,
                Restitution   = 0,
                UserData      = "Post"
            };
            PolygonDef goalRightLineDef = new PolygonDef
            {
                Friction    = 1,
                Restitution = 0
            };

            goalRightLineDef.SetAsBox(0.46f, 0.001f, new Vec2(0.04f, -0.069f), 0);
            this.goalRight.CreateShape(goalRightBarDef);
            this.goalRight.CreateShape(goalRightLineDef);

            BodyDef legLeftDef = new BodyDef
            {
                Position = new Vec2(playerLeftDef.Position.X, playerLeftDef.Position.Y + 0.2f)
            };

            this.legLeft = this.world.CreateBody(legLeftDef);
            PolygonDef legLeftShapeDef = new PolygonDef
            {
                Density     = 1.4f,
                Friction    = 3,
                Restitution = 0,
                Vertices    = new Vec2[] { new Vec2(0.12f, 0.03f), new Vec2(-0.07f, -0.06f), new Vec2(-0.03f, -0.15f) },
                VertexCount = 3
            };

            this.legLeft.CreateShape(legLeftShapeDef);
            this.legLeft.SetMassFromShapes();

            RevoluteJointDef jointLeftDef = new RevoluteJointDef
            {
                Body1          = this.playerLeft,
                Body2          = this.legLeft,
                LocalAnchor1   = new Vec2(0.05f, -0.4f),
                LocalAnchor2   = new Vec2(0, -0.64f),
                LowerAngle     = AngleConversion.DegreeToRadian(-18),
                UpperAngle     = AngleConversion.DegreeToRadian(13),
                EnableMotor    = true,
                EnableLimit    = true,
                MaxMotorTorque = 0.08f
            };

            this.jointLeft = this.world.CreateJoint(jointLeftDef);

            BodyDef legRightDef = new BodyDef
            {
                Position = new Vec2(playerRightDef.Position.X, playerRightDef.Position.Y + 0.2f)
            };

            this.legRight = this.world.CreateBody(legRightDef);
            PolygonDef legRightShapeDef = new PolygonDef
            {
                Density     = 1.4f,
                Friction    = 3,
                Restitution = 0,
                Vertices    = new Vec2[] { new Vec2(-0.12f, 0.03f), new Vec2(0.03f, -0.15f), new Vec2(0.07f, -0.06f) },
                VertexCount = 3
            };

            this.legRight.CreateShape(legRightShapeDef);
            this.legRight.SetMassFromShapes();

            RevoluteJointDef jointRightDef = new RevoluteJointDef
            {
                Body1          = this.playerRight,
                Body2          = this.legRight,
                LocalAnchor1   = new Vec2(-0.05f, -0.4f),
                LocalAnchor2   = new Vec2(0, -0.64f),
                LowerAngle     = AngleConversion.DegreeToRadian(-13),
                UpperAngle     = AngleConversion.DegreeToRadian(18),
                EnableMotor    = true,
                EnableLimit    = true,
                MaxMotorTorque = 0.08f
            };

            this.jointRight = this.world.CreateJoint(jointRightDef);

            this.playerLeftTimers.Defend  = 12000;
            this.playerRightTimers.Defend = 12000;

            this.listener.OnResult  += this.BallContact;
            this.listener.OnPersist += this.BallContacting;
            this.listener.OnAdd     += this.BallContacting;
            this.listener.OnAdd     += this.PlayerLeftContact;
            this.listener.OnAdd     += this.PlayerRightContact;
            this.listener.OnAdd     += this.PlayersTackle;
            this.listener.OnRemove  += this.PlayerLeftContactRemove;
            this.listener.OnRemove  += this.PlayerRightContactRemove;
            this.world.SetContactListener(this.listener);
        }