void make_floor(std::unique_ptr<PhysicsWorld>& world)
{
    btTransform trans;
    trans.setIdentity();
    trans.setOrigin(btVector3(0.0, -5.0, 0.0));
    btScalar mass(0.0);
    btVector3 local_inertia(0, 0, 0);

    btStaticPlaneShape* shape = new btStaticPlaneShape(btVector3(0.0f, 1.0f, 0.0f), 0.0f);
    world->get_collision_shapes().push_back(shape);
    btDefaultMotionState* motion_state = new btDefaultMotionState(trans);

    shape->calculateLocalInertia(mass, local_inertia);

    btRigidBody::btRigidBodyConstructionInfo body_info(mass, motion_state, shape, local_inertia);
    btRigidBody* body = new btRigidBody(body_info);
    body->setRestitution(0);

    world->get_dynamics_world()->addRigidBody(body);
}
Beispiel #2
0
int main(int   argc,    char *argv[]) {
    init_list_body_data();


    int  nbodies, x, y;
    Window win; // initialization for a window
    GC gc; // graphics context
    Display *display = NULL;
    unsigned int width = X_RESN, height = Y_RESN; /* window size */
    clock_t start, end, elapsed;



    int rank;
    int size;

    MPI_Init (&argc, &argv);      /* starts MPI */
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);        /* get current process id */
    MPI_Comm_size(MPI_COMM_WORLD, &size);        /* get number of processes */



    // I follow this topic
    // http://stackoverflow.com/questions/9864510/struct-serialization-in-c-and-transfer-over-mpi
    // alow MPI tranfer truct data type
    const int       num_of_item = 8;
    int             blocklengths[8] = {1, 1, 1, 1, 1, 1, 1, 1};
    MPI_Datatype    types[8] = {MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_DOUBLE, MPI_INT};
    MPI_Datatype    mpi_body_type;
    MPI_Aint        offsets[8];

    offsets[0] = offsetof(body, position_x);
    offsets[1] = offsetof(body, position_y);
    offsets[2] = offsetof(body, velocity_x);
    offsets[3] = offsetof(body, velocity_y);
    offsets[4] = offsetof(body, force_x);
    offsets[5] = offsetof(body, force_y);
    offsets[6] = offsetof(body, mass);
    offsets[7] = offsetof(body, body_id);

    MPI_Type_create_struct(num_of_item, blocklengths, offsets, types, &mpi_body_type);
    MPI_Type_commit(&mpi_body_type);





    //
    // http://stackoverflow.com/questions/11246150/synchronizing-master-slave-model-with-mpi
    // for master/slave
    int j = 0;
    int i = 0;
    int k = 0;
    int num_of_move =100;
    int index = 0;
    int p;


    int steps = MAX_BODY / (size - 1);

    if (( MAX_BODY % (size - 1)) > 0) {
        steps++;
    }
    if(rank == 0)
    {
        display = x11setup(&win, &gc, width, height);
        // other setup code in the master
    }
    for (j = 0; j < num_of_move; j++) {
        //which number of moves  process complte!?
        if(rank == 0) {
            printf("Processed %d of %d\n", j, num_of_move);
        }
        for (k = 0; k < MAX_BODY; k ++) {

            index = 0;
            for (i = 0; i <  steps; i++ ) {

                if(rank == 0) { //master
                    for(p = 1; p < size; p++){

                        body a =  list_body[k];
                        body b =  list_body[index];
                        if (index >= MAX_BODY -1) {
                            b = list_body[MAX_BODY -1];
                        }
                        // send 2 body to calculate force
                        MPI_Send(&a, 1, mpi_body_type, p, 20, MPI_COMM_WORLD);
                        MPI_Send(&b, 1, mpi_body_type, p, 20, MPI_COMM_WORLD);

                        //recieve result
                        body b_recv;
                        MPI_Recv(&b_recv, 1, mpi_body_type, p, 21, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

                        //update to list
                        list_body[k] = b_recv;
                        index++;
                    }

                }
                else { //workers

                    //receive 2 body to calculate force
                    body  a;
                    MPI_Recv(&a, 1, mpi_body_type, 0, 20, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
                    body  b;
                    MPI_Recv(&b, 1, mpi_body_type, 0, 20, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

                    // calculating....
                    update_body_force(&a, &b);
                    update_body_velocity(&a);
                    update_body_location(&a);

                    // finish! send body a to master
                    MPI_Send(&a, 1, mpi_body_type, 0, 21, MPI_COMM_WORLD);
                }
            }
        }
        if(rank == 0) {
            XClearWindow(display, win);
            for (i = 0; i < MAX_BODY; i++) { // draw the bodies on the display
                body b = list_body[i];
                // this function will draw a circle inside a 3x3 box with the upper left
                // corner at (x,y). N.b. the last 2 arguments mean that it will fill from
                // 0 to 360 degrees - a full circle
                if (i == 0) {
                    XFillArc(display, win, gc, b.position_x , b.position_y, 6, 6, 0, 23040);
                }
                XFillArc(display, win, gc, b.position_x , b.position_y, 3, 3, 0, 23040);

                // you campicould also use XDrawPoint(display, win, gc, x, y) to draw a single
                // pixel at (x,y)
            }
            XFlush(display);
            //reset force
            for (i = 0; i < MAX_BODY; i++) {
                    body *b = & list_body[i];

                    b->force_x = 0;
                    b->force_y = 0;
            }

        }


        usleep(100 * 1000);

    }
    // finaly, use rank=0 (master) to output result
    if (rank == 0) {
        i = 0;
        for (i = 0; i < MAX_BODY; i++) {
            body_info(list_body[i]);
        }
    }



    // main loop
//    int running = 1; // loop variable
//    start = clock();
//    while(running) {

//        // checks to see if there have been any events,
//        // will exit the main loop if any key is pressed
//        if(rank==0) {
//            if(XPending(display)) {
//                XEvent ev;
//                XNextEvent(display, &ev);
//                switch(ev.type) {
//                    case KeyPress:
//                        running = 0;
//                        break;
//                }
//            }
//        }


        // your code to calculate the forces on the bodies goes here


//        end = clock();
//        elapsed = end - start;
//        // only update the display if > 1 millisecond has passed since the last update
//        if(elapsed / (CLOCKS_PER_SEC/1000) > 1 && rank==0) {
//            XClearWindow(display, win);
//            for (i = 0; i < MAX_BODY; i++) { // draw the bodies on the display
//                body b = list_body[i];
//                // this function will draw a circle inside a 3x3 box with the upper left
//                // corner at (x,y). N.b. the last 2 arguments mean that it will fill from
//                // 0 to 360 degrees - a full circle
//                XFillArc(display, win, gc, b.position_x , b.position_y, 3, 3, 0, 23040);

//                // you campicould also use XDrawPoint(display, win, gc, x, y) to draw a single
//                // pixel at (x,y)
//            }
//            start = end;
//            XFlush(display);
//        }
//    }

//    if(rank==0 && display) {
//        XCloseDisplay(display); // close the display window
//    }


    MPI_Finalize();


    return EXIT_SUCCESS;
}
Beispiel #3
0
int Handle_Packet (char *buffer, int bytes_recieved)
{
    IP_Header     *ip;
    int           from_mask[4], to_mask[4];
    unsigned int  body_offset, body_size;    
       

     #ifdef DEBUG
     #ifdef UNIX

       addr.s_addr = IP_FROM(buffer);
       fprintf (stdout, "\n%s", inet_ntoa(addr));
       if (test_address (from_mask, flags.from_ip_mask) == 0)
       { fprintf (stdout, " (Src OK)"); }
       else
       { fprintf (stdout, " (Src WRONG)"); }

       addr.s_addr = IP_TO(buffer);
       fprintf (stdout, "\n%s", inet_ntoa(addr));
       if (test_address (to_mask, flags.to_ip_mask) == 0)
       { fprintf (stdout, " (Dest OK)"); }
       else
       { fprintf (stdout, " (Dest WRONG)"); }

       fprintf (stdout, "\nProtocol: %d", PROTOCOL(buffer));

     #endif
     #endif

     /********************************************************/
     /*               Dumping ethernet headers ?             */
     /********************************************************/
 
     if (flags.eth_hd == YES)
     { dump_eth_header(ETH_HEADER(buffer), flags.desc); }

     if (ETH_FORMAT(buffer) != ETHERNET_II) { return HANDLER_OK; }

     /********************************************************/
     /*                   Dump *IP* stuff *ONLY*             */
     /********************************************************/

     if (ETH_TYPE(buffer) == IP_ETH)
     {

        /********************************************************/
        /* Set IP masks                                         */
        /********************************************************/

        get_addr (IP_FROM(buffer), from_mask);
        get_addr (IP_TO(buffer), to_mask);

        /*******************************************************/
        /* Checking IP address with mask                       */
        /* Checking port numbers                               */
        /* Checking protocol                                   */
        /* ...                                                 */
        /*******************************************************/

        if
        (
          (test_address (from_mask, flags.from_ip_mask) == 0)
          &&
          (test_address (to_mask, flags.to_ip_mask) == 0)
          && (
               (flags.from_port_num == ALL_PORT)
               ||
               (get_src_port_number(buffer) == flags.from_port_num)
             )
          && (
               (flags.to_port_num == ALL_PORT)
               ||
               (get_dst_port_number(buffer) == flags.to_port_num)
             )
          && (
               (
                 (PROTOCOL(buffer) == UDP_PACKET)
                 &&
                 (flags.proto_udp == YES)
               )
               ||              
               (
                 (PROTOCOL(buffer) == TCP_PACKET)
                 &&
                 (flags.proto_tcp == YES)
               )
               ||              
               (
                 (PROTOCOL(buffer) == IGMP_PACKET)
                 &&
                 (flags.proto_igmp == YES)
               )
             ) /* end test on protocol */
        ) /* end if condition statement */
        {
          /******************************************************************/
          /* Sanity check                                                   */
          /******************************************************************/

          ip = IP_HEADER(buffer);
          if (
                ((ntohs(ip->ip_total_length)+ETH_HD_LEN) > bytes_recieved)
                &&
               (flags.verbose == YES)
             )
          {
             fprintf (stdout, "\nWARNING: received %d bytes", bytes_recieved);
             fprintf (stdout, "\n         total packet size ");
             fprintf (stdout, "%d bytes", ntohs(ip->ip_total_length)+ETH_HD_LEN);
             fprintf (stdout, "\n\nThis may be a network error ... skipping");
             fprintf (stdout, " the packet.\n");
             fflush (stdout);
             return HANDLER_OK;
          }

          /******************************************************************/
          /* Dump the IP header if requested                                */
          /******************************************************************/

          if (flags.ip_hd == YES) { dump_ip_header(ip, flags.desc); }

          /******************************************************************/
          /* Dump the following headers if requested:                       */
          /*    - UDP                                                       */
          /*    - TCP                                                       */
          /*                                                                */
          /* In the case of UDP, it may be a RIP message.                   */
          /******************************************************************/

          if (PROTOCOL(buffer) == UDP_PACKET)
          {
             if (flags.udp_hd == YES)
             { dump_udp_header(UDP_HEADER(buffer), flags.desc); }
             
             switch (UDP_SRC_PORT(buffer))
             {
               case SER_RIP:
                    if (RIP_VERSION(buffer) == 1)
                    { dump_rip1_header (buffer, flags.desc); }
                    else
                    { fprintf (stdout, "\nRIP version 2 !"); };
                    break;
               
               default:;
             }
          }

          if ((PROTOCOL(buffer) == TCP_PACKET) && (flags.tcp_hd == YES))
          { dump_tcp_header(TCP_HEADER(buffer), flags.desc); }

          /********************************************************************/
          /* Dump the packet bopy for the following protocols if requested:   */
          /*    - TCP                                                         */
          /*    - UDP                                                         */
          /*    - IGMP                                                        */
          /*                                                                  */
          /* Note that for IGMP the IGMP header is not separated from the bo- */
          /* -dy.                                                             */
          /********************************************************************/

          if (flags.body_level != BD_NONE)
          {
            /* Get offset and size of the body */
            body_info (buffer, &body_offset, &body_size);

           switch (flags.body_level)
           {
              case BD_HEXA   : dump_hexa (buffer+body_offset, body_size);
                               break;
              case BD_ASCII  : dump_ascii (buffer+body_offset, body_size);
                               break;
              case BD_MIX    : dump (buffer+body_offset, body_size); break;
              default : {
                          if (flags.verbose == YES)
                          { fprintf (stderr, "\nUnknown dump_body option\n"); }
                          else
                          { fprintf (stderr, "\nInternal error\n"); }
                          return HANDLER_ERR;
                        }
            } /* end switch (dump_body) */
          } /* end if for UDP/TCP/dump_body */
        } 

        return HANDLER_OK;       
     } /* end if for *IP* stuff *ONLY* */

     /*****************************************************/
     /* So it is not an IP stuff                          */
     /*****************************************************/

     /******************************************************/
     /*  May be it is ARP ?                                */
     /******************************************************/

     if ((ETH_TYPE(buffer) == ARP_ETH) && (flags.proto_arp == YES))
     {
        dump_ARP_header (ARP_HEADER(buffer), flags.desc);
     }


     return HANDLER_OK;       

}