• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Andrew Blem

How Do I Implement An OffSide in Simple Soccer

3 posts in this topic

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

0

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)
0

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.

0

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).

0

Share this post


Link to post
Share on other sites

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  
Followers 0