vrsode/src/Car3DS.cpp

Go to the documentation of this file.
00001 /*
00002 #include "Car3DS.h"
00003 #include "PhysicsManager.h"
00004 #include "LogManager.h"
00005 
00006 #include <vrs/box.h>
00007 #include <vrs/color.h>
00008 #include <vrs/io/filedataresource.h>
00009 #include <vrs/torus.h>
00010 #include <vrs/io/threedsreader.h>
00011 #include <vrs/vector.h>
00012 #include <vrs/opengl/shapematerialgl.h>
00013 #include <vrs/sg/scenegraphprinter.h>
00014 
00015 
00016 namespace vrsode
00017 {
00018 
00019 Car3DS::Car3DS(VRS::SO<VRS::SceneThing> p_parent)
00020 {
00021     m_springLengthFront = 0.0;
00022     m_springLengthRear = 0.0;
00023 
00024     m_springPowerFront = 31;
00025     m_springPowerRear = 27;
00026 
00027     m_springSpeedFront = 1.4;
00028     m_springSpeedRear = 1.4;
00029 
00030     m_wheelSizeFront = 1.0;
00031     m_wheelSizeRear = 1.0;
00032     m_wheelMass = 0.01;
00033 
00034     m_bodyMass = 8;
00035 
00036 
00037     m_rootNode = new VRS::SceneThing(p_parent);
00038     m_rootNode ->append(new VRS::ShapeMaterialGL(
00039             VRS::Color(0.1, 0.1, 0.1), VRS::Color(0.1, 0.1, 0.1)));
00040 
00041     VRS::SO<VRS::SceneThing> st;
00042 
00043     // collision space
00044 //  m_space = PhysicsManager::get()->createSimpleSpace(0);
00045 
00046     // chassis body
00047     st = new VRS::SceneThing(m_rootNode);
00048     st->append(new VRS::ShapeMaterialGL(
00049             VRS::Color(1.0, 0.3, 0.3), VRS::Color(0.15, 0.05, 0.05)));
00050 
00051     VRS::SO<VRS::ThreeDSReader> reader = new VRS::ThreeDSReader();
00052 
00053 
00054     st->append(((VRS::SceneThing*)reader->
00055             readObject("car01chassis.3ds", "Cube.002")->object(2))->object(2));
00056 
00057 //     st->append(new VRS::Box(VRS::Vector(-2.0, -0.2, -0.8),
00058 //                             VRS::Vector(2.0, 0.2, 0.8)));
00059 
00060     for(int i = 0; i < st->objects(); i++)
00061         std::cout << ((VRS::SceneThing*)st->object(i))->
00062                 getObjectName() << std::endl;
00063 
00064     m_chassis = PhysicsManager::get()->createPhysicsBody(st, CS_Box);
00065     m_chassis->setRotation(1,0,0,0,0,0,90);
00066 //     m_chassis->setDebugNode();
00067 //  m_space->addChild(m_chassis);
00068 
00069     // wheel body front left
00070     st = new VRS::SceneThing(m_rootNode);
00071     st->append(new VRS::Torus(m_wheelSizeFront));
00072     m_wheelFL = PhysicsManager::get()->createPhysicsBody(st, CS_Sphere);
00073 //     m_wheelFL->setDebugNode();
00074 //  m_space->addChild(m_wheelFL);
00075 
00076     // suspension body front left
00077     st = new VRS::SceneThing(m_rootNode);
00078     st->append(new VRS::Box(VRS::Vector(-0.01, -0.01, -0.01),
00079                             VRS::Vector( 0.01,  0.01,  0.01)));
00080     m_suspensionFL = PhysicsManager::get()->createPhysicsBody(st, CS_Box);
00081 //     m_suspensionFL->setDebugNode();
00082 //  m_space->addChild(m_suspensionFL);
00083 
00084     // steering body front left
00085     st = new VRS::SceneThing(m_rootNode);
00086     st->append(new VRS::Box(VRS::Vector(-0.01, -0.01, -0.01),
00087                             VRS::Vector( 0.01,  0.01,  0.01)));
00088     m_steerFL = PhysicsManager::get()->createPhysicsBody(st, CS_Box);
00089 //     m_steerFL->setDebugNode();
00090 //  m_space->addChild(m_steerFL);
00091 
00092     // wheel body front right
00093     st = new VRS::SceneThing(m_rootNode);
00094     st->append(new VRS::Torus(m_wheelSizeFront));
00095     m_wheelFR = PhysicsManager::get()->createPhysicsBody(st, CS_Sphere);
00096 //     m_wheelFR->setDebugNode();
00097 //  m_space->addChild(m_wheelFR);
00098 
00099     // suspension body front right
00100     st = new VRS::SceneThing(m_rootNode);
00101     st->append(new VRS::Box(VRS::Vector(-0.01, -0.01, -0.01),
00102                             VRS::Vector( 0.01,  0.01,  0.01)));
00103     m_suspensionFR = PhysicsManager::get()->createPhysicsBody(st, CS_Box);
00104 //     m_suspensionFR->setDebugNode();
00105 //  m_space->addChild(m_suspensionFR);
00106 
00107     // steering body front right
00108     st = new VRS::SceneThing(m_rootNode);
00109     st->append(new VRS::Box(VRS::Vector(-0.01, -0.01, -0.01),
00110                             VRS::Vector( 0.01,  0.01,  0.01)));
00111     m_steerFR = PhysicsManager::get()->createPhysicsBody(st, CS_Box);
00112 //     m_steerFR->setDebugNode();
00113 //  m_space->addChild(m_steerFR);
00114 
00115     // wheel body rear left
00116     st = new VRS::SceneThing(m_rootNode);
00117     st->append(new VRS::Torus(m_wheelSizeRear));
00118     m_wheelRL = PhysicsManager::get()->createPhysicsBody(st, CS_Sphere);
00119 //     m_wheelRL->setDebugNode();
00120 //  m_space->addChild(m_wheelRL);
00121 
00122     // suspension body rear left
00123     st = new VRS::SceneThing(m_rootNode);
00124     st->append(new VRS::Box(VRS::Vector(-0.01, -0.01, -0.01),
00125                             VRS::Vector( 0.01,  0.01,  0.01)));
00126     m_suspensionRL = PhysicsManager::get()->createPhysicsBody(st, CS_Box);
00127 //     m_suspensionRL->setDebugNode();
00128 //  m_space->addChild(m_suspensionRL);
00129 
00130     // wheel body rear right
00131     st = new VRS::SceneThing(m_rootNode);
00132     st->append(new VRS::Torus(m_wheelSizeRear));
00133     m_wheelRR = PhysicsManager::get()->createPhysicsBody(st, CS_Sphere);
00134 //     m_wheelRR->setDebugNode();
00135 //  m_space->addChild(m_wheelRR);
00136 
00137     // suspension body rear right
00138     st = new VRS::SceneThing(m_rootNode);
00139     st->append(new VRS::Box(VRS::Vector(-0.01, -0.01, -0.01),
00140                             VRS::Vector( 0.01,  0.01,  0.01)));
00141     m_suspensionRR = PhysicsManager::get()->createPhysicsBody(st, CS_Box);
00142 //     m_suspensionRR->setDebugNode();
00143 //  m_space->addChild(m_suspensionRR);
00144 
00145     // rotate wheels to position
00146     m_wheelFL->setRotation(1,0,0,0,0,0,90);
00147     m_wheelFR->setRotation(1,0,0,0,0,0,90);
00148     m_wheelRL->setRotation(1,0,0,0,0,0,90);
00149     m_wheelRR->setRotation(1,0,0,0,0,0,90);
00150 
00151     double maxSpringLength = m_springLengthFront > m_springLengthRear ?
00152                              m_springLengthFront : m_springLengthRear;
00153 
00154     // position chassis bodies
00155     m_chassis->setPosition( 0.0, 1.6 + maxSpringLength,  0.0);
00156 
00157     // position wheel bodies
00158     m_wheelFL->setPosition( 2.1, 1.6 + maxSpringLength, -1.8);
00159     m_wheelFR->setPosition( 2.1, 1.6 + maxSpringLength,  1.8);
00160     m_wheelRL->setPosition(-2.1, 1.6 + maxSpringLength, -1.8);
00161     m_wheelRR->setPosition(-2.1, 1.6 + maxSpringLength,  1.8);
00162 
00163     // position steer bodies
00164     m_steerFL->setPosition( 2.3, 1.0, -0.5);
00165     m_steerFR->setPosition( 2.3, 1.0,  0.5);
00166 
00167     // position suspension bodies
00168     m_suspensionFL->setPosition( 2.1, 1.1, -0.5);
00169     m_suspensionFR->setPosition( 2.1, 1.1,  0.5);
00170     m_suspensionRL->setPosition(-2.1, 1.1, -0.5);
00171     m_suspensionRR->setPosition(-2.1, 1.1,  0.5);
00172 
00173     // joints
00174     // front left
00175     // connect wheel and steering
00176     m_axisFL = PhysicsManager::get()->
00177             createHingeJoint(m_wheelFL, m_steerFL,
00178                              VRS::Vector( 2.1, 1.6 + maxSpringLength,-1.8),
00179                              VRS::Vector(0,0,1));
00180 
00181     // connect steering and suspension
00182     m_steerControlFL = PhysicsManager::get()->
00183             createHingeJoint(m_steerFL, m_suspensionFL,
00184                              VRS::Vector( 2.1, 0.7, -1.8),
00185                              VRS::Vector(0,1,0));
00186 
00187     // connect suspension and chassis
00188     m_springFL = PhysicsManager::get()->
00189             createSliderJoint(m_chassis, m_suspensionFL,
00190                              VRS::Vector(0,1,0));
00191 
00192     // front right
00193     // connect wheel and steering
00194     m_axisFR = PhysicsManager::get()->
00195             createHingeJoint(m_wheelFR, m_steerFR,
00196                              VRS::Vector( 2.1, 1.6 + maxSpringLength, 1.8),
00197                              VRS::Vector(0,0,1));
00198 
00199 
00200     // connect steering and suspension
00201     m_steerControlFR = PhysicsManager::get()->
00202             createHingeJoint(m_steerFR, m_suspensionFR,
00203                              VRS::Vector( 2.1, 0.7, 1.8),
00204                              VRS::Vector(0,1,0));
00205 
00206 
00207     // connect suspension and chassis
00208     m_springFR = PhysicsManager::get()->
00209             createSliderJoint(m_chassis, m_suspensionFR,
00210                              VRS::Vector(0,1,0));
00211 
00212     // rear left
00213     // connect wheel and suspension
00214     m_axisRL = PhysicsManager::get()->
00215             createHingeJoint(m_suspensionRL, m_wheelRL,
00216                              VRS::Vector(-2.1, 1.6 + maxSpringLength,-1.8),
00217                              VRS::Vector(0,0,1));
00218 
00219     // connect suspension and chassis
00220     m_springRL = PhysicsManager::get()->
00221             createSliderJoint(m_chassis, m_suspensionRL,
00222                              VRS::Vector(0,1,0));
00223 
00224     // rear right
00225     // connect wheel and suspension
00226     m_axisRR = PhysicsManager::get()->
00227             createHingeJoint(m_suspensionRR, m_wheelRR,
00228                              VRS::Vector(-2.1, 1.6 + maxSpringLength, 1.8),
00229                              VRS::Vector(0,0,1));
00230 
00231     // connect suspension and chassis
00232     m_springRR = PhysicsManager::get()->
00233             createSliderJoint(m_chassis, m_suspensionRR,
00234                              VRS::Vector(0,1,0));
00235 
00236     // high speed rotation mode
00237     m_wheelFL->setHighSpeedRotationMode(true);
00238     m_wheelFR->setHighSpeedRotationMode(true);
00239     m_wheelRL->setHighSpeedRotationMode(true);
00240     m_wheelRR->setHighSpeedRotationMode(true);
00241 
00242     m_wheelFL->setHighSpeedRotationAxis(VRS::Vector(0,0,1));
00243     m_wheelFR->setHighSpeedRotationAxis(VRS::Vector(0,0,1));
00244     m_wheelRL->setHighSpeedRotationAxis(VRS::Vector(0,0,1));
00245     m_wheelRR->setHighSpeedRotationAxis(VRS::Vector(0,0,1));
00246 
00247 
00248     // set masses
00249     m_chassis->setMass(m_bodyMass, VRS::Vector(0,-30,0));
00250     m_wheelFL->setMass(m_wheelMass);
00251     m_wheelFR->setMass(m_wheelMass);
00252     m_wheelRL->setMass(m_wheelMass);
00253     m_wheelRR->setMass(m_wheelMass);
00254 
00255     // configure suspension springs
00256     m_springFL->setHighStop(0.0);
00257     m_springFL->setLowStop(-m_springLengthFront);
00258     m_springFL->setMotorVelocity(m_springSpeedFront);
00259     m_springFL->setMotorMaxForce(m_springPowerFront);
00260 
00261     m_springFR->setHighStop(0.0);
00262     m_springFR->setLowStop(-m_springLengthFront);
00263     m_springFR->setMotorVelocity(m_springSpeedFront);
00264     m_springFR->setMotorMaxForce(m_springPowerFront);
00265 
00266     m_springRL->setHighStop(0.0);
00267     m_springRL->setLowStop(-m_springLengthRear);
00268     m_springRL->setMotorVelocity(m_springSpeedRear);
00269     m_springRL->setMotorMaxForce(m_springPowerRear);
00270 
00271     m_springRR->setHighStop(0.0);
00272     m_springRR->setLowStop(-m_springLengthRear);
00273     m_springRR->setMotorVelocity(m_springSpeedRear);
00274     m_springRR->setMotorMaxForce(m_springPowerRear);
00275 
00276     // configure steering
00277     m_steerControlFL->setHighStop(0.0);
00278     m_steerControlFL->setLowStop(0.0);
00279     m_steerControlFR->setHighStop(0.0);
00280     m_steerControlFR->setLowStop(0.0);
00281 
00282 //     // cfm
00283 //     double cfm = 0.000001;
00284 //
00285 //     m_axisFL->setCFM(cfm);
00286 //     m_axisFR->setCFM(cfm);
00287 //     m_axisRL->setCFM(cfm);
00288 //     m_axisRR->setCFM(cfm);
00289 //
00290 //     m_steerControlFL->setCFM(cfm);
00291 //     m_steerControlFR->setCFM(cfm);
00292 //
00293 //     m_springFL->setCFM(cfm);
00294 //     m_springFR->setCFM(cfm);
00295 //     m_springRL->setCFM(cfm);
00296 //     m_springRR->setCFM(cfm);
00297 
00298     // done :)
00299 }
00300 
00301 Car3DS::~Car3DS()
00302 {
00303     // TODO: cleanup
00304 }
00305 
00306 void
00307 Car3DS::accelerate(double p_amount)
00308 {
00309 
00310     if(p_amount)
00311     {
00312         m_axisRL->setMotorVelocity(100 * p_amount);
00313         m_axisRR->setMotorVelocity(100 * p_amount);
00314         m_axisRL->setMotorMaxForce(50);
00315         m_axisRR->setMotorMaxForce(50);
00316     }
00317     else
00318     {
00319         m_axisRL->setMotorVelocity(100);
00320         m_axisRR->setMotorVelocity(100);
00321         m_axisRL->setMotorMaxForce(0);
00322         m_axisRR->setMotorMaxForce(0);
00323     }
00324 }
00325 
00326 void
00327 Car3DS::steerLeft(double p_amount)
00328 {
00329     m_steerControlFL->setHighStop(-0.55 * p_amount);
00330     m_steerControlFL->setLowStop(-0.55 * p_amount);
00331     m_steerControlFR->setHighStop(-0.55 * p_amount);
00332     m_steerControlFR->setLowStop(-0.55 * p_amount);
00333 }
00334 
00335 void
00336 Car3DS::steerRight(double p_amount)
00337 {
00338     m_steerControlFL->setHighStop(0.55 * p_amount);
00339     m_steerControlFL->setLowStop(0.55 * p_amount);
00340     m_steerControlFR->setHighStop(0.55 * p_amount);
00341     m_steerControlFR->setLowStop(0.55 * p_amount);
00342 }
00343 
00344 void
00345 Car3DS::nos(double p_amount)
00346 {
00347     m_chassis->addRelativeForceAtRelativePosition(
00348             VRS::Vector(350 * p_amount,-40,0),
00349             VRS::Vector(-2,0,0));
00350 }
00351 
00352 void
00353 Car3DS::handBreak(bool p_enable)
00354 {
00355     if(p_enable)
00356     {
00357         m_axisFL->setMotorVelocity(0);
00358         m_axisFR->setMotorVelocity(0);
00359         m_axisFL->setMotorMaxForce(15);
00360         m_axisFR->setMotorMaxForce(15);
00361         m_axisRL->setMotorVelocity(0);
00362         m_axisRR->setMotorVelocity(0);
00363         m_axisRL->setMotorMaxForce(200);
00364         m_axisRR->setMotorMaxForce(200);
00365     }
00366     else
00367     {
00368         m_axisFL->setMotorVelocity(100);
00369         m_axisFR->setMotorVelocity(100);
00370         m_axisFL->setMotorMaxForce(0);
00371         m_axisFR->setMotorMaxForce(0);
00372         m_axisRL->setMotorVelocity(100);
00373         m_axisRR->setMotorVelocity(100);
00374         m_axisRL->setMotorMaxForce(0);
00375         m_axisRR->setMotorMaxForce(0);
00376     }
00377 }
00378 
00379 VRS::SO<VRS::SceneThing>
00380 Car3DS::sceneThing()
00381 {
00382     return m_chassis->sceneThing();
00383 }
00384 
00385 void
00386 Car3DS::attachCamera(VRS::SO<VRS::Camera> p_camera)
00387 {
00388 
00389 }
00390 
00391 }*/

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