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); }
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; }
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; }
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; }
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); }