Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10681 in orxonox.OLD


Ignore:
Timestamp:
Jun 9, 2007, 12:06:51 AM (17 years ago)
Author:
rennerc
Message:

less magic

Location:
branches/adm/src/world_entities/npcs
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/adm/src/world_entities/npcs/adm_turret.cc

    r10679 r10681  
    6767                        .describe("The filename of the World Entity, that is to be shot at")
    6868                        .defaultValues("");
     69               
     70                LoadParam(root, "type", this, AdmTurret, setType)
     71                        .describe("floor|ceil");
    6972
    7073                LoadParamXML(root, "Cannons", this, AdmTurret, addCannons)
     
    7477
    7578        }
     79//         this->removeNodeFlags( PNODE_ALL );
     80//         this->addNodeFlags( PNODE_ROTATE_AND_MOVE );
    7681       
    77         //HACK this is really ugly because if you move the AdmTurret its cannons wont follow
    78         if ( this->cannons )
    79           this->cannons->setParent( NullParent::getNullParent() );
     82        if ( this->isCeil )
     83        {
     84          Vector a = this->sensor->getRelCoor();
     85          this->sensor->setRelCoor( -a.x, -a.y, -a.z );
     86          a = this->cannons->getRelCoor();
     87          this->cannons->setRelCoor( -a.x, -a.y, -a.z );
     88        }
    8089}
    8190
     
    96105}
    97106
     107void AdmTurret::setType( const std::string& type )
     108{
     109  if ( type == "floor" )
     110  {
     111    this->isCeil = false;
     112    return;
     113  }
     114 
     115  if ( type == "ceil" )
     116  {
     117    this->isCeil = true;
     118    return;
     119  }
     120 
     121  //invalid argument
     122  PRINTF(1)("%s is not a valid type for AdmTurret\n", type.c_str());
     123  assert("false");
     124}
     125
    98126void AdmTurret::init()
    99127{
    100128        this->registerObject(this, AdmTurret::_objectList);
    101129       
    102         //this->removeNodeFlags( 7 );
     130        this->bodyAngle = this->cannonAngle = 0.0f;
     131        this->isActive = true;
     132        this->range = 400;
     133        this->isCeil = false;
    103134}
    104135
     
    112143
    113144  Vector playerPos = this->myTarget->getAbsCoor();
    114   this->moveTowards( playerPos - ( this->cannons->getAbsCoor() ),  dt);
     145  Vector ds = playerPos - ( this->cannons->getAbsCoor() );
     146  if ( isActive && ds.len() <= range )
     147    this->moveTowards( ds,  dt);
     148  else
     149    this->moveTowards( Vector(0, -1, 0), dt );
    115150}
    116151
     
    216251void AdmTurret::moveTowards( Vector targetDir, float dt )
    217252{
    218 #if 0
    219   static Quaternion mrot = this->getAbsDir();
    220   Quaternion ry( dt, Vector(0, 1, 0) );
    221   mrot *= ry;
    222   this->setAbsDir(mrot);
    223 
    224   static Quaternion rot = this->cannons->getAbsDir();
    225   Quaternion rx( dt, Vector( 0, 0, 1 ) );
    226   rot *= rx;
    227   this->cannons->setAbsDir(rot*mrot);
    228   return;
    229 #endif
    230 
    231 
    232 
    233 
    234   Quaternion cur = this->getAbsDir();
    235  
    236   Quaternion ry( dt, cur.inverse().apply( Vector( 0, 1, 0 ) ) );
    237  
    238   Quaternion tmp1 = cur * ry;
    239   Quaternion tmp2 = cur * ry.inverse();
    240  
    241   bool usetmp1 = false;
    242   Quaternion r1;
    243   if ( tmp1.apply( Vector(1, 0, 0) ).dot(targetDir) < tmp2.apply( Vector(1, 0, 0)).dot(targetDir) )
     253  if ( this->cannons->getParent() != NullParent::getNullParent() )
     254    this->cannons->setParent( NullParent::getNullParent() );
     255 
     256  targetDir.normalize();
     257 
     258  float dAngle = dt;
     259 
     260  float bestResult = -1.0f;
     261  Quaternion bestBodyRot;
     262  Quaternion bestCannonRot;
     263  float bestBodyAngle = 0.0f;
     264  float bestCannonAngle = 0.0f;
     265 
     266//   Quaternion baseRot(0, Vector);
     267//   if ( isCeil )
     268//   {
     269//     printf( "ceil\n" );
     270//     baseRot = Quaternion( PI/2, Vector( 0, 0, 1 ) );
     271//   }
     272 
     273  for ( int dBodyAngle = -1; dBodyAngle<2; dBodyAngle++ )
    244274  {
    245     usetmp1 = true;
    246     cur = tmp1;
    247     r1 = ry;
     275    for ( int dCannonAngle = -1; dCannonAngle<2; dCannonAngle++ )
     276    {
     277      float bodyAngle = this->bodyAngle + dBodyAngle*dAngle;
     278      float cannonAngle = this->cannonAngle + dCannonAngle*dAngle;
     279     
     280      while ( bodyAngle > 2*PI )
     281        bodyAngle -= 2*PI;
     282      while ( bodyAngle < 0 )
     283        bodyAngle += 2*PI;
     284      while ( cannonAngle > 2*PI )
     285        cannonAngle -= 2*PI;
     286      while ( cannonAngle < 0 )
     287        cannonAngle += 2*PI;
     288     
     289      Quaternion bodyRot( bodyAngle, Vector( 0, 1, 0 ) );
     290      Quaternion cannonRot( cannonAngle, Vector( 0, 0, 1) );
     291     
     292//       bodyRot = baseRot * bodyRot;
     293//       cannonRot = baseRot * cannonRot;
     294     
     295      float result = (bodyRot * cannonRot).apply( Vector( -1, 0, 0 ) ).dot( targetDir );
     296     
     297      if ( result > bestResult )
     298      {
     299        bestResult = result;
     300        bestBodyRot = bodyRot;
     301        bestBodyAngle = bodyAngle;
     302        bestCannonRot = cannonRot;
     303        bestCannonAngle = cannonAngle;
     304      }
     305    }
    248306  }
    249   else
    250   {
    251     cur = tmp2;
    252     r1 = ry.inverse();
    253   }
    254  
    255   this->setAbsDir( cur );
    256 
    257  
    258   Quaternion cur2 = this->cannons->getAbsDir();
    259   Quaternion rx( dt, cur.inverse().apply( Vector( 0, 0, 1 ) ) );
    260   Quaternion rr( dt, cur2.inverse().apply( Vector( 0, 1, 0 ) ) );
    261   if ( !usetmp1 )
    262     rr = rr.inverse();
    263  
    264  
    265   tmp1 = cur2 * rx;
    266   tmp2 = cur2 * rx.inverse();
    267 
    268   Quaternion dec;
    269   Quaternion r2;
    270   if ( tmp1.apply(  Vector(-1, 0, 0) ).dot(targetDir) > tmp2.apply( Vector(-1, 0, 0) ).dot(targetDir) )
    271   {
    272     r2 = rx;
    273     dec = tmp1;
    274   }
    275   else
    276   {
    277     r2 = rx.inverse();
    278     dec = tmp2;
    279   }
    280  
    281   float dp = dec.apply( Vector(-1, 0, 0) ).dot(Vector(0, 1, 0));
    282   if ( dp > -0.9 && dp < 0.9 )
    283   {
    284     cur2 = dec;
    285   }
    286 
    287   this->cannons->setAbsDir( cur2  );
    288 
    289 
    290 
    291 }
     307 
     308  this->bodyAngle = bestBodyAngle;
     309  this->cannonAngle = bestCannonAngle;
     310 
     311  this->setAbsDir( bestBodyRot );
     312  this->cannons->setAbsDir( bestBodyRot * bestCannonRot );
     313}
  • branches/adm/src/world_entities/npcs/adm_turret.h

    r10677 r10681  
    4444
    4545                   void moveTowards( Vector targetDir, float dt );
     46                   
     47                   float bodyAngle;
     48                   float cannonAngle;
     49                   
     50                   bool isActive;
     51                   float range;
     52                   
     53                   bool isCeil;
     54                   void setType( const std::string& type );
    4655                                           
    4756      };
Note: See TracChangeset for help on using the changeset viewer.