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
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);
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
00106
00107 }
00108 }
00109
00110 for( unsigned k = 0; k < 2; k++ )
00111 {
00112 dJointSetHinge2Param( m_Wheel[k], dParamLoStop, m_fSteer );
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
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159