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
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 Point candidates[8];
00065
00066
00067
00068
00069 for(unsigned int i = 0; i < 8; i++)
00070 {
00071
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
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
00093
00094 Vector pos = m_carBody->position();
00095 unsigned int nearest = TerrainLoader::nearestIndex(
00096 pos[0], pos[2], candidates, 8);
00097
00098
00099 m_position[0] = candidates[nearest].x;
00100 m_position[2] = candidates[nearest].y;
00101
00102
00103
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
00115 m_loader->replaceTerrain(
00116 m_carBody->position(),
00117 Vector(candidates[nearest].x, 0.0, candidates[nearest].y));
00118
00119
00120
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
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 }