Game Development Community

Wheeled vehicle code change help please

by Ron Nelson · in Torque Game Engine · 04/04/2008 (5:49 pm) · 0 replies

For my game I need to move the tire emitter to the wheel->tire datablock. I have been pretty successful in almost everything in the code except this crashes it every time. I know this because it is where the debugger went to. For some reason it is having issues with me using an array. I really want to use one for my material system.

Hold someone have a look at this and tell me if there is someway to get it to work?

bool WheeledVehicle::onNewDataBlock(GameBaseData* dptr)
{
   // Delete any existing wheel resources if we're switching
   // datablocks.
   if (mDataBlock) 
   {
      Wheel* wend = &mWheel[mDataBlock->wheelCount];
      for (Wheel* wheel = mWheel; wheel < wend; wheel++) 
      {
		  for (int a=0; a<MAX_MAT_FX; a++)
		  {
			  if (wheel->emitterDust[a])
			  {
				  wheel->emitterDust[a]->deleteWhenEmpty();
				  wheel->emitterDust[a] = 0;
			  }

			  if (wheel->emitterSkid[a])
			  {
				  wheel->emitterSkid[a]->deleteWhenEmpty();
				  wheel->emitterSkid[a] = 0;
			  }
		  }
         delete wheel->shapeInstance;
         wheel->shapeInstance = 0;
      }
   }

   // Load up the new datablock
   mDataBlock = dynamic_cast<WheeledVehicleData*>(dptr);
   if (!mDataBlock || !Parent::onNewDataBlock(dptr))
      return false;

   F32 frontStatic = 0;
   F32 backStatic = 0;
   F32 fCount = 0;
   F32 bCount = 0;

   // Set inertial tensor, default for the vehicle is sphere
   if (mDataBlock->massBox.x > 0 && mDataBlock->massBox.y > 0 && mDataBlock->massBox.z > 0)
      mRigid.setObjectInertia(mDataBlock->massBox);
   else
      mRigid.setObjectInertia(mObjBox.max - mObjBox.min);

   // Initialize the wheels...
   for (S32 i = 0; i < mDataBlock->wheelCount; i++) 
   {
      Wheel* wheel = &mWheel[i];
      wheel->data = &mDataBlock->wheel[i];
      wheel->tire = 0;
      wheel->spring = 0;

      wheel->surface.contact = false;
      wheel->surface.object  = NULL;
      wheel->avel = 0;
      wheel->apos = 0;
      wheel->extension = 1;
      wheel->slip = 0;

      wheel->springThread = 0;

      // Steering on the front tires by default
      if (wheel->data->pos.y > 0)
         wheel->steering = 1;

      // Build wheel animation threads
      if (wheel->data->springSequence != -1) {
         wheel->springThread = mShapeInstance->addThread();
         mShapeInstance->setSequence(wheel->springThread,wheel->data->springSequence,0);
      }

      // Each wheel get's it's own particle emitter
	  for (int a=0; a<MAX_MAT_FX; a++)
	  {
		  if (wheel->tire->dustEmitter[a] != NULL) 
		  {
			  wheel->emitterDust[a] = new ParticleEmitter;
			  wheel->emitterDust[a]->onNewDataBlock(wheel->tire->dustEmitter[a]);
			  wheel->emitterDust[a]->registerObject();
		  }
	  }

	  for (int e=0; e<MAX_MAT_FX; e++)
	  {
		  if (wheel->tire->skidEmitter[e] != NULL) 
		  {
			  wheel->emitterSkid[e] = new ParticleEmitter;
			  wheel->emitterSkid[e]->onNewDataBlock(wheel->tire->skidEmitter[e]);
			  wheel->emitterSkid[e]->registerObject();
		  }
	  }
   }

The applicable header data is that in struct WheeledVehicleTire: public SimDataBlock I added
ParticleEmitterData* dustEmitter[MAX_MAT_FX];
   S32 dustEmitterID[MAX_MAT_FX];
   ParticleEmitterData* skidEmitter[MAX_MAT_FX];
   S32 skidEmitterID[MAX_MAT_FX];
   DecalData *decalData[MAX_MAT_FX];
   S32 decalID[MAX_MAT_FX];

and in struct Wheel in class WheeledVehicle: public Vehicle I added
SimObjectPtr<ParticleEmitter> emitterDust[MAX_MAT_FX];
	  SimObjectPtr<ParticleEmitter> emitterSkid[MAX_MAT_FX];

As for the rest of the code I am positive the way I initialized it icorrect because I already used this method for the rigidshape class and it works great. It seems to be getting upset of the use of the wheels and tires.

Any help that can be provided would be greatly appreciated. Thanks in advance.