00001
00002 #include "CarSimple.h"
00003 #include "PhysicsManager.h"
00004 #include "CylinderShape.h"
00005 #include "random_utils/LogManager.h"
00006
00007 #include <vrs/box.h>
00008 #include <vrs/color.h>
00009 #include <vrs/torus.h>
00010 #include <vrs/vector.h>
00011 #include <vrs/opengl/shapematerialgl.h>
00012 #include <vrs/translation.h>
00013
00014
00015 namespace vrsode
00016 {
00017
00018 CarSimple::CarSimple(VRS::SO<VRS::SceneThing> p_parent)
00019 {
00020 m_springLengthFront = 0.2;
00021 m_springLengthRear = 0.2;
00022 m_springPowerFront = 31;
00023 m_springPowerRear = 27;
00024 m_springSpeedFront = 1.4;
00025 m_springSpeedRear = 1.4;
00026 m_wheelSizeFront = 0.3;
00027 m_wheelSizeRear = 0.4;
00028 m_wheelMass = 0.01;
00029 m_bodyMass = 8.0;
00030 m_debugNodes = true;
00031 m_wheelMu = dInfinity;
00032
00033 m_carMaxSpeed = 25;
00034 m_carPower = 50;
00035
00036 m_chassisOffset = VRS::Vector( 0.0, 1.6, 0.0);
00037
00038 m_wheelOffsetFL = VRS::Vector( 2.1, 1.0, -1.8);
00039 m_wheelOffsetFR = VRS::Vector( 2.1, 1.0, 1.8);
00040 m_wheelOffsetRL = VRS::Vector(-2.1, 1.0, -1.8);
00041 m_wheelOffsetRR = VRS::Vector(-2.1, 1.0, 1.8);
00042
00043 m_suspensionOffsetFL = VRS::Vector( 2.1, 1.1, -0.5);
00044 m_suspensionOffsetFR = VRS::Vector( 2.1, 1.1, 0.5);
00045 m_suspensionOffsetRL = VRS::Vector(-2.1, 1.1, -0.5);
00046 m_suspensionOffsetRR = VRS::Vector(-2.1, 1.1, 0.5);
00047
00048 m_steerOffsetFL = VRS::Vector( 2.3, 1.0, -0.5);
00049 m_steerOffsetFR = VRS::Vector( 2.3, 1.0, 0.5);
00050
00051
00052 m_rootNode = new VRS::SceneThing(p_parent);
00053 m_rootNode ->append(new VRS::ShapeMaterialGL(
00054 VRS::Color(0.1, 0.1, 0.1), VRS::Color(0.1, 0.1, 0.1)));
00055
00056 VRS::SO<VRS::SceneThing> st;
00057
00058
00059 st = new VRS::SceneThing(m_rootNode);
00060 st->append(new VRS::ShapeMaterialGL(
00061 VRS::Color(1.0, 0.3, 0.3), VRS::Color(0.15, 0.05, 0.05)));
00062 st->append(new VRS::Box(VRS::Vector(-2.0, -0.2, -0.8),
00063 VRS::Vector(2.0, 0.2, 0.8)));
00064 m_chassis = new PhysicsBody(st, CS_Box);
00065 m_chassis->setDebugNode(m_debugNodes);
00066
00067
00068 st = new VRS::SceneThing(m_rootNode);
00069 st->append(new VRS::Torus(m_wheelSizeFront));
00070
00071 VRS::SO<Shape> wheelShapeFL =
00072 new CylinderShape(VRS::Vector(0,1,0),1,m_wheelSizeFront*2.0);
00073 m_wheelFL = new PhysicsBody(st, wheelShapeFL);
00074
00075 m_wheelFL->setDebugNode(m_debugNodes);
00076 m_wheelFL->surface()->setMu(m_wheelMu);
00077
00078
00079 st = new VRS::SceneThing(m_rootNode);
00080 m_suspensionFL = new PhysicsBody(st, CS_None);
00081 m_suspensionFL->setDebugNode(m_debugNodes);
00082
00083
00084 st = new VRS::SceneThing(m_rootNode);
00085 m_steerFL = new PhysicsBody(st, CS_None);
00086 m_steerFL->setDebugNode(m_debugNodes);
00087
00088
00089 st = new VRS::SceneThing(m_rootNode);
00090 st->append(new VRS::Torus(m_wheelSizeFront));
00091 VRS::SO<Shape> wheelShapeFR =
00092 new CylinderShape(VRS::Vector(0,1,0),1,m_wheelSizeFront*2.0);
00093 m_wheelFR = new PhysicsBody(st, wheelShapeFR);
00094
00095 m_wheelFR->setDebugNode(m_debugNodes);
00096 m_wheelFR->surface()->setMu(m_wheelMu);
00097
00098
00099 st = new VRS::SceneThing(m_rootNode);
00100 m_suspensionFR = new PhysicsBody(st, CS_None);
00101 m_suspensionFR->setDebugNode(m_debugNodes);
00102
00103
00104 st = new VRS::SceneThing(m_rootNode);
00105 m_steerFR = new PhysicsBody(st, CS_None);
00106 m_steerFR->setDebugNode(m_debugNodes);
00107
00108
00109 st = new VRS::SceneThing(m_rootNode);
00110 st->append(new VRS::Torus(m_wheelSizeRear));
00111 VRS::SO<Shape> wheelShapeRL =
00112 new CylinderShape(VRS::Vector(0,1,0),1,m_wheelSizeRear*2.0);
00113 m_wheelRL = new PhysicsBody(st, wheelShapeRL);
00114
00115 m_wheelRL->setDebugNode(m_debugNodes);
00116 m_wheelRL->surface()->setMu(m_wheelMu);
00117
00118
00119 st = new VRS::SceneThing(m_rootNode);
00120 m_suspensionRL = new PhysicsBody(st, CS_None);
00121 m_suspensionRL->setDebugNode(m_debugNodes);
00122
00123
00124 st = new VRS::SceneThing(m_rootNode);
00125 st->append(new VRS::Torus(m_wheelSizeRear));
00126 VRS::SO<Shape> wheelShapeRR =
00127 new CylinderShape(VRS::Vector(0,1,0),1,m_wheelSizeRear*2.0);
00128 m_wheelRR = new PhysicsBody(st, wheelShapeRR);
00129
00130 m_wheelRR->setDebugNode(m_debugNodes);
00131 m_wheelRR->surface()->setMu(m_wheelMu);
00132
00133
00134 st = new VRS::SceneThing(m_rootNode);
00135 m_suspensionRR = new PhysicsBody(st, CS_None);
00136 m_suspensionRR->setDebugNode(m_debugNodes);
00137
00138
00139 m_wheelFL->setRotation(1,0,0,90);
00140 m_wheelFR->setRotation(1,0,0,90);
00141 m_wheelRL->setRotation(1,0,0,90);
00142 m_wheelRR->setRotation(1,0,0,90);
00143
00144
00145 m_chassis->setPosition(m_chassisOffset);
00146
00147
00148 m_wheelFL->setPosition(m_wheelOffsetFL);
00149 m_wheelFR->setPosition(m_wheelOffsetFR);
00150 m_wheelRL->setPosition(m_wheelOffsetRL);
00151 m_wheelRR->setPosition(m_wheelOffsetRR);
00152
00153
00154 m_steerFL->setPosition(m_steerOffsetFL);
00155 m_steerFR->setPosition(m_steerOffsetFR);
00156
00157
00158 m_suspensionFL->setPosition(m_suspensionOffsetFL);
00159 m_suspensionFR->setPosition(m_suspensionOffsetFR);
00160 m_suspensionRL->setPosition(m_suspensionOffsetRL);
00161 m_suspensionRR->setPosition(m_suspensionOffsetRR);
00162
00163
00164
00165
00166 m_axisFL = new PhysicsJointHinge(m_wheelFL, m_steerFL,
00167 VRS::Vector( 2.1, 1.0,-1.8),
00168 VRS::Vector(0,0,1));
00169
00170
00171 m_steerControlFL =new PhysicsJointHinge(m_steerFL, m_suspensionFL,
00172 VRS::Vector( 2.1, 0.7, -1.8),
00173 VRS::Vector(0,1,0));
00174
00175
00176 m_springFL = new PhysicsJointSlider(m_chassis, m_suspensionFL,
00177 VRS::Vector(0,1,0));
00178
00179
00180
00181 m_axisFR = new PhysicsJointHinge(m_wheelFR, m_steerFR,
00182 VRS::Vector( 2.1, 1.0, 1.8),
00183 VRS::Vector(0,0,1));
00184
00185
00186
00187 m_steerControlFR = new PhysicsJointHinge(m_steerFR, m_suspensionFR,
00188 VRS::Vector( 2.1, 0.7, 1.8),
00189 VRS::Vector(0,1,0));
00190
00191
00192
00193 m_springFR = new PhysicsJointSlider(m_chassis, m_suspensionFR,
00194 VRS::Vector(0,1,0));
00195
00196
00197
00198 m_axisRL = new PhysicsJointHinge(m_suspensionRL, m_wheelRL,
00199 VRS::Vector(-2.1, 1.0,-1.8),
00200 VRS::Vector(0,0,1));
00201
00202
00203 m_springRL = new PhysicsJointSlider(m_chassis, m_suspensionRL,
00204 VRS::Vector(0,1,0));
00205
00206
00207
00208 m_axisRR = new PhysicsJointHinge(m_suspensionRR, m_wheelRR,
00209 VRS::Vector(-2.1, 1.0, 1.8),
00210 VRS::Vector(0,0,1));
00211
00212
00213 m_springRR = new PhysicsJointSlider(m_chassis, m_suspensionRR,
00214 VRS::Vector(0,1,0));
00215
00216
00217 m_wheelFL->setHighSpeedRotationAxis(VRS::Vector(0,0,1));
00218 m_wheelFR->setHighSpeedRotationAxis(VRS::Vector(0,0,1));
00219 m_wheelRL->setHighSpeedRotationAxis(VRS::Vector(0,0,1));
00220 m_wheelRR->setHighSpeedRotationAxis(VRS::Vector(0,0,1));
00221
00222 m_wheelFL->setHighSpeedRotationMode(true);
00223 m_wheelFR->setHighSpeedRotationMode(true);
00224 m_wheelRL->setHighSpeedRotationMode(true);
00225 m_wheelRR->setHighSpeedRotationMode(true);
00226
00227
00228 m_chassis->setMass(m_bodyMass, VRS::Vector(0,-30,0));
00229 m_wheelFL->setMass(m_wheelMass);
00230 m_wheelFR->setMass(m_wheelMass);
00231 m_wheelRL->setMass(m_wheelMass);
00232 m_wheelRR->setMass(m_wheelMass);
00233
00234
00235 m_springFL->setHighStop(0.0);
00236 m_springFL->setLowStop(-m_springLengthFront);
00237 m_springFL->setMotorVelocity(m_springSpeedFront);
00238 m_springFL->setMotorMaxForce(m_springPowerFront);
00239
00240 m_springFR->setHighStop(0.0);
00241 m_springFR->setLowStop(-m_springLengthFront);
00242 m_springFR->setMotorVelocity(m_springSpeedFront);
00243 m_springFR->setMotorMaxForce(m_springPowerFront);
00244
00245 m_springRL->setHighStop(0.0);
00246 m_springRL->setLowStop(-m_springLengthRear);
00247 m_springRL->setMotorVelocity(m_springSpeedRear);
00248 m_springRL->setMotorMaxForce(m_springPowerRear);
00249
00250 m_springRR->setHighStop(0.0);
00251 m_springRR->setLowStop(-m_springLengthRear);
00252 m_springRR->setMotorVelocity(m_springSpeedRear);
00253 m_springRR->setMotorMaxForce(m_springPowerRear);
00254
00255
00256 m_steerControlFL->setHighStop(0.0);
00257 m_steerControlFL->setLowStop(0.0);
00258 m_steerControlFR->setHighStop(0.0);
00259 m_steerControlFR->setLowStop(0.0);
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 }
00279
00280 CarSimple::~CarSimple()
00281 {
00282
00283 }
00284
00285 void
00286 CarSimple::accelerate(double p_amount)
00287 {
00288 enableAllBodies();
00289
00290 if(p_amount)
00291 {
00292 m_axisRL->setMotorVelocity(m_carMaxSpeed * p_amount);
00293 m_axisRR->setMotorVelocity(m_carMaxSpeed * p_amount);
00294 m_axisRL->setMotorMaxForce(m_carPower);
00295 m_axisRR->setMotorMaxForce(m_carPower);
00296 }
00297 else
00298 {
00299 m_axisRL->setMotorVelocity(m_carMaxSpeed);
00300 m_axisRR->setMotorVelocity(m_carMaxSpeed);
00301 m_axisRL->setMotorMaxForce(0);
00302 m_axisRR->setMotorMaxForce(0);
00303 }
00304 }
00305
00306 void
00307 CarSimple::steerLeft(double p_amount)
00308 {
00309 enableAllBodies();
00310 m_steerControlFL->setHighStop(-0.55 * p_amount);
00311 m_steerControlFL->setLowStop(-0.55 * p_amount);
00312 m_steerControlFR->setHighStop(-0.55 * p_amount);
00313 m_steerControlFR->setLowStop(-0.55 * p_amount);
00314 }
00315
00316 void
00317 CarSimple::steerRight(double p_amount)
00318 {
00319 enableAllBodies();
00320 m_steerControlFL->setHighStop(0.55 * p_amount);
00321 m_steerControlFL->setLowStop(0.55 * p_amount);
00322 m_steerControlFR->setHighStop(0.55 * p_amount);
00323 m_steerControlFR->setLowStop(0.55 * p_amount);
00324 }
00325
00326 void
00327 CarSimple::nos(double p_amount)
00328 {
00329 enableAllBodies();
00330 m_chassis->addRelativeForceAtRelativePosition(
00331 VRS::Vector(350 * p_amount,-40,0),
00332 VRS::Vector(-2,0,0));
00333 }
00334
00335 void
00336 CarSimple::handBreak(bool p_enable)
00337 {
00338 enableAllBodies();
00339 if(p_enable)
00340 {
00341 m_axisFL->setMotorVelocity(0);
00342 m_axisFR->setMotorVelocity(0);
00343 m_axisFL->setMotorMaxForce(15);
00344 m_axisFR->setMotorMaxForce(15);
00345 m_axisRL->setMotorVelocity(0);
00346 m_axisRR->setMotorVelocity(0);
00347 m_axisRL->setMotorMaxForce(200);
00348 m_axisRR->setMotorMaxForce(200);
00349 }
00350 else
00351 {
00352 m_axisFL->setMotorVelocity(100);
00353 m_axisFR->setMotorVelocity(100);
00354 m_axisFL->setMotorMaxForce(0);
00355 m_axisFR->setMotorMaxForce(0);
00356 m_axisRL->setMotorVelocity(100);
00357 m_axisRR->setMotorVelocity(100);
00358 m_axisRL->setMotorMaxForce(0);
00359 m_axisRR->setMotorMaxForce(0);
00360 }
00361 }
00362
00363 VRS::SO<VRS::SceneThing>
00364 CarSimple::sceneThing()
00365 {
00366 return m_chassis->sceneThing();
00367 }
00368
00369 void
00370 CarSimple::attachCamera(VRS::SO<VRS::Camera> p_camera)
00371 {
00372
00373 }
00374
00375 void
00376 CarSimple::enableAllBodies()
00377 {
00378 m_chassis->setEnabled(true);
00379
00380 m_wheelFL->setEnabled(true);
00381 m_wheelFR->setEnabled(true);
00382 m_wheelRL->setEnabled(true);
00383 m_wheelRR->setEnabled(true);
00384
00385 m_suspensionFL->setEnabled(true);
00386 m_suspensionFR->setEnabled(true);
00387 m_suspensionRL->setEnabled(true);
00388 m_suspensionRR->setEnabled(true);
00389
00390 m_steerFL->setEnabled(true);
00391 m_steerFR->setEnabled(true);
00392 }
00393
00394 void
00395 CarSimple::setPosition(VRS::Vector p_position)
00396 {
00397
00398 m_chassis->setPosition(m_chassisOffset + p_position);
00399 m_chassis->setLinearVelocity(VRS::Vector(0,0,0));
00400 m_chassis->setAngularVelocity(VRS::Vector(0,0,0));
00401
00402
00403 m_wheelFL->setPosition(m_wheelOffsetFL + p_position);
00404 m_wheelFR->setPosition(m_wheelOffsetFR + p_position);
00405 m_wheelRL->setPosition(m_wheelOffsetRL + p_position);
00406 m_wheelRR->setPosition(m_wheelOffsetRR + p_position);
00407 m_wheelFL->setLinearVelocity(VRS::Vector(0,0,0));
00408 m_wheelFR->setLinearVelocity(VRS::Vector(0,0,0));
00409 m_wheelRL->setLinearVelocity(VRS::Vector(0,0,0));
00410 m_wheelRR->setLinearVelocity(VRS::Vector(0,0,0));
00411 m_wheelFL->setAngularVelocity(VRS::Vector(0,0,0));
00412 m_wheelFR->setAngularVelocity(VRS::Vector(0,0,0));
00413 m_wheelRL->setAngularVelocity(VRS::Vector(0,0,0));
00414 m_wheelRR->setAngularVelocity(VRS::Vector(0,0,0));
00415
00416
00417 m_steerFL->setPosition(m_steerOffsetFL + p_position);
00418 m_steerFR->setPosition(m_steerOffsetFR + p_position);
00419 m_steerFL->setLinearVelocity(VRS::Vector(0,0,0));
00420 m_steerFR->setLinearVelocity(VRS::Vector(0,0,0));
00421 m_steerFL->setAngularVelocity(VRS::Vector(0,0,0));
00422 m_steerFR->setAngularVelocity(VRS::Vector(0,0,0));
00423
00424
00425 m_suspensionFL->setPosition(m_suspensionOffsetFL + p_position);
00426 m_suspensionFR->setPosition(m_suspensionOffsetFR + p_position);
00427 m_suspensionRL->setPosition(m_suspensionOffsetRL + p_position);
00428 m_suspensionRR->setPosition(m_suspensionOffsetRR + p_position);
00429 m_suspensionFL->setLinearVelocity(VRS::Vector(0,0,0));
00430 m_suspensionFR->setLinearVelocity(VRS::Vector(0,0,0));
00431 m_suspensionRL->setLinearVelocity(VRS::Vector(0,0,0));
00432 m_suspensionRR->setLinearVelocity(VRS::Vector(0,0,0));
00433 m_suspensionFL->setAngularVelocity(VRS::Vector(0,0,0));
00434 m_suspensionFR->setAngularVelocity(VRS::Vector(0,0,0));
00435 m_suspensionRL->setAngularVelocity(VRS::Vector(0,0,0));
00436 m_suspensionRR->setAngularVelocity(VRS::Vector(0,0,0));
00437
00438 enableAllBodies();
00439 }
00440
00441 void
00442 CarSimple::setPosition(double p_x, double p_y, double p_z)
00443 {
00444 setPosition(VRS::Vector(p_x, p_y, p_z));
00445 }
00446
00447 VRS::Vector
00448 CarSimple::position()
00449 {
00450
00451 return m_chassis->translationNode()->getTranslate();
00452 }
00453
00454 }