RaceCar.cpp

Go to the documentation of this file.
00001 #include "RaceCar.h"
00002 
00003 REGISTER_ODE_OBJECT_CLASS( RaceCar )
00004 
00005 RaceCar::~RaceCar()
00006 {
00007 }
00008 
00009 void RaceCar::Init()
00010 {
00011     MercuryODEObjectLoadable::Init();
00012 }
00013 
00014 bool RaceCar::LoadFromINI( MercuryINI & pINI, const MString & sShapeName, const dWorldID &oWorld )
00015 {
00016     if ( !MercuryODEObjectLoadable::LoadFromINI( pINI, sShapeName, oWorld ) )
00017         return false;
00018 
00019     for( unsigned i = 0; i < 4; i++ )
00020     {
00021         MString sName = GetName() + ssprintf( "Wheel%d", i );
00022         MercuryODEObjectLoadable * c = (*ToMakeODEs)["MercuryODEObjectLoadable"]( sName );
00023 
00024         c->m_oSpace = m_oSpace;
00025         c->m_bBodied = true;
00026 
00027         switch( i )
00028         {
00029         case 0: c->m_pPrePosition = MercuryPoint( 2.5, 4, 0 ); break;
00030         case 1: c->m_pPrePosition = MercuryPoint( -2.5, 4, 0 ); break;
00031         case 2: c->m_pPrePosition = MercuryPoint( 2.5, -4, 0 ); break;
00032         case 3: c->m_pPrePosition = MercuryPoint( -2.5, -4, 0 ); break;
00033         }
00034 
00035         c->m_pPrePosition += m_pPrePosition;
00036 
00037         c->m_oWorldSpace = m_oSpace;
00038         c->SetName( sName );
00039         c->Init();
00040 
00041         c->LoadFromINI( pINI, "RCWheel", oWorld );
00042         
00043         m_pWorld->AddObject( c, true );
00044         oWheels[i] = c;
00045     }
00046 
00047     for( unsigned j = 0; j < 4; j++ )
00048     {
00049         dBodyEnable( oWheels[j]->m_oBody );
00050         
00051         MQuaternion qThis = (float*)dBodyGetRotation( m_oBody );
00052         MercuryPoint pThis = dBodyGetPosition( m_oBody );
00053 
00054         //Rotated axis of rotation (for all wheels, tanget to the car)
00055         MercuryPoint p = (qThis * MercuryPoint(1,0,0) * qThis.conjugate()).ToPoint();
00056 
00057         m_Wheel[j] = dJointCreateHinge2( m_pWorld->GetWorldID(), 0 );
00058         dJointAttach( m_Wheel[j], m_oBody, oWheels[j]->m_oBody );
00059         const dReal * rearWheelPos = dBodyGetPosition( oWheels[j]->m_oBody );
00060         dJointSetHingeAnchor( m_Wheel[j], rearWheelPos[0], rearWheelPos[1], rearWheelPos[2] );
00061         dJointSetHinge2Axis1( m_Wheel[j], 0, 0, 1 );
00062         dJointSetHinge2Axis2( m_Wheel[j], 1, 0, 0 );
00063         dJointSetHinge2Param (m_Wheel[j],dParamFudgeFactor,1.1);
00064     
00065         if( j < 2 )
00066         {
00067             dJointSetHinge2Param (m_Wheel[j],dParamLoStop,0);   //Steering
00068             dJointSetHinge2Param (m_Wheel[j],dParamHiStop,0);
00069         } else {
00070             dJointSetHinge2Param (m_Wheel[j],dParamLoStop,0);
00071             dJointSetHinge2Param (m_Wheel[j],dParamHiStop,0);
00072         }
00073     }
00074 
00075     return true;
00076 }
00077 
00078 void RaceCar::PreCycleUpdate( const float dTime )
00079 {
00080     for( unsigned i = 0; i < 4; i++ )
00081     {
00082         dBodyEnable( oWheels[i]->m_oBody );
00083     }
00084 
00085     if( m_bBreaking )
00086     {
00087         dBodySetAngularVel( oWheels[0]->m_oBody, 0, 0, 0 );
00088         dBodySetAngularVel( oWheels[1]->m_oBody, 0, 0, 0 );
00089         dBodySetAngularVel( oWheels[2]->m_oBody, 0, 0, 0 );
00090         dBodySetAngularVel( oWheels[3]->m_oBody, 0, 0, 0 );
00091     }
00092     else
00093     {
00094         float fTotalDisjointSpeed = 
00095             dJointGetHinge2Angle2Rate( m_Wheel[0] ) + dJointGetHinge2Angle2Rate( m_Wheel[2] ) +
00096             dJointGetHinge2Angle2Rate( m_Wheel[1] ) + dJointGetHinge2Angle2Rate( m_Wheel[3] );
00097 
00098 
00099         if( fTotalDisjointSpeed < m_fSpeed )
00100         {
00101             float fTorque = m_fSpeed - fTotalDisjointSpeed;
00102             fTorque *= 1/dTime;
00103             dJointAddHinge2Torques (m_Wheel[3], 0,fTorque);
00104             dJointAddHinge2Torques (m_Wheel[2], 0,fTorque);
00105 //          dJointAddHinge2Torques (m_Wheel[1], 0,fTorque);
00106 //          dJointAddHinge2Torques (m_Wheel[0], 0,fTorque);
00107         }
00108     }
00109 
00110     for( unsigned k = 0; k < 2; k++ )
00111     {
00112         dJointSetHinge2Param( m_Wheel[k], dParamLoStop, m_fSteer ); //Steering
00113         dJointSetHinge2Param( m_Wheel[k], dParamHiStop, m_fSteer );
00114     }
00115 
00116     MercuryODEObjectLoadable::PreCycleUpdate( dTime );
00117 }
00118 
00119 bool RaceCar::Collide( MercuryODEObject * pHit, dContact &pContact, MercuryODEWorld * pWorld )
00120 {
00121     if( pHit == this )
00122         return false;
00123     return true;
00124 }
00125 
00126 void RaceCar::Message( int Message, PStack & data, const MString & name )
00127 {
00128     MercuryODEObjectLoadable::Message( Message, data, name );
00129 }
00130 
00131 
00132 
00133 /* 
00134  * Copyright (c) 2007, Charles Lohr
00135  * All rights reserved.
00136  *
00137  * Redistribution and use in source and binary forms, with or
00138  * without modification, are permitted provided that the following
00139  * conditions are met:
00140  *  -   Redistributions of source code must retain the above
00141  *      copyright notice, this list of conditions and the following disclaimer.
00142  *  -   Redistributions in binary form must reproduce the above copyright
00143  *      notice, this list of conditions and the following disclaimer in
00144  *      the documentation and/or other materials provided with the distribution.
00145  *  -   Neither the name of the Mercury Engine nor the names of its
00146  *      contributors may be used to endorse or promote products derived from
00147  *      this software without specific prior written permission.
00148  *
00149  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00150  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00151  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00152  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00153  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00154  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00155  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00156  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00157  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00158  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00159  */

Hosted by SourceForge.net Logo