踢球的方法2

#include <iostream>
#include <rcssclient/agent.h>
#include <rcssclient/parser.h>
#include <rcssclient/socket.h>
#include <rcssclient/command.h>

using namespace rcss;
using namespace rcss::client;

class MyAgent : public Agent {
public:
    MyAgent() {}

    // 当接收到服务器消息时调用
    void handleServerMsg( const char * msg ) override {
        Parser parser;
        MsgParser::Ptr msg_parser = parser.parse( msg );
        if ( msg_parser ) {
            // 这里可以处理不同类型的消息,例如视觉信息、听觉信息等
            // 以下是一个简单的示例,假设接收到消息后,机器人向球的方向移动
            if ( msg_parser->getType() == MsgParser::T_VISUAL ) {
                VisualInfo::Ptr visual_info = std::dynamic_pointer_cast< VisualInfo >( msg_parser );
                if ( visual_info ) {
                    // 查找球的位置
                    VisualInfo::ObjectMap::const_iterator ball_it = visual_info->objects().find( "ball" );
                    if ( ball_it != visual_info->objects().end() ) {
                        const VisualObject& ball = ball_it->second;
                        // 获取球的相对位置
                        double ball_x = ball.pos().x();
                        double ball_y = ball.pos().y();

                        // 简单的移动策略:向球的方向移动
                        if ( ball_x > 0 ) {
                            // 向前移动
                            sendCommand( new CmdDash( 100.0 ) );
                        } else if ( ball_x < 0 ) {
                            // 向后移动
                            sendCommand( new CmdDash( -100.0 ) );
                        }
                        if ( ball_y > 0 ) {
                            // 向右转动
                            sendCommand( new CmdTurn( 10.0 ) );
                        } else if ( ball_y < 0 ) {
                            // 向左转动
                            sendCommand( new CmdTurn( -10.0 ) );
                        }
                    }
                }
            }
        }
    }
};

int main() {
    MyAgent agent;
    // 连接到服务器
    if ( agent.connect( "localhost", 6000 ) ) {
        std::cout << "Connected to the server." << std::endl;
        // 注册球员
        agent.sendCommand( new CmdInit( "player", 0, "default" ) );
        // 进入主循环
        agent.run();
    } else {
        std::cerr << "Failed to connect to the server." << std::endl;
    }
    return 0;
}
#include <iostream>
#include <rcssclient/agent.h>
#include <rcssclient/parser.h>
#include <rcssclient/socket.h>
#include <rcssclient/command.h>

using namespace rcss;
using namespace rcss::client;

class MoveToBallAgent : public Agent {
public:
    MoveToBallAgent() {}

    void handleServerMsg(const char* msg) override {
        Parser parser;
        MsgParser::Ptr msg_parser = parser.parse(msg);
        if (msg_parser) {
            if (msg_parser->getType() == MsgParser::T_VISUAL) {
                VisualInfo::Ptr visual_info = std::dynamic_pointer_cast<VisualInfo>(msg_parser);
                if (visual_info) {
                    // 查找球的位置
                    auto ball_it = visual_info->objects().find("ball");
                    if (ball_it != visual_info->objects().end()) {
                        const VisualObject& ball = ball_it->second;
                        double ball_x = ball.pos().x();
                        double ball_y = ball.pos().y();

                        // 简单的移动策略
                        if (ball_x > 0.5) {
                            sendCommand(new CmdDash(100.0));
                        } else if (ball_x < -0.5) {
                            sendCommand(new CmdDash(-100.0));
                        }
                        if (ball_y > 0.5) {
                            sendCommand(new CmdTurn(10.0));
                        } else if (ball_y < -0.5) {
                            sendCommand(new CmdTurn(-10.0));
                        }
                    }
                }
            }
        }
    }
};

int main() {
    MoveToBallAgent agent;
    if (agent.connect("localhost", 6000)) {
        std::cout << "Connected to the server." << std::endl;
        agent.sendCommand(new CmdInit("player", 0, "default"));
        agent.run();
    } else {
        std::cerr << "Failed to connect to the server." << std::endl;
    }
    return 0;
}
#include <iostream>
#include <rcssclient/agent.h>
#include <rcssclient/parser.h>
#include <rcssclient/socket.h>
#include <rcssclient/command.h>

using namespace rcss;
using namespace rcss::client;

class PassingAgent : public Agent {
public:
    PassingAgent() {}

    void handleServerMsg(const char* msg) override {
        Parser parser;
        MsgParser::Ptr msg_parser = parser.parse(msg);
        if (msg_parser) {
            if (msg_parser->getType() == MsgParser::T_VISUAL) {
                VisualInfo::Ptr visual_info = std::dynamic_pointer_cast<VisualInfo>(msg_parser);
                if (visual_info) {
                    // 查找球的位置
                    auto ball_it = visual_info->objects().find("ball");
                    if (ball_it != visual_info->objects().end()) {
                        const VisualObject& ball = ball_it->second;
                        double ball_dist = ball.dist();

                        // 如果球离得很近,尝试传球
                        if (ball_dist < 1.0) {
                            // 简单假设向前传球
                            sendCommand(new CmdKick(100.0, 0.0));
                        }
                    }
                }
            }
        }
    }
};

int main() {
    PassingAgent agent;
    if (agent.connect("localhost", 6000)) {
        std::cout << "Connected to the server." << std::endl;
        agent.sendCommand(new CmdInit("player", 0, "default"));
        agent.run();
    } else {
        std::cerr << "Failed to connect to the server." << std::endl;
    }
    return 0;
}
#include <iostream>
#include <rcssclient/agent.h>
#include <rcssclient/parser.h>
#include <rcssclient/socket.h>
#include <rcssclient/command.h>

using namespace rcss;
using namespace rcss::client;

class BallTrackingAgent : public Agent {
public:
    BallTrackingAgent() {}

    void handleServerMsg(const char* msg) override {
        Parser parser;
        MsgParser::Ptr msg_parser = parser.parse(msg);
        if (msg_parser) {
            if (msg_parser->getType() == MsgParser::T_VISUAL) {
                VisualInfo::Ptr visual_info = std::dynamic_pointer_cast<VisualInfo>(msg_parser);
                if (visual_info) {
                    // 查找球的位置和速度
                    auto ball_it = visual_info->objects().find("ball");
                    if (ball_it != visual_info->objects().end()) {
                        const VisualObject& ball = ball_it->second;
                        // 获取球的位置
                        double ball_x = ball.pos().x();
                        double ball_y = ball.pos().y();
                        // 获取球的速度
                        double ball_vx = ball.vel().x();
                        double ball_vy = ball.vel().y();

                        // 预测球在未来1秒的位置
                        double future_ball_x = ball_x + ball_vx * 1.0;
                        double future_ball_y = ball_y + ball_vy * 1.0;

                        // 计算机器人需要移动的方向和距离
                        double dx = future_ball_x;
                        double dy = future_ball_y;

                        // 简单的移动策略
                        if (dx > 0.5) {
                            sendCommand(new CmdDash(100.0));
                        } else if (dx < -0.5) {
                            sendCommand(new CmdDash(-100.0));
                        }
                        if (dy > 0.5) {
                            sendCommand(new CmdTurn(10.0));
                        } else if (dy < -0.5) {
                            sendCommand(new CmdTurn(-10.0));
                        }
                    }
                }
            }
        }
    }
};

int main() {
    BallTrackingAgent agent;
    if (agent.connect("localhost", 6000)) {
        std::cout << "Connected to the server." << std::endl;
        agent.sendCommand(new CmdInit("player", 0, "default"));
        agent.run();
    } else {
        std::cerr << "Failed to connect to the server." << std::endl;
    }
    return 0;
}
#include <Eigen/Dense>
#include <iostream>

int main() {
    Eigen::Vector2d ball_position(1.0, 2.0);
    Eigen::Vector2d ball_velocity(0.5, 0.3);
    // 预测未来1秒的位置
    Eigen::Vector2d future_position = ball_position + ball_velocity * 1.0;
    std::cout << "Future position: " << future_position.transpose() << std::endl;
    return 0;
}
#include <iostream>
#include <rcssclient/agent.h>
#include <rcssclient/parser.h>
#include <rcssclient/socket.h>
#include <rcssclient/command.h>

using namespace rcss;
using namespace rcss::client;

class BallTrackingAgent : public Agent {
public:
    BallTrackingAgent() {}

    // 处理服务器发送的消息
    void handleServerMsg(const char* msg) override {
        Parser parser;
        MsgParser::Ptr msg_parser = parser.parse(msg);
        if (msg_parser) {
            // 处理视觉信息
            if (msg_parser->getType() == MsgParser::T_VISUAL) {
                VisualInfo::Ptr visual_info = std::dynamic_pointer_cast<VisualInfo>(msg_parser);
                if (visual_info) {
                    // 查找球的信息
                    auto ball_it = visual_info->objects().find("ball");
                    if (ball_it != visual_info->objects().end()) {
                        const VisualObject& ball = ball_it->second;

                        // 获取球的位置
                        double ball_x = ball.pos().x();
                        double ball_y = ball.pos().y();

                        // 获取球的速度
                        double ball_vx = ball.vel().x();
                        double ball_vy = ball.vel().y();

                        // 预测球在未来 1 秒的位置
                        double future_ball_x = ball_x + ball_vx * 1.0;
                        double future_ball_y = ball_y + ball_vy * 1.0;

                        // 计算机器人需要移动的方向和距离
                        double dx = future_ball_x;
                        double dy = future_ball_y;

                        // 设定移动阈值,避免微小波动导致频繁动作
                        const double threshold = 0.1;

                        // 根据预测位置计算移动指令
                        if (dx > threshold) {
                            // 向前移动
                            sendCommand(new CmdDash(100.0));
                        } else if (dx < -threshold) {
                            // 向后移动
                            sendCommand(new CmdDash(-100.0));
                        }

                        if (dy > threshold) {
                            // 右转
                            sendCommand(new CmdTurn(10.0));
                        } else if (dy < -threshold) {
                            // 左转
                            sendCommand(new CmdTurn(-10.0));
                        }
                    }
                }
            }
        }
    }
};

int main() {
    BallTrackingAgent agent;
    // 连接到服务器,默认地址为 localhost,端口为 6000
    if (agent.connect("localhost", 6000)) {
        std::cout << "Connected to the server." << std::endl;
        // 初始化球员,角色为 player,队名为 default,编号为 0
        agent.sendCommand(new CmdInit("player", 0, "default"));
        // 进入主循环,持续处理服务器消息
        agent.run();
    } else {
        std::cerr << "Failed to connect to the server." << std::endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值