⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soccerbase.cpp

📁 linux 下的机器人足球仿真平台
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    {        base.GetLog()->Error() << "(SoccerBase) ERROR: no transform parent "                          << "found in GetBody()\n";        return false;    }  body = shared_dynamic_cast<Body>(parent->FindChildSupportingClass<Body>());  if (body.get() == 0)    {        base.GetLog()->Error()            << "ERROR: (SoccerBase: " << base.GetName()            << ") parent node has no Body child.";                return false;    }  return true;}boolSoccerBase::GetBall(const Leaf& base, shared_ptr<Ball>& ball){    static shared_ptr<Scene> scene;    static shared_ptr<Ball> ballRef;        if (scene.get() == 0)    {        if (! GetActiveScene(base,scene))        {            base.GetLog()->Error()                << "(SoccerBase) ERROR: " << base.GetName()                << ", could not get active scene.\n";                    return false;        }    }           if (ballRef.get() == 0)    {        ballRef = shared_dynamic_cast<Ball>            (base.GetCore()->Get(scene->GetFullPath() + "Ball"));                if (ballRef.get() == 0)        {            base.GetLog()->Error()                << "(SoccerBase) ERROR: " << base.GetName()                << ", found no ball node\n";                        return false;        }    }            ball = ballRef;    return true;}boolSoccerBase::GetBallBody(const Leaf& base, shared_ptr<Body>& body){    static shared_ptr<Scene> scene;    static shared_ptr<Body> bodyRef;        if (scene.get() == 0)    {        if (! GetActiveScene(base,scene))        {            base.GetLog()->Error()                << "(SoccerBase) ERROR: " << base.GetName()                << ", could not get active scene.\n";                    return false;        }    }     if (bodyRef.get() == 0)    {        bodyRef = shared_dynamic_cast<Body>            (base.GetCore()->Get(scene->GetFullPath() + "Ball/physics"));                if (bodyRef.get() == 0)        {            base.GetLog()->Error()                << "(SoccerBase) ERROR: " << base.GetName()                << ", found no ball body node\n";                            return false;        }    }        body = bodyRef;        return true;}boolSoccerBase::GetBallCollider(const zeitgeist::Leaf& base,                boost::shared_ptr<oxygen::SphereCollider>& sphere){    static shared_ptr<Scene> scene;    static shared_ptr<SphereCollider> sphereRef;        if (scene.get() == 0)    {        if (! GetActiveScene(base,scene))        {            base.GetLog()->Error()                << "(SoccerBase) ERROR: " << base.GetName()                << ", could not get active scene.\n";                    return false;        }    }    if (sphereRef.get() == 0)    {        sphereRef = shared_dynamic_cast<SphereCollider>            (base.GetCore()->Get(scene->GetFullPath() + "Ball/geometry"));                if (sphereRef.get() == 0)        {            base.GetLog()->Error()                << "(SoccerBase) ERROR:" << base.GetName()                << ", Ball got no SphereCollider node\n";                        return false;        }    }       sphere = sphereRef;      return true;}salt::Vector3fSoccerBase::FlipView(const salt::Vector3f& pos, TTeamIndex ti){    salt::Vector3f newPos;    switch (ti)    {    case TI_RIGHT:        newPos[0] = -pos[0];        newPos[1] = -pos[1];        newPos[2] = pos[2];        break;    case TI_NONE:    case TI_LEFT:        newPos = pos;        break;    }    return newPos;}TTeamIndexSoccerBase::OpponentTeam(TTeamIndex ti){    switch (ti)    {    case TI_RIGHT:        return TI_LEFT;    case TI_LEFT:        return TI_RIGHT;    default:        return TI_NONE;    }}stringSoccerBase::PlayMode2Str(const TPlayMode mode){    switch (mode)    {    case PM_BeforeKickOff:        return STR_PM_BeforeKickOff;    case PM_KickOff_Left:        return STR_PM_KickOff_Left;    case PM_KickOff_Right:        return STR_PM_KickOff_Right;    case PM_PlayOn:        return STR_PM_PlayOn;    case PM_KickIn_Left:        return STR_PM_KickIn_Left;    case PM_KickIn_Right:        return STR_PM_KickIn_Right;    case PM_CORNER_KICK_LEFT:        return STR_PM_CORNER_KICK_LEFT;    case PM_CORNER_KICK_RIGHT:        return STR_PM_CORNER_KICK_RIGHT;    case PM_GOAL_KICK_LEFT:        return STR_PM_GOAL_KICK_LEFT;    case PM_GOAL_KICK_RIGHT:        return STR_PM_GOAL_KICK_RIGHT;    case PM_OFFSIDE_LEFT:        return STR_PM_OFFSIDE_LEFT;    case PM_OFFSIDE_RIGHT:        return STR_PM_OFFSIDE_RIGHT;    case PM_GameOver:        return STR_PM_GameOver;    case PM_Goal_Left:        return STR_PM_Goal_Left;    case PM_Goal_Right:        return STR_PM_Goal_Right;    case PM_FREE_KICK_LEFT:        return STR_PM_FREE_KICK_LEFT;    case PM_FREE_KICK_RIGHT:        return STR_PM_FREE_KICK_RIGHT;    default:        return STR_PM_Unknown;    };}shared_ptr<ControlAspect>SoccerBase::GetControlAspect(const zeitgeist::Leaf& base,const string& name){  static const string gcsPath = "/sys/server/gamecontrol/";  shared_ptr<ControlAspect> aspect = shared_dynamic_cast<ControlAspect>    (base.GetCore()->Get(gcsPath + name));  if (aspect.get() == 0)    {        base.GetLog()->Error()            << "ERROR: (SoccerBase: " << base.GetName()            << ") found no ControlAspect " << name << "\n";    }  return aspect;}boolSoccerBase::MoveAgent(shared_ptr<Transform> agent_aspect, const Vector3f& pos){    Vector3f agentPos = agent_aspect->GetWorldTransform().Pos();        shared_ptr<Transform> parent = shared_dynamic_cast<Transform>            (agent_aspect->FindParentSupportingClass<Transform>().lock());    if (parent.get() == 0)    {        agent_aspect->GetLog()->Error() << "(MoveAgent) ERROR: can't get parent node.\n";        return false;    }    Leaf::TLeafList leafList;    parent->ListChildrenSupportingClass<Body>(leafList, true);    if (leafList.size() == 0)    {        agent_aspect->GetLog()->Error()            << "(MoveAgent) ERROR: agent aspect doesn't have "            << "children of type Body\n";        return false;    }    Leaf::TLeafList::iterator iter = leafList.begin();    // move all child bodies     for (iter; iter != leafList.end(); ++iter)    {                    shared_ptr<Body> childBody =             shared_dynamic_cast<Body>(*iter);        Vector3f childPos = childBody->GetPosition();        childBody->SetPosition(pos + (childPos-agentPos));        childBody->SetVelocity(Vector3f(0,0,0));        childBody->SetAngularVelocity(Vector3f(0,0,0));    }}boolSoccerBase::MoveAndRotateAgent(shared_ptr<Transform> agent_aspect, const Vector3f& pos, float angle){    Vector3f agentPos = agent_aspect->GetWorldTransform().Pos();        shared_ptr<Transform> parent = shared_dynamic_cast<Transform>            (agent_aspect->FindParentSupportingClass<Transform>().lock());    if (parent.get() == 0)    {        agent_aspect->GetLog()->Error() << "(MoveAndRotateAgent) ERROR: can't get parent node.\n";        return false;    }    Leaf::TLeafList leafList;    parent->ListChildrenSupportingClass<Body>(leafList, true);    if (leafList.size() == 0)    {        agent_aspect->GetLog()->Error()            << "(MoveAndRotateAgent) ERROR: agent aspect doesn't have "            << "children of type Body\n";        return false;    }        shared_ptr<Body> body;    GetAgentBody(agent_aspect, body);    Matrix bodyR = body->GetRotation();    bodyR.InvertRotationMatrix();    Matrix mat;    mat.RotationZ(gDegToRad(angle));    mat *= bodyR;    Leaf::TLeafList::iterator iter = leafList.begin();           // move all child bodies     for (iter;         iter != leafList.end();         ++iter         )        {            	       shared_ptr<Body> childBody =                 shared_dynamic_cast<Body>(*iter);	        	    Vector3f childPos = childBody->GetPosition();            Matrix childR = childBody->GetRotation();            childR = mat*childR;    	    childBody->SetPosition(pos + mat.Rotate(childPos-agentPos));    	    childBody->SetVelocity(Vector3f(0,0,0));    	    childBody->SetAngularVelocity(Vector3f(0,0,0));            childBody->SetRotation(childR);                	}}salt::AABB3 SoccerBase::GetAgentBoundingBox(const zeitgeist::Leaf& base){    salt::AABB3 boundingBox;    shared_ptr<Space> parent = base.FindParentSupportingClass<Space>().lock();    if (!parent)    {        base.GetLog()->Error()                << "(GetAgentBoundingBox) ERROR: can't get parent node.\n";        return boundingBox;    }        /* We can't simply use the GetWorldBoundingBox of the space node, becuase      * (at least currently) it'll return a wrong answer. Currently, the space     * object is always at (0,0,0) which is encapsulated in the result of it's     * GetWorldBoundingBox method call.     */            Leaf::TLeafList baseNodes;    parent->ListChildrenSupportingClass<BaseNode>(baseNodes);    if (baseNodes.empty())        base.GetLog()->Error()                << "(GetAgentBoundingBox) ERROR: space object doesn't have any"                << " children of type BaseNode.\n";    else        boundingBox =             shared_static_cast<BaseNode>(*baseNodes.begin())->GetWorldBoundingBox();    for (Leaf::TLeafList::iterator i = baseNodes.begin(); i!= baseNodes.end(); ++i)    {        shared_ptr<BaseNode> node = shared_static_cast<BaseNode>(*i);        boundingBox.Encapsulate(node->GetWorldBoundingBox());    }    return boundingBox;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -