Пример #1
0
// initialize initializes the general display design coordinator, creates the 
// primitive sets, textures, objects, lights, sounds, cameras, and text items
//
void Design::initialize() {

       // general display design
    //
   Reflectivity redish = Reflectivity(red);
   Reflectivity greenish = Reflectivity(green);
   Reflectivity bluish = Reflectivity(blue);
   Reflectivity whitish = Reflectivity(white);
   setProjection(0.9f, 1.0f, 1000.0f);
   setAmbientLight(1, 1, 1);
   // camera at a distance - in lhs coordinates
    // camera at a distance - in lhs coordinates
   iCamera* camera = CreateCamera();
   camera->translate(0, 190,-500);
   camera->setRadius(17.8f);
   
    lastUpdate = now;	

    hud = CreateHUD(0.72f, 0.01f, 0.27f, 0.99f, CreateTexture(HUD_IMAGE));
    // cameras ----------------------------------------------------------------

   velocitytxt_=CreateText(Rectf(0.05f,0.27f,0.95f,0.37f),hud,L"",TEXT_HEIGHT,TEXT_TYPEFACE,TEXT_LEFT);
   deltatxt_=CreateText(Rectf(0.05f,0.17f,0.95f,0.27f),hud,L"",TEXT_HEIGHT,TEXT_TYPEFACE,TEXT_LEFT);
   positiontxt_=CreateText(Rectf(0.05f,0.38f,0.95f,0.48f),hud,L"",TEXT_HEIGHT,TEXT_TYPEFACE,TEXT_LEFT);

   lasttextupdate=now;

   // game ----------------------------------------------------------------------
   setBackground(CreateTexture(L"farm.png"));
   catcher = CreatePhysicsBox(-40, -5, 0, 40, 5, 0, &bluish, 1, PHYS_Floating, true);
   iAPIWindow* win = getWindow();
   catcher->translate(0, -70, 0);

   truck = CreatePhysicsBox(-100, -2, 0, 100, 2, 0, &redish, 1, PHYS_Floating, true);
   truck->translate(300, -50, 0);

   Reflectivity yellowish = Reflectivity(yellow);

   iPhysics* fallingBox = CreatePhysicsBox(-10, -10, -10, 10, 10, 10, &yellowish, 1, PHYS_Falling, true);
   fallingBox->translate(-350, 350, 0);
   fallingBox->setVelocity(Vector(5, 20, 0));
   fallingBox->addBodyForce(Vector(0, -10, 0));
   fallingBox->setCollision(CreateCSphere(fallingBox, 5));
   objects.insert(objects.end(), fallingBox);

   wchar_t str[MAX_DESC + 1];
   StringCbPrintfW(str, MAX_DESC, L"Score: 0");
   velocitytxt_->set(str);

   StringCbPrintfW(str, MAX_DESC, L"Life left: 5"); 
   deltatxt_->set(str);
}
Пример #2
0
// update updates the position and orientation of each object according to the 
// actions initiated by the user
//
void Design::update() 
{
   bool
         translate = false,
         rotate    = false;
   int 
         delta = now - lastUpdate,
         dX = 0;
   float wZ = 0;
   static int 
         accum    = delta,
         next     = 3000,
         score    = 0,
         life     = 5,
         update   = true,
         level    = 0,
         bottom   = 0;
   wchar_t str[MAX_DESC + 1];

   if (!update)
   {
      StringCbPrintfW(str, MAX_DESC, L"GAME OVER"); 
      deltatxt_->set(str);

      StringCbPrintfW(str, MAX_DESC, L"Score: %d", score);
      velocitytxt_->set(str);
      return;
   }

   if (pressed(MOVE_LEFT))
   {
      translate = true;
      dX = -10;
   }
   
   if (pressed(ROTATE_LEFT))
   {
      rotate = true;
      wZ = -0.05f;
      bottom -= 2;
   }

   if (pressed(MOVE_RIGHT))
   {
      translate = true;
      dX = 10;
   }

   if (pressed(ROTATE_RIGHT))
   {
      rotate = true;
      wZ = 0.05f;
      bottom -= 2;
   }

   if (translate)
      catcher->translate(dX, 0, 0);
   if (rotate)
      catcher->rotatez(wZ);


   const CollisionContact* cc;
   cs_->populateContactList(delta/UNITS_PER_SEC);
   int nc=cs_->getNumContacts();
   std::vector<iPhysics*> toRemove; 

   if(nc!=0)
   {
      float vrn, J;
      Vector force,n,relativeVelocity;
      Vector g1deltap, g2deltap;

      for(int i = 1; i < nc ;i++)
      {
         cc = cs_->getContactList(i);
         
         
         iPhysics* g1 = cc->g1->getPhysics();
         iPhysics* g2 = cc->g2->getPhysics();

         if(g1 == catcher && g2 != catcher)
         {
            if (g2 == catcher)
            {
               catcher->translate(-dX, 0, 0);
               continue;
            }

            n = cc->normal;
            relativeVelocity = -g2->velocity();

            // normal component of the relative velocity
            vrn = dot(relativeVelocity, n);

            // magnitude of the impulse at collision
            J = - vrn * 2.0f / (1.0f / cc->g1->getPhysics()->mass() + 1.0f / cc->g2->getPhysics()->mass());

            // force generated by the impulse
            force = J * n / (float(delta)/float(UNITS_PER_SEC));

            // apply the force to both objects
            cc->g2->getPhysics()->addimpulseForce(-1 * force);
            // lose 4% of velocity
            cc->g2->getPhysics()->setVelocity(0.6f * (g2->velocity() + (-J * n)/ cc->g2->getPhysics()->mass()));

            //push objects apart so that it doesn't keep colliding
            float massTotal = (cc->g1->getPhysics()->mass() + cc->g2->getPhysics()->mass()) * 0.85f;
            float rbd1mf = cc->g1->getPhysics()->mass() / massTotal;
            float rbd2mf = cc->g2->getPhysics()->mass() / massTotal;
            Vector g1deltap (-1*(rbd1mf * cc->depth * n));
            Vector g2deltap (rbd2mf * cc->depth * n);

            cc->g2->getPhysics()->translate(g2deltap.x,g2deltap.y,g2deltap.z);
         }

         if (g1 == truck && g2 != truck)
         {
            if (g2 == catcher)
            {
               catcher->translate(-dX, 0, 0);
            }

            if (std::find(objects.begin(), objects.end(), g2) == objects.end())
               continue;

            toRemove.insert(toRemove.begin(), g2);
            StringCbPrintfW(str, MAX_DESC, L"Score: %d", ++score);
            velocitytxt_->set(str);
         }
      }
   }

   for (int i = toRemove.size() - 1; i >= 0; --i)
   {
      iPhysics* object = toRemove.at(i);
      cs_->remove(object->collisionGeometry());
      remove(object->bv());
      objects.remove(object);
   }

   accum += delta;

   if (accum > next)
   {
      if (score > 0 && !(score % 5))
      {
         level++;
         next -= 250;
      }

      Reflectivity yellowish = Reflectivity(yellow);
      iPhysics* fallingBox = CreatePhysicsBox(-10, -10, -10, 10, 10, 10, &yellowish, 1, PHYS_Falling, true);

      fallingBox->translate(-350, 350, 0);
      srand(time(0));
      fallingBox->setVelocity(Vector(rand() % 40 + 10 * level, -20, 0));
      fallingBox->addBodyForce(Vector(0, -10, 0));
      fallingBox->setCollision(CreateCSphere(fallingBox, 5));
      objects.insert(objects.end(), fallingBox);

      accum = 0;

      if (next < 0)
      {
         next = 100;
      }
   }

   Vector stretchPos = catcher->position();
   LIST_iPHYSICS::iterator itr = objects.begin();

   while (itr != objects.end())
   {
      iPhysics* object = *itr;
      Vector position = object->position();

      if (position.y < stretchPos.y - 5 + bottom)
      {
         if (--life < 0)
         {
            StringCbPrintfW(str, MAX_DESC, L"Your lost "); 
            deltatxt_->set(str);

            update = false;
         }

         cs_->remove(object->collisionGeometry());
         remove(object->bv());

         itr = objects.erase(itr);
         
         StringCbPrintfW(str, MAX_DESC, L"Life left: %d ", life); 
         deltatxt_->set(str);

         continue;
      }

      (*itr)->update(delta);
      ++itr;
   }
}
Пример #3
0
// initialize initializes the general display design coordinator, creates the 
// primitive sets, textures, objects, lights, sounds, cameras, and text items
//
void Design::initialize() {

    // general parameters
    //
	Reflectivity redish = Reflectivity(red);
	Reflectivity greenish = Reflectivity(green);
	Reflectivity bluish = Reflectivity(blue);
	Reflectivity whitish = Reflectivity(white);
	iGraphic* box;
    setProjection(0.9f, 1.0f, 1000.0f);
    setAmbientLight(0.9f, 0.9f, 0.9f);

    iCamera* camera = CreateCamera();
    camera->translate(0,150,0);
    camera->setRadius(17.8f);
	camera->rotatex(3.1459/2.0f);



    int i=0;
    for(i=0;i<map_.numvert();i++){
		 box   = CreateBox(-3, -3, -3 * MODEL_Z_AXIS,  3, 3, 3 * MODEL_Z_AXIS);
		CreateObject(box,&redish)->translate(map_.vx(i),map_.vy(i),map_.vz(i));
    }
    for(int j=0;j<map_.numvert();j++){
      LList<EdgeInfo>& edgelist=map_.edges(j);
      Node<EdgeInfo>* curr;
      while(curr=edgelist.curr()){
        int from=curr->data().from();
        int to=curr->data().to();
        if(from < to){
          Vector frompos=map_.pos(from);
          Vector topos=map_.pos(to);
		  float speed= curr->data().speed();
		  iGraphic* path=CreatePath(frompos.x,frompos.y,frompos.z,topos.x,topos.y,topos.z);
		  Reflectivity* pathcolour=(speed<FAST)?((speed<MEDIUM)?&redish:&bluish):&greenish;
		  CreateObject(path,pathcolour);
          #if DEBUG==1
          fprintf(debugfile,"from/to: %d %d\n",from,to);          
          fprintf(debugfile,"frompos %f %f %f\n",frompos.x,frompos.y,frompos.z);
          fprintf(debugfile,"topos %f %f %f\n",topos.x,topos.y,topos.z);
          fprintf(debugfile,"v %f %f %f\n",v.x,v.y,v.z);
          fprintf(debugfile,"edglen= %f\n",edgelen);
          #endif
		  i++;
        }
        edgelist.gonext();
      }
    }
	bucket_=CreatePhysicsBox(-2,-2,-2,2,2,2,&bluish,1,PHYS_FixedInSpace);
    whichbox_=0;
    Vector boxloc=map_.pos(whichbox_);
    bucket_->translate(boxloc.x,boxloc.y+4,boxloc.z);
	box = CreateBox(-3.1, -3.1, -3.1 * MODEL_Z_AXIS,  3.1, 3.1, 3.1 * MODEL_Z_AXIS);
	(highlighter_ = CreateObject(box,&greenish))->translate(map_.vx(0),map_.vy(0),map_.vz(0));
	selectloc_=0;
	lastFireTime_=0;
	ismoving_=false;
	searchroutine_=GREEDY;


	hud = CreateHUD(0.72f, 0.01f, 0.27f, 0.99f, CreateTexture(HUD_IMAGE));


	velocitytxt_=CreateText(Rectf(0.05f,0.27f,0.95f,0.37f),hud,L"",TEXT_HEIGHT,TEXT_TYPEFACE,TEXT_LEFT);
	deltatxt_=CreateText(Rectf(0.05f,0.17f,0.95f,0.27f),hud,L"",TEXT_HEIGHT,TEXT_TYPEFACE,TEXT_LEFT);
	positiontxt_=CreateText(Rectf(0.05f,0.38f,0.95f,0.48f),hud,L"",TEXT_HEIGHT,TEXT_TYPEFACE,TEXT_LEFT);


	lasttextupdate=now;
	
}