Game Development Community

Bad collision when switch collision mesh for vehicle at runtime

by Ji Xinyu · in Torque 3D Beginner · 08/02/2013 (8:25 pm) · 1 replies

I want to implement a functionality for vehicle that switch collision mesh for vehicle at runtime.So I made a car model with two dummy objects named "collision-1" and "collision-2",two collision meshs named "col-1" and "col-2".I want to switch the collision mesh between "col-1" and "col-2" at runtime.
After I added some code to the engine,I tested the code using wheeledvehicle for the car.First I add a car into gameworld, second switched the collision mesh from "col-1" to "col-2", then I drived the car collide with the "station01" model many times.I am sure that the collision mesh had been switched to "col-2" from "col-1".But there is a problem when I test collision for "col-2".That is most collisions between my car and station01 model are normal,but some times after the collision I can not move the car,I can not drive the car backwards.why?
who can give me some suggestions?thanks very much.My code as follow:(I added the code marked by "//switch collision mesh" and I test the code in TGEA1.8.2)

//in vehicle.h
class Vehicle: public ShapeBase
{
   ......
   enum MaskBits {
      PositionMask = Parent::NextFreeMask << 0,
      EnergyMask   = Parent::NextFreeMask << 1,
      //switch collision mesh<<
      ColMeshMask  = Parent::NextFreeMask << 2,
      NextFreeMask = Parent::NextFreeMask << 3
      //switch collision mesh>>
   };
   ......
   ShapeBaseConvex mConvex;
   //switch collision mesh<<
   S32 mActiveColNum;               //mark the current collision mesh,will be Assigned to mConvex.hullId
   //switch collision mesh>>
   int restCount;
   ......
   void enableCollision();

   //switch collision mesh<<
   void setActiveCollision(S32 activeColNum);  
   //switch collision mesh>>

   /// Returns the velocity of the vehicle
   Point3F getVelocity() const;

};

//in vehicle.cpp

Vehicle::Vehicle()
{
   ......
   mWakeSound = NULL;

   //switch collision mesh<<
   mActiveColNum = 0;
   //switch collision mesh>>
}

void Vehicle::processTick(const Move* move)
{
   ......
      updateContainer();
   }

   //switch collision mesh<<
   if(mConvex.hullId != mActiveColNum)
   {
      mConvex.hullId = mActiveColNum;
      mConvex.transform = 0;
      mConvex.findNodeTransform();
      setMaskBits( ColMeshMask );
   }
   //switch collision mesh>>
}

U32 Vehicle::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
{
   U32 retMask = Parent::packUpdate(con, mask, stream);

   stream->writeFlag(mJetting);

   //switch collision mesh<<
   if ( stream->writeFlag( mask & ColMeshMask ) )  
      stream->write(mActiveColNum);  
   //switch collision mesh>>
   ......
}

void Vehicle::unpackUpdate(NetConnection *con, BitStream *stream)
{
   Parent::unpackUpdate(con,stream);

   mJetting = stream->readFlag();

    //switch collision mesh<<
   if ( stream->readFlag() ) // ColMeshMask   
      stream->read(&mActiveColNum); 
   //switch collision mesh>>
   ......
}

//switch collision mesh <<
void Vehicle::setActiveCollision(S32 activeColNum)
{
   if((activeColNum>=0)&&(activeColNum <= mDataBlock->collisionDetails.size()))
	mActiveColNum = actveColNum;
}

ConsoleMethod(Vehicle, setActiveCollision, void, 3, 3, "obj.setActiveCollision(activeCollisionNum)")
{
   S32 actColNum = dAtoi(argv[2]);
   object->setActiveCollision(actColNum);
}
//switch collision mesh >>
when switch the collision mesh from "col-1"(default collison mesh) to "col-2" for vehicle,use the function "car.setActvieCollision(1)"on server side. Pass value 0 to this function means select "col-1" and value 1 means select "col-2".

#1
08/02/2013 (8:58 pm)
I notice you're using a single-vector collisionDetails. What do your buildPolyList, buildConvex, support, getPolyList, and getFeatures methods look like? Half the code in that WIP thrown out for peer-review presumes we're working with a set of vectors, and tracking the one we care about via that mActiveCollisionset, not a single one anymore, so need to know what other differences are there.