random_racer/src/LoaderGlue.cpp

Go to the documentation of this file.
00001 #include <math.h>
00002 #include <stdarg.h>
00003 #include <stdio.h>
00004 
00005 #include <sstream>
00006 
00007 #include <vrsode/CollisionBody.h>
00008 #include <vrsode/PlaneShape.h>
00009 #include <vrsode/PhysicsManager.h>
00010 
00011 #include "Console.h"
00012 #include "ItemDistributor.h"
00013 #include "ReloadPlane.h"
00014 #include "TerrainLoader.h"
00015 
00016 #include "LoaderGlue.h"
00017 
00018 using namespace VRS;
00019 using namespace vrsode;
00020 
00021 
00022 namespace random_racer
00023 {
00024 
00025 LoaderGlue::LoaderGlue(
00026     SO<CollisionBody> p_collisionBody,
00027     SO<TerrainLoader> p_loader,
00028     float p_boxSize,
00029     float p_centerDiff,
00030     Vector p_position,
00031     SO<ItemDistributor> p_itemDistributor)
00032 {
00033     m_carBody       = p_collisionBody;
00034     m_loader        = p_loader;
00035     m_halfBoxSize   = p_boxSize/2.0;
00036     m_centerDiff    = p_centerDiff;
00037     m_position      = p_position;
00038     m_callback      = new MethodCallback1<LoaderGlue, unsigned int>(
00039                           this, &LoaderGlue::collisionCallback);
00040     m_itemDistributor = p_itemDistributor;
00041 }
00042 
00043 LoaderGlue::~LoaderGlue()
00044 {
00045 }
00046 
00047 void
00048 LoaderGlue::collisionCallback(unsigned int p_id)
00049 {
00050     // std::cout << "ZACK " << p_id
00051     //           << (p_id > 3 ? "  ODE " : " (VRS)") << std::endl;
00052 
00053     // ok, we have 8 possible new center points for our
00054     // new planes, this is how they are located, the dot
00055     // is our previous position.
00056     //
00057     //   7    0    1
00058     //
00059     //   6    .    2
00060     //
00061     //   5    4    3
00062     //
00063     // the difference between 0 and . is m_centerDiff
00064     Point candidates[8];
00065 
00066     // FIXME: maybe optimize this if there is time
00067 
00068     // fill all possible next points with defaults
00069     for(unsigned int i = 0; i < 8; i++)
00070     {
00071         // 0, 1, 7
00072         if(i == 7 || i <= 1)
00073             candidates[i].y = m_position[2] + m_centerDiff;
00074 
00075         else if(i == 6 || i == 2)
00076             candidates[i].y = m_position[2];
00077 
00078         else
00079             candidates[i].y = m_position[2] - m_centerDiff;
00080 
00081         // 1, 2, 3
00082         if(i != 0 && i <= 3)
00083             candidates[i].x = m_position[0] + m_centerDiff;
00084 
00085         else if(i == 0 || i == 4)
00086             candidates[i].x = m_position[0];
00087 
00088         else
00089             candidates[i].x = m_position[0] - m_centerDiff;
00090     }
00091 
00092     // find the nearest possible next plane center for the cars
00093     // current position.
00094     Vector pos = m_carBody->position();
00095     unsigned int nearest = TerrainLoader::nearestIndex(
00096                                                  pos[0], pos[2], candidates, 8);
00097 
00098     // remember this position for the next time
00099     m_position[0] = candidates[nearest].x;
00100     m_position[2] = candidates[nearest].y;
00101 
00102 
00103     // lock only if we are in vrs thread and the reloacte the planes
00104     if(p_id < 4)
00105     {    
00106         PhysicsManager::get()->lock();
00107         relocatePlanesAround(candidates[nearest].x, candidates[nearest].y);
00108         PhysicsManager::get()->unlock();
00109     }
00110     else
00111         relocatePlanesAround(candidates[nearest].x, candidates[nearest].y);
00112 
00113 
00114     // tell the loader to replace our terrain
00115     m_loader->replaceTerrain(
00116             m_carBody->position(),
00117             Vector(candidates[nearest].x, 0.0, candidates[nearest].y));
00118 
00119 
00120     // tell the itemditributor to distribute new coins
00121     if(m_itemDistributor != NULL)
00122         m_itemDistributor->distributeItems(3, m_carBody->position(), 
00123                 m_loader->viewSize() / 3.0);
00124 }
00125 
00126 void
00127 LoaderGlue::registerPlanes(SO<ReloadPlane>* p_planeArray)
00128 {
00129     for(unsigned int i = 0; i < 4; i++)
00130         m_planes[i] = p_planeArray[i];
00131 
00132     m_planes[0]->relocate(Vector(-1,0,0), 0.0);
00133     m_planes[1]->relocate(Vector(1,0,0),  0.0);
00134     m_planes[2]->relocate(Vector(0,0,-1), 0.0);
00135     m_planes[3]->relocate(Vector(0,0,1),  0.0);
00136 
00137     Vector pos = m_carBody->position();
00138     relocatePlanesAround(pos[0], pos[2]);
00139 }
00140 
00141 void
00142 LoaderGlue::relocatePlanesAround(double p_x, double p_y)
00143 {
00144     // std::cout << "locating around " << p_x << ", "<< p_y << std::endl;
00145 
00146     m_planes[0]->relocate( -(m_halfBoxSize) - p_x );
00147     m_planes[1]->relocate( p_x - (m_halfBoxSize)  );
00148     m_planes[2]->relocate( -(m_halfBoxSize) - p_y );
00149     m_planes[3]->relocate( p_y - (m_halfBoxSize)  );
00150 }
00151 
00152 void
00153 LoaderGlue::checkForCollisions()
00154 {
00155     for(unsigned int i = 0; i < 4; i++)
00156         m_planes[i]->checkForCollision();
00157 }
00158 
00159 } // namespace random_racer

Generated on Fri May 11 21:01:57 2007 for Random Racer by  doxygen 1.5.1