Sign in to follow this  

How Do I Implement An OffSide in Simple Soccer

This topic is 1371 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hie guys i am trying to impplement the Offside rule in my soccer game. I am using Programming Game AI by example doingbthe java version.I have done throw in, goal kick and corner kick, now i have a problem on implementing the offside.Can anyone please tell me how I can do this

 

  /**
     *  this demo works on a fixed frame rate (60 by default) so we don't need
     *  to pass a time_elapsed as a parameter to the game entities
     */
    public void Update() throws InterruptedException {
        if (m_bPaused) {
            return;
        }

        //update the balls
        m_pBall.Update();

        //update the teams
        m_pRedTeam.Update();
        m_pBlueTeam.Update();

        //if a goal has been detected reset the pitch ready for kickoff
        if (m_pBlueGoal.Scored(m_pBall) || m_pRedGoal.Scored(m_pBall)) {
            m_bGameOn = false;

            //reset the ball                                                      
            m_pBall.PlaceAtPosition(new Vector2D((double) m_cxClient / 2.0, (double) m_cyClient / 2.0));

            //get the teams ready for kickoff
            m_pRedTeam.GetFSM().ChangeState(PrepareForKickOff.Instance());
            m_pBlueTeam.GetFSM().ChangeState(PrepareForKickOff.Instance());
        }
       //call is throw in method
       isThrowIn(m_pBall);
       //call is corner kick method
       isCornerKick(m_pBall);
       
       itsAnOffSide();
    }
    
    /*public void itsAnOffSide(){
        if(m_pRedTeam.InControl() && m_pRedTeam.isOffSideTeamPlayer()== m_pRedTeam.Receiver()){
            m_pRedTeam.GetFSM().ChangeState(PrepareForKickOff.Instance());
        }else if(m_pBlueTeam.InControl() && m_pRedTeam.isOffSideTeamPlayer()== m_pBlueTeam.Receiver()){
           m_pBlueTeam.GetFSM().ChangeState(PrepareForKickOff.Instance());
        }
    }*/
    //method to check if its a corner kick or not
    public void isCornerKick(final SoccerBall ball) throws InterruptedException{
        /*if ball intersects with redteam's goalline whilst the redteam
         * was in control its a corner kick
        */
         if(LineIntersection2D(ball.Pos(), ball.OldPos(), TopLeft, BottomLeft) && m_pRedTeam.InControl()){
            m_bGameOn = false;
            m_pBall.PlaceAtPosition(new Vector2D(0, 0));
            Thread.sleep(1000);
            m_pRedTeam.GetFSM().ChangeState(Defending.Instance());
            m_pBlueTeam.GetFSM().ChangeState(Attacking.Instance());
            //return true;
         }else
              /*if ball intersects with redteam's goalline whilst the blueteam
               * was in control its a goal kick
              */
             if(LineIntersection2D(ball.Pos(), ball.OldPos(), TopLeft, BottomLeft) && m_pBlueTeam.InControl()){
             m_bGameOn = false;
             m_pBall.PlaceAtPosition(new Vector2D(150, 100));
             Thread.sleep(1000);
             m_pRedTeam.GetFSM().ChangeState(Attacking.Instance());
             m_pBlueTeam.GetFSM().ChangeState(Defending.Instance());
           // return true;
         } else/*if ball intersects with blueteam's goalline whilst the blueteam
               * was in control its a corner kick
              */
        if(LineIntersection2D(ball.Pos(), ball.OldPos(), TopRight, BottomRight) && m_pBlueTeam.InControl()){
            m_bGameOn = false;
            m_pBall.PlaceAtPosition(new Vector2D(700, 400));
            m_pRedTeam.GetFSM().ChangeState(Attacking.Instance());
            m_pBlueTeam.GetFSM().ChangeState(Defending.Instance());
            //return true;
        }else/*if ball intersects with blueteam's goalline whilst the blueteam
               * was in control its a corner kick
              */
        if(LineIntersection2D(ball.Pos(), ball.OldPos(), TopRight, BottomRight) && m_pRedTeam.InControl()){
            m_bGameOn = false;
            m_pBall.PlaceAtPosition(new Vector2D(600, 250));
            m_pRedTeam.GetFSM().ChangeState(Defending.Instance());
            //m_pBlueTeam.SetControllingPlayer(m_pBlueTeam.GetPlayerFromID(tick));
            m_pBlueTeam.GetFSM().ChangeState(Attacking.Instance());
            //return true;
        }
        //return false;
    }
    
    /*
    *method to check if ita a throw in or not,
    *checks which team was in control whenn the ball intersected with the walls;
    ball position is reset and the controlling team return to its home region
    */
    public  void isThrowIn(final SoccerBall ball) throws InterruptedException {
        if (LineIntersection2D(ball.Pos(), ball.OldPos(), TopLeft, TopRight)&& m_pRedTeam.InControl()) {
            m_bGameOn = false;
            m_pBall.PlaceAtPosition(new Vector2D(ball.OldPos().x, ball.OldPos().y));
            Thread.sleep(1000);
            m_pRedTeam.GetFSM().ChangeState(PrepareForKickOff.Instance());
            //m_pBlueTeam.GetFSM().ChangeState(Defending.Instance());
            //return true;
        }else if (LineIntersection2D(ball.Pos(), ball.OldPos(), TopLeft, TopRight)&& m_pBlueTeam.InControl()) {
            m_bGameOn = false;
            m_pBall.PlaceAtPosition(new Vector2D(ball.OldPos().x, ball.OldPos().y));
            Thread.sleep(1000);
            m_pBlueTeam.GetFSM().ChangeState(PrepareForKickOff.Instance());
            //m_pBlueTeam.GetFSM().ChangeState(Defending.Instance());
            //return true;
        }else if(LineIntersection2D(ball.Pos(), ball.OldPos(), BottomLeft, BottomRight) && m_pRedTeam.InControl()){
            m_bGameOn = false;
            m_pBall.PlaceAtPosition(new Vector2D(ball.OldPos().x, ball.OldPos().y));
            Thread.sleep(1000);
            m_pRedTeam.GetFSM().ChangeState(PrepareForKickOff.Instance());
            //return true;
        }else if(LineIntersection2D(ball.Pos(), ball.OldPos(), BottomLeft, BottomRight) && m_pBlueTeam.InControl()){
            m_bGameOn = false;
            m_pBall.PlaceAtPosition(new Vector2D(ball.OldPos().x, ball.OldPos().y));
            Thread.sleep(1000);
            m_pBlueTeam.GetFSM().ChangeState(PrepareForKickOff.Instance());
            //return true;
        }
        //return false;
    }
/*
    method which returns the closest player to own goal
    */
     private PlayerBase CalculateClosestPlayerToOwnGoal(SoccerTeam team) {
         
         double ClosestSoFar = MaxFloat;
         for(int i =0; i < team.Members().size();i++){
             if(team.Members().get(i).m_PlayerRole == attacker || team.Members().get(i).m_PlayerRole == defender){
             double dist = Math.sqrt(Math.pow(team.Members().get(i).Pos().x - team.HomeGoal().Center().x,2));
             if( dist < ClosestSoFar){
                 
                  m_pPlayerClosestToHomeGoal = team.Members().get(i);
             }
                         
         }
         }
         return m_pPlayerClosestToHomeGoal;
    }
     /*
       *method which returns a player who is nearest to the opposition goal
     */
      private PlayerBase CalculateClosestPlayerToOppGoal(SoccerTeam team) {
         
         double ClosestSoFar = MaxFloat;
         for(int i =0; i< team.Members().size();i++){
             if(!team.Members().get(i).isControllingPlayer()){
             double dist = Math.sqrt(Math.pow(team.Members().get(i).Pos().x - team.OpponentsGoal().Center().x,2));
             if( dist < ClosestSoFar){
                 
                  m_pPlayerClosestToOppGoal = team.Members().get(i);
             }
         }
         }
         return m_pPlayerClosestToOppGoal;
    }
      
      public void itsAnOffSide(){
          if(m_pBlueTeam.HomeGoal().Center().x - CalculateClosestPlayerToOppGoal(m_pRedTeam).Pos().x
                  < m_pBlueTeam.HomeGoal().Center().x - CalculateClosestPlayerToOwnGoal(m_pBlueTeam).Pos().x
                  && m_pPlayerClosestToOppGoal.BallWithinReceivingRange()){
                  //){
             m_bGameOn = false;
               //reset the ball                                                      
            m_pBall.PlaceAtPosition(new Vector2D((double) m_cxClient / 2.0, (double) m_cyClient / 2.0));

            //get the teams ready for kickoff
            m_pRedTeam.GetFSM().ChangeState(PrepareForKickOff.Instance());
            m_pBlueTeam.GetFSM().ChangeState(PrepareForKickOff.Instance());
          }
      }

 

That is the code to do the calculations. Can someone tell me where Iam going wrong or if there is a better option

Share this post


Link to post
Share on other sites
Do you understand the offside rule? If you are in the opponents half when a forward pass by your own team is made, and you are nearer the goal than all but one defender (usually the goalie, but this is not a requirement) then you are offside. Let's ignore the "interfering with play" rule ;)

The important thing is you can only be offside when the pass is made, and you must be ahead of the ball (I.e. it is a forward pass)

Share this post


Link to post
Share on other sites

I don't think he was asking for what the offside rule is, but rather how he could implement it.

 

A very basic version could be as simple as storing a list of offside players each time the ball is played (other than during exceptions such as throw-ins) by calculating the offside line and classifying players as offside or not. At each ball touch you would see if any player you consider active was in the offside list, and then you could call the offside. The list would be cleared at each ball touch. How you determine a player is active is up to how nuanced you want your game to be. It could be as simple as if the touching player is in the offside list, or maybe more complicated, such as there is an offside player within a certain distance of the ball, or a player affected the keeper's line of sight to the ball.

 

There are a lot of special cases to handle as well, such as a rebound off the keeper to a player who was in an offside position at the time of the shot.

 

In real life, the referee doesn't make his decision at the time the ball is touched, he is continually evaluating the play until he deems that no offside players had gained an advantage, or "become active". Continuously evaluating the offside conditions each frame (until the next time the ball is played or offside is called) is the best way to accurately model the behaviour of a referee. This approach would still rebuild an offside list at the time the ball is played, but instead of evaluating offside only at the time of touch, you would evaluate the game context each frame to see if any player in the offside list is considered to be active.

Share this post


Link to post
Share on other sites

Ok, maybe Paradigm shifter was correct. Your logic isn't appropriate for the offside call.

 

A player is in an OFFSIDE POSITION if he is nearer to the attacking goal than the ball, is on the attacking half, and has one or fewer opponents (including keeper) nearer to the attacking goal. It is not just the attacker that is closest to goal (all 11 players can theoretically be in an offside position). Players can not be in an offside position from a throw-in.

 

A player can only be called OFFSIDE if he becomes active and the ball was played when he was in an offside position.

 

So, to implement this...

 

When ball is played:

-Track offside players

 

Each frame:

-Test to see if any offside players are active, if they are, blow the whistle.

-Evaluate whether you can remove players from the offside list (long enough time, ball controlled relatively safely, play stopped for some other reason).

Share this post


Link to post
Share on other sites

This topic is 1371 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this