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

📄 reflect.c

📁 机器人足球,是用c语言实现的。大概有两千行
💻 C
字号:
/*计算球反弹后的新方向角和新速度*/

int WhetherReflect(Football *ball,Robot *dubu,FootballPitch *pitch,int i);
int MeetBoundary(Football *ball,FootballPitch *pitch);
int MeetRobot(Football *ball,Robot *dubu);

/*单人足球的球的反弹角的计算*/
int WhetherReflect(Football *ball,Robot *dubu,FootballPitch *pitch,int i)
{
    float x1,y1,x2,y2,x3,y3,x4,y4;
    int x;

    /* when the ball meets the boundary */
    if(fabs(ball->ball_x - pitch->position[0]) <= (ball->radius + 2))
    {
        if(ball->ball_y >= 250 || ball->ball_y <= 200)
            ball->direction_angle = ReflectAngle(ball->direction_angle,pitch->position[0],pitch->position[1],pitch->position[0],pitch->position[3]);
    }
    else if(fabs(ball->ball_x - pitch->position[2]) <= (ball->radius + 2))
    {
        if(ball->ball_y >= 250 || ball->ball_y <= 200)
            ball->direction_angle = ReflectAngle(ball->direction_angle,pitch->position[2],pitch->position[1],pitch->position[2],pitch->position[3]);
    }
    else if(fabs(ball->ball_y - pitch->position[1]) <= (ball->radius + 2))
        ball->direction_angle = ReflectAngle(ball->direction_angle,pitch->position[0],pitch->position[1],pitch->position[2],pitch->position[1]);
    else if(fabs(ball->ball_y - pitch->position[3]) <= (ball->radius + 2))
        ball->direction_angle = ReflectAngle(ball->direction_angle,pitch->position[0],pitch->position[3],pitch->position[2],pitch->position[3]);

    /* when the ball meets the angle */
    else if(fabs(Distance(ball->ball_x,ball->ball_y,90,60,70,80) - ball->radius) <= 2)
        ball->direction_angle = ReflectAngle(ball->direction_angle,90,60,70,80);
    else if(fabs(Distance(ball->ball_x,ball->ball_y,550,60,570,80) - ball->radius) <= 2)
        ball->direction_angle = ReflectAngle(ball->direction_angle,550,60,570,80);
    else if(fabs(Distance(ball->ball_x,ball->ball_y,550,390,570,370) - ball->radius) <= 2)
        ball->direction_angle = ReflectAngle(ball->direction_angle,550,390,570,370);
    else if(fabs(Distance(ball->ball_x,ball->ball_y,70,370,90,390) - ball->radius) <= 2)
        ball->direction_angle = ReflectAngle(ball->direction_angle,70,370,90,390);

    /* when the ball meets the robot */
    x1 = dubu->position_x + dubu->reverse_radius * cos(PI / 4 + dubu->direction_indicator);
    y1 = dubu->position_y - dubu->reverse_radius * sin(PI / 4 + dubu->direction_indicator);
    x2 = dubu->position_x + dubu->reverse_radius * cos(3 * PI / 4 + dubu->direction_indicator);
    y2 = dubu->position_y - dubu->reverse_radius * sin(3 * PI / 4 + dubu->direction_indicator);
    x3 = dubu->position_x + dubu->reverse_radius * cos(5 * PI / 4 + dubu->direction_indicator);
    y3 = dubu->position_y - dubu->reverse_radius * sin(5 * PI / 4 + dubu->direction_indicator);
    x4 = dubu->position_x + dubu->reverse_radius * cos(7 * PI / 4 + dubu->direction_indicator);
    y4 = dubu->position_y - dubu->reverse_radius * sin(7 * PI / 4 + dubu->direction_indicator);
    if(fabs(Distance(ball->ball_x,ball->ball_y,x1,y1,x2,y2) - ball->radius) <= 1)
    {
        x = WhetherInSide(ball,dubu);
        if(x==1)
        {
            ball->direction_angle = ReflectAngle(ball->direction_angle,x1,y1,x2,y2);
        }
    }
    if(fabs(Distance(ball->ball_x,ball->ball_y,x2,y2,x3,y3) - ball->radius) <= 3)
    {
        x = WhetherInView(ball,dubu);
        if(x==0)
        {
            ComputeShootAngle(ball,dubu);
            x = WhetherReverse(dubu);
            if(x)
                ReverseToGoal(ball,dubu);
            if(ball->init_speed < 4)   /* prevent speed is too big so that the ball beyond the boundary */
            {
                ball->init_speed = SynthesisSpeed(ball->init_speed,4,ball->direction_angle,dubu->direction_indicator);
                ball->direction_angle = SynthesisAngle(ball->init_speed,4,ball->direction_angle,dubu->direction_indicator);
            }
            else
            {
                ball->init_speed = SynthesisSpeed(ball->init_speed,0.1,ball->direction_angle,dubu->direction_indicator);
                ball->direction_angle = SynthesisAngle(ball->init_speed,0.1,ball->direction_angle,dubu->direction_indicator);
            }
        }
    }
    if(fabs(Distance(ball->ball_x,ball->ball_y,x3,y3,x4,y4) - ball->radius) <= 1)
    {
        x = WhetherInSide(ball,dubu);
        if(x==1)
        {
            ball->direction_angle = ReflectAngle(ball->direction_angle,x3,y3,x4,y4);
        }
    }
    if(fabs(Distance(ball->ball_x,ball->ball_y,x4,y4,x1,y1) - ball->radius) <= 3)
    {
        x = WhetherInView(ball,dubu);
        if(x==0)
        {
            ComputeShootAngle(ball,dubu);
            x = WhetherReverse(dubu);
            if(x)
                ReverseToGoal(ball,dubu);
            if(ball->init_speed < 4)   /* prevent speed is too big so that the ball beyond the boundary */
            {
                ball->init_speed = SynthesisSpeed(ball->init_speed,4,ball->direction_angle,dubu->direction_indicator);
                ball->direction_angle = SynthesisAngle(ball->init_speed,4,ball->direction_angle,dubu->direction_indicator);
            }
            else
            {
                ball->init_speed = SynthesisSpeed(ball->init_speed,0.1,ball->direction_angle,dubu->direction_indicator);
                ball->direction_angle = SynthesisAngle(ball->init_speed,0.1,ball->direction_angle,dubu->direction_indicator);
            }
        }
    }

    return 0;
}

/*以下是二人足球中球的反弹角的计算*/
int MeetBoundary(Football *ball,FootballPitch *pitch)
{
    /* when the ball meets the boundary */
    if(fabs(ball->ball_x - pitch->position[0]) <= (ball->radius + 2))
    {
        if(ball->ball_y >= 250 || ball->ball_y <= 200)
            ball->direction_angle = ReflectAngle(ball->direction_angle,pitch->position[0],pitch->position[1],pitch->position[0],pitch->position[3]);
    }
    else if(fabs(ball->ball_x - pitch->position[2]) <= (ball->radius + 2))
    {
        if(ball->ball_y >= 250 || ball->ball_y <= 200)
            ball->direction_angle = ReflectAngle(ball->direction_angle,pitch->position[2],pitch->position[1],pitch->position[2],pitch->position[3]);
    }
    else if(fabs(ball->ball_y - pitch->position[1]) <= (ball->radius + 2))
        ball->direction_angle = ReflectAngle(ball->direction_angle,pitch->position[0],pitch->position[1],pitch->position[2],pitch->position[1]);
    else if(fabs(ball->ball_y - pitch->position[3]) <= (ball->radius + 2))
        ball->direction_angle = ReflectAngle(ball->direction_angle,pitch->position[0],pitch->position[3],pitch->position[2],pitch->position[3]);

    /* when the ball meets the angle */
    else if(fabs(Distance(ball->ball_x,ball->ball_y,90,60,70,80) - ball->radius) <= 2)
        ball->direction_angle = ReflectAngle(ball->direction_angle,90,60,70,80);
    else if(fabs(Distance(ball->ball_x,ball->ball_y,550,60,570,80) - ball->radius) <= 2)
        ball->direction_angle = ReflectAngle(ball->direction_angle,550,60,570,80);
    else if(fabs(Distance(ball->ball_x,ball->ball_y,550,390,570,370) - ball->radius) <= 2)
        ball->direction_angle = ReflectAngle(ball->direction_angle,550,390,570,370);
    else if(fabs(Distance(ball->ball_x,ball->ball_y,70,370,90,390) - ball->radius) <= 2)
        ball->direction_angle = ReflectAngle(ball->direction_angle,70,370,90,390);

    return 0;
}

int MeetRobot(Football *ball,Robot *dubu)
{
    float x1,x2,x3,x4,y1,y2,y3,y4;
    int x;

    /* when the ball meets the robot */
    x1 = dubu->position_x + dubu->reverse_radius * cos(PI / 4 + dubu->direction_indicator);
    y1 = dubu->position_y - dubu->reverse_radius * sin(PI / 4 + dubu->direction_indicator);
    x2 = dubu->position_x + dubu->reverse_radius * cos(3 * PI / 4 + dubu->direction_indicator);
    y2 = dubu->position_y - dubu->reverse_radius * sin(3 * PI / 4 + dubu->direction_indicator);
    x3 = dubu->position_x + dubu->reverse_radius * cos(5 * PI / 4 + dubu->direction_indicator);
    y3 = dubu->position_y - dubu->reverse_radius * sin(5 * PI / 4 + dubu->direction_indicator);
    x4 = dubu->position_x + dubu->reverse_radius * cos(7 * PI / 4 + dubu->direction_indicator);
    y4 = dubu->position_y - dubu->reverse_radius * sin(7 * PI / 4 + dubu->direction_indicator);
    if(fabs(Distance(ball->ball_x,ball->ball_y,x1,y1,x2,y2) - ball->radius) <= 3)
    {
        x = WhetherInSide(ball,dubu);
        if(x==1)
        {
            ball->direction_angle = ReflectAngle(ball->direction_angle,x1,y1,x2,y2);
        }
    }
    if(fabs(Distance(ball->ball_x,ball->ball_y,x2,y2,x3,y3) - ball->radius) <= 5)
    {
        x = WhetherInView(ball,dubu);
        if(x==0)
        {
            ComputeShootAngle(ball,dubu);
            x = WhetherReverse(dubu);
            if(x)
                ReverseToGoal(ball,dubu);
            if(ball->init_speed < 4)   /* prevent speed is too big so that the ball beyond the boundary */
            {
                ball->init_speed = SynthesisSpeed(ball->init_speed,4,ball->direction_angle,dubu->direction_indicator);
                ball->direction_angle = SynthesisAngle(ball->init_speed,4,ball->direction_angle,dubu->direction_indicator);
            }
            else
            {
                ball->init_speed = SynthesisSpeed(ball->init_speed,0.1,ball->direction_angle,dubu->direction_indicator);
                ball->direction_angle = SynthesisAngle(ball->init_speed,0.1,ball->direction_angle,dubu->direction_indicator);
            }
        }
    }
    if(fabs(Distance(ball->ball_x,ball->ball_y,x3,y3,x4,y4) - ball->radius) <= 3)
    {
        x = WhetherInSide(ball,dubu);
        if(x==1)
        {
            ball->direction_angle = ReflectAngle(ball->direction_angle,x3,y3,x4,y4);
        }
    }
    if(fabs(Distance(ball->ball_x,ball->ball_y,x4,y4,x1,y1) - ball->radius) <= 5)
    {
        x = WhetherInView(ball,dubu);
        if(x==0)
        {
            ComputeShootAngle(ball,dubu);
            x = WhetherReverse(dubu);
            if(x)
                ReverseToGoal(ball,dubu);
            if(ball->init_speed < 4)   /* prevent speed is too big so that the ball beyond the boundary */
            {
                ball->init_speed = SynthesisSpeed(ball->init_speed,4,ball->direction_angle,dubu->direction_indicator);
                ball->direction_angle = SynthesisAngle(ball->init_speed,4,ball->direction_angle,dubu->direction_indicator);
            }
            else
            {
                ball->init_speed = SynthesisSpeed(ball->init_speed,0.1,ball->direction_angle,dubu->direction_indicator);
                ball->direction_angle = SynthesisAngle(ball->init_speed,0.1,ball->direction_angle,dubu->direction_indicator);
            }
        }
    }

    return 0;
}

⌨️ 快捷键说明

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