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