Beispiel #1
0
		void VehicleObject::init_vehicle_physics(const Vector3f& box_size, const PhysObjInfo& info,
											     const VehicleProperties& properties)
		{

			m_iright_index = 0;
			m_iup_index = 1;
			m_iforward_index = 2;

			set_vehicle_properties(properties);

			m_box_size = btVector3((btScalar)box_size.x, (btScalar)box_size.y, (btScalar)box_size.z);

			//Physics stuff
			btCollisionShape* pChassisShape = new btBoxShape(m_box_size);
			m_pListOfCollisionShapes.push_back(pChassisShape);

			btCompoundShape* pCompoundShape = new btCompoundShape();
			m_pListOfCollisionShapes.push_back(pCompoundShape);

			btTransform localTrans;
			localTrans.setIdentity();
			localTrans.setOrigin(btVector3(0, 1, 0));

			pCompoundShape->addChildShape(localTrans, pChassisShape);

			//Setup chassis
			btTransform chassisTrans;
			chassisTrans.setIdentity();
			chassisTrans.setOrigin(btVector3(0, 0, 0));

			btVector3 localInertia(0, 0, 0);
			pCompoundShape->calculateLocalInertia(info.mass, localInertia);
			btDefaultMotionState* vehicleMotionState = new btDefaultMotionState(chassisTrans);
			btRigidBody::btRigidBodyConstructionInfo cInfo(info.mass, vehicleMotionState, pCompoundShape, localInertia);

			if(m_rbody)
			{
				delete m_rbody;
				m_rbody = 0;
			}

			m_rbody = new btRigidBody(cInfo);
			m_rbody->setContactProcessingThreshold(1e18f);
			m_rbody->setUserPointer(&m_entity_id);

			//initialise other parts of the vehicle
			init_engine();
			init_vehicle();
			init_wheels();

		}
int main(int argc , char *argv[])
{
  // init vehicle
  init_vehicle();

  // connect to server
  if(connect_server() != 0) {
    // error
    puts("Closing.");
    return 0;
  }

  // connected
  puts("All peachy.");
  puts("Sending hi.");


  // lets go in big loop and process incoming data
  while(veh.flag){
    // Sort of hearbeat
    //send_hi();

    // recv is blocking call
    // data from server
    veh.cnt = recv(veh.socket_desc, veh.server_reply , SERVER_MSG_SIZE , 0);
    if (veh.cnt > 0 ) {
      // we received data
      // print to terminal that a message from server came in
      printf("RECEIVED: %d BYTES\n", veh.cnt);
      printf("MSG FROM SERVER: %s\n", veh.server_reply);

      if (check_for_init() == 1){
        // wasn't init
        // check for stop
        if (check_for_stop() == 1) {
          // wasn't stop
          // check for params
          if (check_for_params() == 1) {
            // wasn't params
            // check for quit
            if (check_for_quit() == 1) {
              // it wasn't quit -> bad command
              printf("Unknown command.\n");
            }
            else {
              //it was quit -> so quit
              veh.flag = 0;
            }
          }
        }
      }

      // send proper response
      send(veh.socket_desc, veh.message, strlen(veh.message), 0);
    }
    else {
      puts("Received no data.");
      // no data -> server probably ended the connection
      veh.flag = 0;
    }

    // erase server reply
    memset(veh.server_reply, 0, SERVER_MSG_SIZE);
    // erase message
    memset(veh.message, 0, SERVER_MSG_SIZE);
    // erase counter
    veh.cnt = 0;
    // sleep 500ms
    //nanosleep((const struct timespec[]){{0, NSLEEP_TIME}}, NULL);

  } // veh.flag

  puts("Closing program.");
  shutdown(veh.socket_desc, 2); // close socket
  if (veh.state != VEH_UNINIT){
    // dont call unless properly initialized
    stop_wings();
  }
  return 0;
}