Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 24, 2006, 6:41:08 PM (18 years ago)
Author:
bottac
Message:

collision detection.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/bsp_model/src/lib/graphics/importer/bsp_manager.cc

    r7801 r7833  
    3232#include "movie_player.h"
    3333
    34 
    35 
     34#include "cd_engine.h"
     35
     36#include "world_entity.h"
    3637
    3738BspManager::BspManager()
     
    4344  this->root  = this->bspFile->get_root();
    4445  this->alreadyVisible = new bool [this->bspFile->numFaces];
     46
     47  CDEngine::getInstance()->setBSPModel(this);
    4548}
    4649
     
    7780
    7881
    79  
    80  // this->viewDir=    State::getCameraTarget()->getAbsCoor() -  State::getCamera()->getAbsCoor() ;
     82
     83  // this->viewDir=    State::getCameraTarget()->getAbsCoor() -  State::getCamera()->getAbsCoor() ;
    8184  float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z);
    8285
     
    8891
    8992  this->cam = State::getCameraTargetNode()->getAbsCoor();
    90  // this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
     93  // this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
    9194  this->outputStartsOut = true;
    9295  this->outputAllSolid = false;
    9396  this->outputFraction = 1.0f;
    94  
     97
    9598  this->checkCollisionRay(this->root,0.0f,1.0f, &(State::getCameraTargetNode()->getLastAbsCoor()), &this->cam);
    96  
     99
    97100  if(this->outputFraction != 1.0f || this->outputAllSolid ) this->drawDebugCube(&this->cam);
    98101
    99102  if ( viscluster < 0  || ((int *)(this->bspFile->header))[35] == 0 )  //!< if (sizeof(Visdata) == 0)
    100103  {
    101    
    102    
    103    
     104
     105
     106
    104107    // Iterate through all Leafspublic final double readLEDouble()
    105108    for(int i = 0; i <  this->bspFile->numLeafs   ; i++ )
     
    108111      leaf& curLeaf = (this->bspFile->leaves)[i];
    109112      if(curLeaf.cluster<0) continue;
    110      
    111      
     113
     114
    112115      /** Do Frustum culling and draw 'em all **/
    113116      bool inFrustum = true;
    114117
    115118      Vector dir;
    116       dir.x = this->cam.x - State::getCameraTargetNode()->getLastAbsCoor().x;
    117       dir.y = this->cam.y - State::getCameraTargetNode()->getLastAbsCoor().y;
    118       dir.z = this->cam.z - State::getCameraTargetNode()->getLastAbsCoor().z;
     119      dir.x = State::getCameraNode()->getAbsDirZ().x;
     120      dir.y =  State::getCameraNode()->getAbsDirZ().y;
     121      dir.z =  State::getCameraNode()->getAbsDirZ().z;
    119122      float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z;
    120123      //if(dist < 0) dist = -dist;
    121124      const float dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] - dist ;
    122125      const float dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] - dist ;
    123      
    124       if(dMins < -30.0 && dMaxs < -30.0)
    125       {   
    126        //continue;
     126
     127      if(dMins < -1.0 && dMaxs < -1.0) {
     128        //continue;
    127129      }
    128       if( (this->cam - Vector(curLeaf.mins[0],curLeaf.mins[1], curLeaf.mins[2])).len() > 3000  && (this->cam - Vector(curLeaf.maxs[0],curLeaf.maxs[1], curLeaf.maxs[2])).len() > 3000)
    129       {
    130         //continue;
     130      if( (this->cam - Vector(curLeaf.mins[0],curLeaf.mins[1], curLeaf.mins[2])).len() > 3000  && (this->cam - Vector(curLeaf.maxs[0],curLeaf.maxs[1], curLeaf.maxs[2])).len() > 3000) {
     131        //continue;
    131132      }
    132    
    133        
     133
     134
    134135      // Iterate through all faces
    135136      for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
     
    138139        if (f >=0 && !this->isAlreadyVisible(f)) {
    139140          this->alreadyVisible[f] = true;
    140                
     141
    141142          addFace(f); // "visibleFaces.append(f)"
    142143        }
     
    147148
    148149    } //for
    149   }
    150   else {
    151 
    152  
     150  } else {
     151
     152
    153153    unsigned int v;
    154154    unsigned char  visSet;
     
    165165      visSet =((char*) (this->bspFile->visData))[v + 8];
    166166
    167       // gets bit of visSet 
     167      // gets bit of visSet
    168168      if( ((visSet) & (1 << (cluster &  7))) != 0 ) {
    169        
     169
    170170        // Frustum culling
    171    
     171
    172172        Vector dir;
    173         dir.x = this->cam.x - State::getCameraTargetNode()->getLastAbsCoor().x;
    174         dir.y = this->cam.y - State::getCameraTargetNode()->getLastAbsCoor().y;
    175         dir.z = this->cam.z - State::getCameraTargetNode()->getLastAbsCoor().z;
     173        dir.x = State::getCameraNode()->getAbsDirX().x;
     174        dir.y =  State::getCameraNode()->getAbsDirX().y;
     175        dir.z =  State::getCameraNode()->getAbsDirX().z;
    176176        const float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z;
    177       //if(dist < 0) dist = -dist;
     177        //if(dist < 0) dist = -dist;
    178178        const float dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] - dist;
    179179        const float dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] - dist;
    180      
    181         if(dMins < -150.0 && dMaxs < -150.0)
    182         {   
    183         continue;
     180
     181        if(dMins < -30.0 && dMaxs < -30.0) {
     182          continue;
    184183        }
    185        
    186        
     184
     185
    187186        // Iterate through all faces
    188187        for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
     
    238237
    239238  if(this->lastTex != curFace.texture) {
    240     if(this->bspFile->Materials[curFace.texture].animated)
    241     {
     239    if(this->bspFile->Materials[curFace.texture].animated) {
    242240      glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_COLOR);
    243241      glEnable(GL_BLEND);
     
    248246      glBindTexture(GL_TEXTURE_2D, n );
    249247      glDisable(GL_BLEND);
    250     }
    251     else
    252     {
    253     this->bspFile->Materials[curFace.texture].mat->select();
    254     this->lastTex = curFace.texture;
     248    } else {
     249      this->bspFile->Materials[curFace.texture].mat->select();
     250      this->lastTex = curFace.texture;
    255251    }
    256252  }
     
    267263
    268264  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    269  
    270  // glColor4f(3.0,3.0,3.0,1.0);
     265
     266  // glColor4f(3.0,3.0,3.0,1.0);
    271267  glEnableClientState(GL_VERTEX_ARRAY );
    272268  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
     
    370366  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
    371367  for(int i = Face->n_meshverts -1; i >=0   ; i--) {
    372    //glFrontFace(GL_CW);
     368    //glFrontFace(GL_CW);
    373369    //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]);
    374370
     
    382378    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    383379    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0]));
    384  
     380
    385381
    386382
     
    390386
    391387
    392   //  glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) );
     388    //  glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) );
    393389
    394390
     
    436432  float startDistance;
    437433  float endDistance;
    438  
     434
    439435  float startFraction = -1.0f;
    440436  float endFraction = 1.0f;
    441437  bool startsOut = false;
    442438  bool endsOut = false;
    443  
     439
    444440  Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor();
    445441  Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor();
    446442
    447   for (int i = 0; i < curBrush->n_brushsides; i++)
    448   {
     443  for (int i = 0; i < curBrush->n_brushsides; i++) {
    449444    brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ;
    450445    plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ;
    451446
    452        startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d;
    453      endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d;
     447    startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d;
     448    endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d;
    454449
    455450    if (startDistance > 0)
     
    458453      endsOut = true;
    459454
    460         // make sure the trace isn't completely on one side of the brush
    461     if (startDistance > 0 && endDistance > 0)
    462     {   // both are in front of the plane, its outside of this brush
     455    // make sure the trace isn't completely on one side of the brush
     456    if (startDistance > 0 && endDistance > 0) {   // both are in front of the plane, its outside of this brush
    463457      return;
    464458    }
    465     if (startDistance <= 0 && endDistance <= 0)
    466     {   // both are behind this plane, it will get clipped by another one
     459    if (startDistance <= 0 && endDistance <= 0) {   // both are behind this plane, it will get clipped by another one
    467460      continue;
    468461    }
    469462
    470         // MMM... BEEFY
    471     if (startDistance > endDistance)
    472     {   // line is entering into the brush
     463    // MMM... BEEFY
     464    if (startDistance > endDistance) {   // line is entering into the brush
    473465      float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // *
    474466      if (fraction > startFraction)
    475467        startFraction = fraction;
    476     }
    477     else
    478     {   // line is leaving the brush
     468    } else {   // line is leaving the brush
    479469      float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // *
    480470      if (fraction < endFraction)
    481471        endFraction = fraction;
    482472    }
    483    
    484   }
    485    if (startsOut == false)
    486     {
    487       this->outputStartsOut = false;
    488       if (endsOut == false)
    489         this->outputAllSolid = true;
    490       return;
    491     }
    492 
    493   if (startFraction < endFraction)
    494   {
    495     if (startFraction > -1.0f && startFraction < outputFraction)
    496     {
     473
     474  }
     475  if (startsOut == false) {
     476    this->outputStartsOut = false;
     477    if (endsOut == false)
     478      this->outputAllSolid = true;
     479    return;
     480  }
     481
     482  if (startFraction < endFraction) {
     483    if (startFraction > -1.0f && startFraction < outputFraction) {
    497484      if (startFraction < 0)
    498485        startFraction = 0;
     
    500487    }
    501488  }
    502  
     489
    503490}
    504491
     
    518505      //object *brush = &BSP.brushes[BSP.leafBrushes[leaf->firstLeafBrush + i]];
    519506      if (curBrush.n_brushsides > 0   &&
    520         ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1))
    521       // CheckBrush( brush );
    522       this->checkBrushRay(&curBrush);
     507          ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1))
     508        // CheckBrush( brush );
     509        this->checkBrushRay(&curBrush);
    523510      if(curBrush.n_brushsides <=0) this->outputAllSolid = true;
    524511    }
     
    548535      fraction2 = (startDistance + EPSILON) * inverseDistance;
    549536    } else if (endDistance < startDistance) {
    550       side = 0; // front(start)->x * (node->plane.x)+ 
     537      side = 0; // front(start)->x * (node->plane.x)+
    551538      float inverseDistance = 1.0f / (startDistance - endDistance);
    552539      fraction1 = (startDistance + EPSILON) * inverseDistance;
     
    596583
    597584}
     585
     586
     587void BspManager::checkCollision(WorldEntity* worldEntity)
     588{
     589
     590  Vector position = worldEntity->getAbsCoor();
     591
     592
     593  Vector forwardDir = worldEntity->getAbsDirX();
     594  forwardDir.x =10*forwardDir.x;
     595  forwardDir.y =10*forwardDir.y;
     596  forwardDir.z =10*forwardDir.z;
     597 
     598  Vector upDir = worldEntity->getAbsDirY();
     599  Vector dest = position;
     600  dest.x  += forwardDir.x;
     601  dest.y  += forwardDir.y;
     602  dest.z  += forwardDir.z;
     603  Vector out = Vector(-1875.0,-1875.0,-1875.0);
     604  if(!worldEntity->isA(CL_PLAYABLE)) {
     605  }
     606  else {
     607    bool collision = false;
     608    Vector position1 = position + Vector(0.0,0.1,0.0);
     609    Vector position2 = position + Vector(0.0,0.2,0.0);
     610    Vector dest1 = position1 + forwardDir;
     611    Vector dest2 = position2 + forwardDir;
     612    dest = position - upDir;
     613    Vector out1;
     614    Vector out2;
     615
     616    this->checkCollisionRay(this->root,0.0f,1.0f, &position1, &dest1 );
     617    if(this->outputFraction == 1.0f)  out1 = dest;
     618    else {
     619      collision = true;
     620      out1.x = position1.x + (dest1.x -position1.x) * this->outputFraction;
     621      out1.y = position1.y + (dest1.y -position1.y) * this->outputFraction;
     622      out1.z = position1.z + (dest1.z - position1.z) * this->outputFraction;
     623
     624    }
     625
     626    this->checkCollisionRay(this->root,0.0f,1.0f, &position2, &dest2 );
     627    if(this->outputFraction == 1.0f) out2= dest;
     628    else
     629    {
     630      collision = true;
     631    out2.x = position2.x + (dest2.x -position2.x) * this->outputFraction;
     632    out2.y = position2.y + (dest2.y -position2.y) * this->outputFraction;
     633    out2.z = position2.z + (dest2.z - position2.z) * this->outputFraction;
     634
     635    }
     636   
     637    this->checkCollisionRay(this->root,0.0f,1.0f, &position, &dest );
     638    if(this->outputFraction == 1.0f) out = dest;
     639    else
     640    {
     641    collision = true;
     642    out.x = position.x + (dest.x -position.x) * this->outputFraction;
     643    out.y = position.y + (dest.y -position.y) * this->outputFraction;
     644    out.z = position.z + (dest.z -position.z) * this->outputFraction;
     645 
     646    }
     647   
     648    if(collision) worldEntity->collidesWithGround(out,out1,out2);
     649 
     650  }
     651
     652}
     653
    598654void  BspManager::checkCollision(BspTreeNode* node, Vector* cam)
    599655{
     
    625681
    626682    if (camLeaf.cluster < 0) {
    627                               this->drawDebugCube(&this->cam);
    628                               this->drawDebugCube(&next);
    629                               State::getPlayer()->getPlayable()->setRelCoor(-100,-100,-100);
    630                               State::getPlayer()->getPlayable()->collidesWith(NULL, State::getCameraTargetNode()->getLastAbsCoor());
    631                               }
     683      this->drawDebugCube(&this->cam);
     684      this->drawDebugCube(&next);
     685      State::getPlayer()->getPlayable()->setRelCoor(-100,-100,-100);
     686      State::getPlayer()->getPlayable()->collidesWith(NULL, State::getCameraTargetNode()->getLastAbsCoor());
     687    }
    632688
    633689
Note: See TracChangeset for help on using the changeset viewer.