public bool CheckIfGoalScored(SoccerBall ball) { bool scored = Geometry.LineIntersection2D(ball.Position, ball.OldPosition, _leftPost, _rightPost); if( scored ) { _goalsScored++; } return scored; }
public bool CheckIfGoalScored(SoccerBall ball) { bool scored = Geometry.LineIntersection2D(ball.Position, ball.OldPosition, _leftPost, _rightPost); if (scored) { _goalsScored++; } return(scored); }
public SteeringBehaviors(PlayerBase player, SoccerBall ball) { _player = player; _ball = ball; _separationMultiple = ParameterManager.Instance.SeparationCoefficient; _viewDistance = ParameterManager.Instance.ViewDistance; _tagged = false; _interposeDist = 0.0; _behaviorFlags = (int)BehaviorFlags.None; _dribbleFeelers = new List <Vector2D>(5); for (int i = 0; i < _dribbleFeelers.Count; i++) { _dribbleFeelers[i] = new Vector2D(); } }
/// <summary> /// This behavior creates a force that steers the agent towards the // ball /// </summary> /// <param name="ball"></param> /// <returns></returns> protected Vector2D calculatePursuitVector(SoccerBall ball) { Vector2D toBall = ball.Position - _player.Position; //the lookahead time is proportional to the distance between the ball //and the pursuer; double lookAheadTime = 0.0; if (Math.Abs(ball.Speed) > Geometry.MinPrecision) { lookAheadTime = toBall.Length / ball.Speed; } //calculate where the ball will be at this time in the future _target = ball.CalculateFuturePosition(lookAheadTime); //now seek to the predicted future position of the ball return(calculateArriveVector(_target, DecelerationState.Fast)); }
public override void Execute(FieldPlayer player) { Random random = new Random(); //calculate the dot product of the vector pointing to the ball //and the player's heading Vector2D ToBall = player.Ball.Position - player.Position; double dot = player.Heading.GetDotProduct(Vector2D.Vec2DNormalize(ToBall)); //cannot kick the ball if the goalkeeper is in possession or if it is //behind the player or if there is already an assigned receiver. So just //continue chasing the ball if (player.Team.ReceivingPlayer != null || player.Pitch.GoalKeeperHasBall || (dot < 0)) { System.Diagnostics.Debug.WriteLine("Goaly has ball / ball behind player"); player.StateMachine.ChangeState(ChaseBallState.Instance); return; } /* Attempt a shot at the goal */ //if a shot is possible, this vector will hold the position along the //opponent's goal line the player should aim for. Vector2D BallTarget = new Vector2D(); //the dot product is used to adjust the shooting force. The more //directly the ball is ahead, the more forceful the kick double power = ParameterManager.Instance.MaxShootingForce * dot; double distance = player.Position.Distance(player.Team.OpponentsGoal.GoalLineCenter); //if it is determined that the player could score a goal from this position //OR if he should just kick the ball anyway, the player will attempt //to make the shot if (player.Team.CanShoot(player.Ball.Position, power, ref BallTarget) || (random.NextDouble() < ParameterManager.Instance.ChancePlayerAttemptsPotShot) || distance < player.Pitch.PlayingArea.Width / 8) { System.Diagnostics.Debug.WriteLine(string.Format("Player {0} attempts a shot at ({1}, {2})", player.ObjectId, BallTarget.X, BallTarget.Y)); //add some noise to the kick. We don't want players who are //too accurate! The amount of noise can be adjusted by altering //Prm.PlayerKickingAccuracy BallTarget = SoccerBall.AddNoiseToKick(player.Ball.Position, BallTarget); //this is the direction the ball will be kicked in Vector2D KickDirection = BallTarget - player.Ball.Position; player.Ball.Kick(KickDirection, power); //change state player.StateMachine.ChangeState(WaitState.Instance); player.FindSupport(); return; } /* Attempt a pass to a player */ //if a receiver is found this will point to it PlayerBase receiver = null; power = ParameterManager.Instance.MaxPassingForce * dot; //test if there are any potential candidates available to receive a pass if (player.IsThreatened() && player.Team.FindPass(player, out receiver, out BallTarget, power, ParameterManager.Instance.MinPassDist)) { //add some noise to the kick BallTarget = SoccerBall.AddNoiseToKick(player.Ball.Position, BallTarget); Vector2D KickDirection = BallTarget - player.Ball.Position; player.Ball.Kick(KickDirection, power); System.Diagnostics.Debug.WriteLine(string.Format("Player {0} passes the ball with force {1} to player {2} Target is ({3},{4})", player.ObjectId, power, receiver.ObjectId, BallTarget.X, BallTarget.Y)); //let the receiver know a pass is coming MessageDispatcher.Instance.DispatchMsg( new TimeSpan(0), player.ObjectId, receiver.ObjectId, (int)SoccerGameMessages.ReceiveBall, BallTarget); //the player should wait at his current position unless instruced //otherwise player.StateMachine.ChangeState(WaitState.Instance); player.FindSupport(); return; } //cannot shoot or pass, so dribble the ball upfield else { player.FindSupport(); player.StateMachine.ChangeState(DribbleState.Instance); } }
public SteeringBehaviors(PlayerBase player, SoccerBall ball) { _player = player; _ball = ball; _separationMultiple = ParameterManager.Instance.SeparationCoefficient; _viewDistance = ParameterManager.Instance.ViewDistance; _tagged = false; _interposeDist = 0.0; _behaviorFlags = (int)BehaviorFlags.None; _dribbleFeelers = new List<Vector2D>(5); for (int i = 0; i < _dribbleFeelers.Count; i++) { _dribbleFeelers[i] = new Vector2D(); } }
/// <summary> /// Given an opponent and an object position this method returns a /// force that attempts to position the agent between them /// </summary> /// <param name="ball"></param> /// <param name="target"></param> /// <param name="distFromTarget"></param> /// <returns></returns> protected Vector2D calculateInterposeVector(SoccerBall ball, Vector2D target, double distFromTarget) { return calculateArriveVector(target + Vector2D.Vec2DNormalize(ball.Position - target) * distFromTarget, DecelerationState.Normal); }
/// <summary> /// This behavior creates a force that steers the agent towards the // ball /// </summary> /// <param name="ball"></param> /// <returns></returns> protected Vector2D calculatePursuitVector(SoccerBall ball) { Vector2D toBall = ball.Position - _player.Position; //the lookahead time is proportional to the distance between the ball //and the pursuer; double lookAheadTime = 0.0; if (Math.Abs(ball.Speed) > Geometry.MinPrecision) { lookAheadTime = toBall.Length / ball.Speed; } //calculate where the ball will be at this time in the future _target = ball.CalculateFuturePosition(lookAheadTime); //now seek to the predicted future position of the ball return calculateArriveVector(_target, DecelerationState.Fast); }
public SoccerPitch(double width, double height) { //int GOAL_WIDTH = 2; //double ballSize = .5; //double ballMass = 1.0; _walls = new List <Wall2D>(); _clientWidth = (int)width; _clientHeight = (int)height; _paused = false; _goalKeeperHasBall = false; _gameInPlay = true; //define the playing area _playingArea = new Region(20, _clientHeight - 20, 20, _clientWidth - 20); //create the regions createRegions(_playingArea.Width / (double)NumRegionsHorizontal, _playingArea.Height / (double)NumRegionsVertical); //create the goals _redGoal = new SoccerGoal(new Vector2D(_playingArea.Left, (_clientHeight - ParameterManager.Instance.GoalWidth) / 2), new Vector2D(_playingArea.Left, _clientHeight - (_clientHeight - ParameterManager.Instance.GoalWidth) / 2), new Vector2D(1.0, 0.0)); _blueGoal = new SoccerGoal(new Vector2D(_playingArea.Right, (_clientHeight - ParameterManager.Instance.GoalWidth) / 2), new Vector2D(_playingArea.Right, _clientHeight - (_clientHeight - ParameterManager.Instance.GoalWidth) / 2), new Vector2D(-1, 0)); //create the soccer ball _ball = new SoccerBall(new Vector2D(_clientWidth / 2.0, _clientHeight / 2.0), ParameterManager.Instance.BallSize, ParameterManager.Instance.BallMass, _walls); //create the teams _redTeam = new SoccerTeam(_redGoal, _blueGoal, this, SoccerTeam.SoccerTeamColor.Red); _blueTeam = new SoccerTeam(_blueGoal, _redGoal, this, SoccerTeam.SoccerTeamColor.Blue); //make sure each team knows who their opponents are _redTeam.OpposingTeam = _blueTeam; _blueTeam.OpposingTeam = _redTeam; //create the walls Vector2D TopLeft = new Vector2D(_playingArea.Left, _playingArea.Top); Vector2D TopRight = new Vector2D(_playingArea.Right, _playingArea.Top); Vector2D BottomRight = new Vector2D(_playingArea.Right, _playingArea.Bottom); Vector2D BottomLeft = new Vector2D(_playingArea.Left, _playingArea.Bottom); _walls.Add(new Wall2D(BottomLeft, _redGoal.RightPost)); _walls.Add(new Wall2D(_redGoal.LeftPost, TopLeft)); _walls.Add(new Wall2D(TopLeft, TopRight)); _walls.Add(new Wall2D(TopRight, _blueGoal.LeftPost)); _walls.Add(new Wall2D(_blueGoal.RightPost, BottomRight)); _walls.Add(new Wall2D(BottomRight, BottomLeft)); }
/// <summary> /// Given an opponent and an object position this method returns a /// force that attempts to position the agent between them /// </summary> /// <param name="ball"></param> /// <param name="target"></param> /// <param name="distFromTarget"></param> /// <returns></returns> protected Vector2D calculateInterposeVector(SoccerBall ball, Vector2D target, double distFromTarget) { return(calculateArriveVector(target + Vector2D.Vec2DNormalize(ball.Position - target) * distFromTarget, DecelerationState.Normal)); }
public SoccerPitch(double width, double height) { //int GOAL_WIDTH = 2; //double ballSize = .5; //double ballMass = 1.0; _walls = new List<Wall2D>(); _clientWidth = (int)width; _clientHeight = (int)height; _paused = false; _goalKeeperHasBall = false; _gameInPlay = true; //define the playing area _playingArea = new Region(20, _clientHeight - 20, 20, _clientWidth - 20); //create the regions createRegions(_playingArea.Width / (double)NumRegionsHorizontal, _playingArea.Height / (double)NumRegionsVertical); //create the goals _redGoal = new SoccerGoal(new Vector2D(_playingArea.Left, (_clientHeight - ParameterManager.Instance.GoalWidth) / 2), new Vector2D(_playingArea.Left, _clientHeight - (_clientHeight - ParameterManager.Instance.GoalWidth) / 2), new Vector2D(1.0, 0.0)); _blueGoal = new SoccerGoal(new Vector2D(_playingArea.Right, (_clientHeight - ParameterManager.Instance.GoalWidth) / 2), new Vector2D(_playingArea.Right, _clientHeight - (_clientHeight - ParameterManager.Instance.GoalWidth) / 2), new Vector2D(-1, 0)); //create the soccer ball _ball = new SoccerBall(new Vector2D(_clientWidth / 2.0, _clientHeight / 2.0), ParameterManager.Instance.BallSize, ParameterManager.Instance.BallMass, _walls); //create the teams _redTeam = new SoccerTeam(_redGoal, _blueGoal, this, SoccerTeam.SoccerTeamColor.Red); _blueTeam = new SoccerTeam(_blueGoal, _redGoal, this, SoccerTeam.SoccerTeamColor.Blue); //make sure each team knows who their opponents are _redTeam.OpposingTeam = _blueTeam; _blueTeam.OpposingTeam = _redTeam; //create the walls Vector2D TopLeft = new Vector2D(_playingArea.Left, _playingArea.Top); Vector2D TopRight = new Vector2D(_playingArea.Right, _playingArea.Top); Vector2D BottomRight = new Vector2D(_playingArea.Right, _playingArea.Bottom); Vector2D BottomLeft = new Vector2D(_playingArea.Left, _playingArea.Bottom); _walls.Add(new Wall2D(BottomLeft, _redGoal.RightPost)); _walls.Add(new Wall2D(_redGoal.LeftPost, TopLeft)); _walls.Add(new Wall2D(TopLeft, TopRight)); _walls.Add(new Wall2D(TopRight, _blueGoal.LeftPost)); _walls.Add(new Wall2D(_blueGoal.RightPost, BottomRight)); _walls.Add(new Wall2D(BottomRight, BottomLeft)); }