How Do I Implement An OffSide in Simple Soccer

Started by
2 comments, last by cardinal 10 years ago

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

Advertisement
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)
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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.

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

This topic is closed to new replies.

Advertisement