Exemple #1
0
void updateBall() {
    // fly a bit
    ball_pos_x += ball_dir_x * ball_speed;
    ball_pos_y += ball_dir_y * ball_speed;

	    // hit by left racket?
    if (ball_pos_x < racket_left_x + racket_width && 
        ball_pos_x > racket_left_x &&
        ball_pos_y < racket_left_y + racket_height &&
        ball_pos_y > racket_left_y) {
        // set fly direction depending on where it hit the racket
        // (t is 0.5 if hit at top, 0 at center, -0.5 at bottom)
        float t = ((ball_pos_y - racket_left_y) / racket_height) - 0.5f;
        ball_dir_x = fabs(ball_dir_x); // force it to be positive
        ball_dir_y = t;
    }
    
    // hit by right racket?
    if (ball_pos_x > racket_right_x && 
        ball_pos_x < racket_right_x + racket_width &&
        ball_pos_y < racket_right_y + racket_height &&
        ball_pos_y > racket_right_y) {
        // set fly direction depending on where it hit the racket
        // (t is 0.5 if hit at top, 0 at center, -0.5 at bottom)
        float t = ((ball_pos_y - racket_right_y) / racket_height) - 0.5f;
        ball_dir_x = -fabs(ball_dir_x); // force it to be negative
        ball_dir_y = t;
    }

    // hit left wall?
    if (ball_pos_x < 0) {
        ++score_right;
        ball_pos_x = width / 2;
        ball_pos_y = height / 2;
        ball_dir_x = fabs(ball_dir_x); // force it to be positive
        ball_dir_y = 0;
    }

    // hit right wall?
    if (ball_pos_x > width) {
        ++score_left;
        ball_pos_x = width / 2;
        ball_pos_y = height / 2;
        ball_dir_x = -fabs(ball_dir_x); // force it to be negative
        ball_dir_y = 0;
    }

    // hit top wall?
    if (ball_pos_y > height) {
        ball_dir_y = -fabs(ball_dir_y); // force it to be negative
    }

    // hit bottom wall?
    if (ball_pos_y < 0) {
        ball_dir_y = fabs(ball_dir_y); // force it to be positive
    }

    // make sure that length of dir stays at 1
    vec2_norm(ball_dir_x, ball_dir_y);
}
Exemple #2
0
int64_t heading_to_point(int64_t current_x, int64_t current_y, int64_t x, int64_t y)
{
    double plane_pos[3], waypoint_pos[3], dir[3];
    double heading;

    plane_pos[0] = static_cast<double>(current_x) ;
    plane_pos[1] = static_cast<double>(current_y) ;
    plane_pos[2] = 0;

    waypoint_pos[0] = static_cast<double>(x) ;
    waypoint_pos[1] = static_cast<double>(y) ;
    waypoint_pos[2] = 0;

    vector_from_to_position(plane_pos, waypoint_pos, dir);
    vec2_norm(dir, dir, FALSE);
    hrl_xydof_to_heading(dir, &heading);

    return convert(bracket_rad_to_deg(heading));
}
Exemple #3
0
void Boat_m_touch(Ent *ent1, Ent *ent2)
{
    Boat *boat = (Boat*)ent1;
    if(Ent_GET(flags, ent2) & EFLAGS_SOLID) {
        if(ent_class_is_subclass(ent2->eclass, &Enemy_CLASS)) {
            set_paused(1);
            set_game_over(1);
        } else {
            float dir_angle = vec2_to_angle(Ent_GET(move_direction, boat)),
                  coll_angle;
            vec2 boat_pos = *Ent_GET(position, boat),
                 coll_pos = *Ent_GET(position, ent2);
            vec2 coll_vec = coll_pos;
        
            vec2_sub(&coll_vec, &boat_pos);
            vec2_norm(&coll_vec);

            coll_angle = vec2_to_angle(&coll_vec);

            if(fabs(dir_angle - coll_angle) <= 45)
                Ent_SET(speed, boat, 0);
        }
    }
}
Exemple #4
0
static inline void gen_infinity(CPoint* out, float width, float angle, int segment_count)
{

    CPoint path[13];
    path[0]=CPointMake(53,23);

    path[1]=CPointMake(49,31);
    path[2]=CPointMake(39,47);

    path[3]=CPointMake(22,47);

    path[4]=CPointMake(6,47);
    path[5]=CPointMake(0,31);

    path[6]=CPointMake(0,23);

    path[7]=CPointMake(0,16);
    path[8]=CPointMake(5,0);

    path[9]=CPointMake(23,0);

    path[10]=CPointMake(39,0);
    path[11]=CPointMake(48,15);

    path[12]=CPointMake(52,21);



    int offset=0;

    int seg;
    for (seg=0; seg<=segment_count; seg++) {
        float tt = ((float)seg/(float)segment_count)*angle;

        int q=4;
        float tstep=1./q;
        int n = floor(tt/tstep);

        if (seg >= segment_count) {
            //n=n-1;//q-1;
        }
        //printf("n>%d\n", n);

        CPoint a = path[0+3*n];;
        CPoint p1 = path[1+3*n];
        CPoint p2 = path[2+3*n];
        CPoint b = path[3+3*n];

        float t=(tt-tstep*n)*q;
        float nt = 1.0f - t;


        vec2 p = {a.x * nt * nt * nt  +  3.0 * p1.x * nt * nt * t  +  3.0 * p2.x * nt * t * t  +  b.x * t * t * t,
                  a.y * nt * nt * nt  +  3.0 * p1.y * nt * nt * t  +  3.0 * p2.y * nt * t * t  +  b.y * t * t * t
                 };

        vec2 tangent = {-3.0 * a.x * nt * nt  +  3.0 * p1.x * (1.0 - 4.0 * t + 3.0 * t * t)  +  3.0 * p2.x * (2.0 * t - 3.0 * t * t)  +  3.0 * b.x * t * t,
                        -3.0 * a.y * nt * nt  +  3.0 * p1.y * (1.0 - 4.0 * t + 3.0 * t * t)  +  3.0 * p2.y * (2.0 * t - 3.0 * t * t)  +  3.0 * b.y * t * t
                       };

        vec2 tan_norm = {-tangent[1], tangent[0]};
        vec2 norm;
        vec2_norm(norm, tan_norm);


        vec2 v;
        vec2 norm_scaled;
        vec2_scale(norm_scaled, norm, +width/2.);
        vec2_add(v, p, norm_scaled);

        out[offset] = CPointMake(v[0], v[1]);
        offset++;

        vec2_scale(norm_scaled, norm, -width/2.);
        vec2_add(v, p, norm_scaled);

        out[offset] = CPointMake(v[0], v[1]);
        offset++;
    }
    //printf("infinity_q>%d", offset);
}
/*
 *      A1-------------------J---I
 *                               | 
 *      A--------------------B   K 
 *                           |   |
 *      A2---------------L   |   |
 *                       |   |   |
 *                       |   |   |
 *                       |   |   |
 *                      C2   C   C1
 */
void
vertex_buffer_add_polyline( vertex_buffer_t * self,
                            vec2 *points, size_t n_points,
                            vec4 color, double thickness,
                            int join, int cap )
{
    typedef struct { float x,y,z,r,g,b,a,s,t,u; } vertex_t;
    assert( self );
    assert( n_points > 2 );
    assert( strcmp( vertex_buffer_format( self ), "v3f:c4f:t3f" ) == 0 );

    float d,w;
    if (thickness < 1.0)
    {
        w = 2.0;
        color.a *= thickness;
        d = (w+2)/w;
    }
    else
    {
        d = (thickness+2.0)/thickness;
        w = thickness+2.0;
    }
    float r = color.r;
    float g = color.g;
    float b = color.b;
    float a = color.a;
    float t = thickness;

    size_t n_vertices = 0;
    size_t n_indices = 0;
    if( (join == bevel_join) )
    {
        n_vertices = 2 + (n_points-2) * 6;
        n_indices =  2*3  + (n_points-2) * 3 * 3;
    }
    else if( join == miter_join )
    {
        n_vertices = 2 + (n_points-2) * 6;
        n_indices =  2*3  + (n_points-2) * 4 * 3;
    }
    else
    {
        n_vertices = 2 + (n_points-2) * 10;
        n_indices =  2*3  + (n_points-2) * 6 * 3;
    }
    vertex_t * vertices = (vertex_t *) calloc( n_vertices, sizeof(vertex_t) );
    GLuint * indices = (GLuint *) calloc( n_indices, sizeof(GLuint) );

    vec2 A,B,C;
    vec2 A1,A2,C1,C2;
    vec2 T_ba,O_ab, T_bc, O_cb;
    vec2 I,J,K,L,T;
    size_t i;
    size_t v_index = 0;
    size_t v_count = 0;
    size_t i_index = 0;
    size_t i_count = 0;
    for( i=0; i< (n_points-2); ++i )
    {
        v_count = 0;
        i_count = 0;

        A = points[i+0];
        B = points[i+1];
        C = points[i+2];
        T_ba = (vec2) {{ B.x-A.x, B.y-A.y }};
        T_bc = (vec2) {{ B.x-C.x, B.y-C.y }};
        O_ab = vec2_ortho( A, B );
        O_cb = vec2_ortho( C, B );
        C1 = (vec2) {{ C.x - w/2*O_cb.x, C.y - w/2*O_cb.y }};
        C2 = (vec2) {{ C.x + w/2*O_cb.x, C.y + w/2*O_cb.y }};
        if( i == 0)
        {
            A1 = (vec2) {{ A.x + w/2*O_ab.x, A.y + w/2*O_ab.y }};
            A2 = (vec2) {{ A.x - w/2*O_ab.x, A.y - w/2*O_ab.y }};
        }
        intersection( A1, (vec2) {{ A1.x + T_ba.x, A1.y + T_ba.y }},
                      C1, (vec2) {{ C1.x + T_bc.x, C1.y + T_bc.y }}, &I);
        int cross = intersection( A2, (vec2) {{ A2.x + T_ba.x, A2.y + T_ba.y }},
                              C2, (vec2) {{ C2.x + T_bc.x, C2.y + T_bc.y }}, &L);
        if( cross == 2)
        {
            J = (vec2) {{ B.x + w/2*O_ab.x, B.y + w/2*O_ab.y }};
            K = (vec2) {{ B.x - w/2*O_cb.x, B.y - w/2*O_cb.y }};        
        }
        else
        {
            T = I; I = L; L = T;
            T = A1; A1 = A2; A2 = T;
            T = C1; C1 = C2; C2 = T;
            J = (vec2) {{ B.x - w/2*O_ab.x, B.y - w/2*O_ab.y }};
            K = (vec2) {{ B.x + w/2*O_cb.x, B.y + w/2*O_cb.y }};        
        }
        vertices[v_index+0] = (vertex_t) { A1.x,A1.y,0, r,g,b,a, 0,+d,t }; // A1
        vertices[v_index+1] = (vertex_t) { A2.x,A2.y,0, r,g,b,a, 0,-d,t }; // A2
        vertices[v_index+2] = (vertex_t) {  I.x, I.y,0, r,g,b,a, 0,+d,t }; // I
        vertices[v_index+3] = (vertex_t) {  J.x, J.y,0, r,g,b,a, 0,+d,t }; // J
        vertices[v_index+4] = (vertex_t) {  K.x, K.y,0, r,g,b,a, 0,+d,t }; // K
        vertices[v_index+5] = (vertex_t) {  L.x, L.y,0, r,g,b,a, 0,-d,t }; // L
        v_count += 6;

        indices[i_index+0] = v_index+0; // A1
        indices[i_index+1] = v_index+1; // A2
        indices[i_index+2] = v_index+5; // L
        i_index += 3; i_count += 3;

        indices[i_index+0] = v_index+0; // A1
        indices[i_index+1] = v_index+3; // J
        indices[i_index+2] = v_index+5; // L
        i_index += 3; i_count += 3;

        /* Bevel joints */
        if( join == bevel_join )
        {
            indices[i_index+0] = v_index+3; // J
            indices[i_index+1] = v_index+4; // K
            indices[i_index+2] = v_index+5; // L
            i_index += 3; i_count += 3;
        }

        /* Miter joints */
        else if( join == miter_join )
        {
            indices[i_index+0] = v_index+2; // I
            indices[i_index+1] = v_index+3; // J
            indices[i_index+2] = v_index+5; // L
            i_index += 3; i_count += 3;

            indices[i_index+0] = v_index+2; // I
            indices[i_index+1] = v_index+4; // K
            indices[i_index+2] = v_index+5; // L
            i_index += 3; i_count += 3;
        }

        /* Round joints */
        else {
            float jk = vec2_norm( (vec2) {{ J.x-K.x, J.y-K.y }} );
            float jb = vec2_norm( (vec2) {{ J.x-B.x, J.y-B.y }} );
            float ib = vec2_norm( (vec2) {{ I.x-B.x, I.y-B.y }} );
            float jl = vec2_norm( (vec2) {{ J.x-L.x, J.y-L.y }} );
            float d1 = ib/jb;
            float d2 = 0.5*jk/jb;
            float d3 = sqrt(1-d2*d2);
            float c = w/jl;
            d1 *= d; d2 *= d; d3 *= d;

            vertices[v_index+6] = (vertex_t) {  I.x, I.y,0, r,g,b,a, -d1,  0,t };    // I'
            vertices[v_index+7] = (vertex_t) {  J.x, J.y,0, r,g,b,a, -d3,+d2,t };    // J'
            vertices[v_index+8] = (vertex_t) {  K.x, K.y,0, r,g,b,a, -d3,-d2,t };    // K'
            vertices[v_index+9] = (vertex_t) {  L.x, L.y,0, r,g,b,a, 2*c*d-d3,0,t }; // L'
            v_count += 4;

            indices[i_index+0] = v_index+6; // I'
            indices[i_index+1] = v_index+7; // J'
            indices[i_index+2] = v_index+8; // K'
            i_index += 3; i_count += 3;

            indices[i_index+0] = v_index+7; // J'
            indices[i_index+1] = v_index+8; // K'
            indices[i_index+2] = v_index+9; // L'
            i_index += 3; i_count += 3;
        }
  
      
        if( i == (n_points-3) )
        {
            vertices[v_index+v_count+0] = (vertex_t) {  C1.x, C1.y,0, r,g,b,a, 0,+d,t };    // C1
            vertices[v_index+v_count+1] = (vertex_t) {  C2.x, C2.y,0, r,g,b,a, 0,-d,t };    // C2
            v_count += 2;

            indices[i_index+0] = v_index+v_count-2+0; // C1
            indices[i_index+1] = v_index+v_count-2+1; // C2
            indices[i_index+2] = v_index+5;           // L
            i_index += 3; i_count += 3;

            indices[i_index+0] = v_index+v_count-2+0; // C1
            indices[i_index+1] = v_index+5;           // L
            indices[i_index+2] = v_index+4;           // K
            i_index += 3; i_count += 3;
        }
        v_index += v_count;
        if( cross == 2 )
        {
            A1 = K;
            A2 = L;
        }
        else
        {
            A1 = L;
            A2 = K;
        }
    }
    vertex_buffer_append( self, vertices, n_vertices, indices,  n_indices );
}