From: Oliver O. <fr...@us...> - 2007-03-07 10:39:33
|
Update of /cvsroot/simspark/simspark/simulations/soccer/plugin/ballstateaspect In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv24718/ballstateaspect Added Files: Tag: projectx ballstateaspect.cpp ballstateaspect.h ballstateaspect_c.cpp Log Message: soccer plugins from rcssserver3D --- NEW FILE: ballstateaspect.cpp --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: ballstateaspect.cpp,v 1.1.2.1 2007/03/07 10:39:20 fruit Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "ballstateaspect.h" #include <zeitgeist/logserver/logserver.h> #include <oxygen/sceneserver/sceneserver.h> #include <oxygen/sceneserver/scene.h> #include <oxygen/agentaspect/agentaspect.h> #include <oxygen/physicsserver/recorderhandler.h> #include <soccer/gamestateaspect/gamestateaspect.h> #include <soccer/soccerbase/soccerbase.h> #include <soccer/ball/ball.h> using namespace oxygen; using namespace boost; using namespace std; using namespace salt; BallStateAspect::BallStateAspect() : SoccerControlAspect() { mBallOnField = false; mLastValidBallPos = Vector3f(0,0,0); mGoalState = TI_NONE; mLastAgentCollisionTime = 0; } BallStateAspect::~BallStateAspect() { } bool BallStateAspect::GetLastCollidingAgent(shared_ptr<AgentAspect>& agent, TTime& time) { agent = mLastCollidingAgent; time = mLastAgentCollisionTime; return (agent.get() != 0); } bool BallStateAspect::GetLastKickingAgent(shared_ptr<AgentAspect>& agent, TTime& time) { agent = mLastKickingAgent; time = mLastAgentKickTime; return (agent.get() != 0); } void BallStateAspect::UpdateLastCollidingAgent() { // get a list of agents that collided with the ball since the last // update of the recorder and remember the first returned node as // the last agent that collided with the ball. RecorderHandler::TParentList agents; mBallRecorder->GetParentsSupportingClass("AgentAspect",agents); if (agents.size() > 0) { mLastCollidingAgent = shared_static_cast<AgentAspect> (make_shared(agents.front())); mLastAgentCollisionTime = mGameState->GetTime(); } // empty the recorder buffer mBallRecorder->Clear(); } void BallStateAspect::UpdateLastCollidingAgent(boost::shared_ptr<AgentAspect> agent) { mLastCollidingAgent = agent; mLastAgentCollisionTime = mGameState->GetTime(); } void BallStateAspect::UpdateLastKickingAgent(boost::shared_ptr<AgentAspect> agent) { mLastKickingAgent = agent; mLastAgentKickTime = mGameState->GetTime(); } void BallStateAspect::UpdateBallOnField() { // get a list of Ball nodes that collided with the field RecorderHandler::TParentList ball; mFieldRecorder->GetParentsSupportingClass("Ball",ball); // the ball is on or above the playing field iff it collided with // the box collider around the playing field mBallOnField = (ball.size() > 0); // empty the recorder buffer mFieldRecorder->Clear(); } void BallStateAspect::UpdateLastValidBallPos() { if (! mBallOnField) { return; } mLastValidBallPos = mBall->GetWorldTransform().Pos(); } void BallStateAspect::UpdateGoalState() { // check both goal box collider RecorderHandler::TParentList ball; mLeftGoalRecorder->GetParentsSupportingClass("Ball",ball); if (! ball.empty()) { mGoalState = TI_LEFT; } else { mRightGoalRecorder->GetParentsSupportingClass("Ball",ball); if (! ball.empty()) { mGoalState = TI_RIGHT; } else { mGoalState = TI_NONE; } } mLeftGoalRecorder->Clear(); mRightGoalRecorder->Clear(); } TTeamIndex BallStateAspect::GetGoalState() { return mGoalState; } void BallStateAspect::Update(float deltaTime) { if ( (mFieldRecorder.get() == 0) || (mBall.get() == 0) || (mBallRecorder.get() == 0) || (mLeftGoalRecorder.get() == 0) || (mRightGoalRecorder.get() == 0) ) { return; } UpdateLastCollidingAgent(); UpdateBallOnField(); UpdateLastValidBallPos(); UpdateGoalState(); } void BallStateAspect::OnLink() { SoccerControlAspect::OnLink(); mFieldRecorder = GetFieldRecorder(); SoccerBase::GetBall(*this,mBall); mBallRecorder = GetBallRecorder(); mLeftGoalRecorder = GetLeftGoalRecorder(); mRightGoalRecorder = GetRightGoalRecorder(); mGameState = shared_dynamic_cast<GameStateAspect> (GetControlAspect("GameStateAspect")); } void BallStateAspect::OnUnlink() { SoccerControlAspect::OnUnlink(); mBallRecorder.reset(); mFieldRecorder.reset(); mLastCollidingAgent.reset(); mLeftGoalRecorder.reset(); mRightGoalRecorder.reset(); mGameState.reset(); } bool BallStateAspect::GetBallOnField() { return mBallOnField; } salt::Vector3f BallStateAspect::GetLastValidBallPosition() { return mLastValidBallPos; } --- NEW FILE: ballstateaspect.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: ballstateaspect.h,v 1.1.2.1 2007/03/07 10:39:20 fruit Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef BALLLSTATEASPECT_H #define BALLLSTATEASPECT_H #include <soccer/soccercontrolaspect/soccercontrolaspect.h> /** BallStateAspect is a ControlAspect that holds information about the current state of the ball in the simulation. */ class Ball; namespace oxygen { class RecorderHandler; class AgentAspect; } class BallStateAspect : public SoccerControlAspect { public: BallStateAspect(); virtual ~BallStateAspect(); /** called during the update of the GameControlServer to allow the ControlAspect to perform any necessary checks. */ virtual void Update(float deltaTime); /** returns the last agent that collided with the ball and the time when this happened*/ bool GetLastCollidingAgent (boost::shared_ptr<oxygen::AgentAspect>& agent, TTime& time); /** returns the last agent that kicked the ball and the time when this happened*/ bool GetLastKickingAgent (boost::shared_ptr<oxygen::AgentAspect>& agent, TTime& time); /** returns true if the ball over the playing field */ bool GetBallOnField(); /** returns the last valid position of the ball over the playing field */ salt::Vector3f GetLastValidBallPosition(); /** returns the goal in which the balls position is or TI_NONE otherwise */ TTeamIndex GetGoalState(); /** updates the reference to the last agent that collided with the ball */ void UpdateLastCollidingAgent(boost::shared_ptr<oxygen::AgentAspect> agent); /** updates the reference to the last agent that kicked the ball */ void UpdateLastKickingAgent(boost::shared_ptr<oxygen::AgentAspect> agent); protected: /** set up the reference to the ball and field collider */ virtual void OnLink(); /** reset the reference to the ball and field recorder */ virtual void OnUnlink(); /** updates the reference to the last agent that collided with the ball */ void UpdateLastCollidingAgent(); /** checks if the ball is on the playing field an updates the mBallOnField flag */ void UpdateBallOnField(); /** checks if the ball is in one of the goals and updates the mGoalState member*/ void UpdateGoalState(); /** if the ball has a valid position, i.e. is on the field, remember it */ void UpdateLastValidBallPos(); protected: /** reference to the Ball node */ boost::shared_ptr<Ball> mBall; /** reference to the Ball collision recorder */ boost::shared_ptr<oxygen::RecorderHandler> mBallRecorder; /** reference to the field collider */ boost::shared_ptr<oxygen::RecorderHandler> mFieldRecorder; /** reference to the left goal recorder */ boost::shared_ptr<oxygen::RecorderHandler> mLeftGoalRecorder; /** reference to the right goal recorder */ boost::shared_ptr<oxygen::RecorderHandler> mRightGoalRecorder; /** holds a reference to the last agent that collided with the ball */ boost::shared_ptr<oxygen::AgentAspect> mLastCollidingAgent; /** holds a reference to the last agent that kicked the ball */ boost::shared_ptr<oxygen::AgentAspect> mLastKickingAgent; /** holds a reference to the GameStateAspect */ boost::shared_ptr<GameStateAspect> mGameState; /** then time when the last agent collided with the ball */ TTime mLastAgentCollisionTime; /** then time when the last agent kicked the ball */ TTime mLastAgentKickTime; /** true if the ball on the soccer field, i.e. not on the border surrounding the soccer field */ bool mBallOnField; /** holds the last valid ball position */ salt::Vector3f mLastValidBallPos; /** indicates in which goal the ball is, TI_NONE otherwise */ TTeamIndex mGoalState; }; DECLARE_CLASS(BallStateAspect); #endif // BALLLSTATEASPECT_H --- NEW FILE: ballstateaspect_c.cpp --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: ballstateaspect_c.cpp,v 1.1.2.1 2007/03/07 10:39:21 fruit Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "ballstateaspect.h" using namespace oxygen; void CLASS(BallStateAspect)::DefineClass() { DEFINE_BASECLASS(SoccerControlAspect); } |