Game Development Community

Vehicle Upside down?

by Ronald J Nelson · in Torque Game Engine · 07/18/2007 (11:49 pm) · 5 replies

I want to be able to allow a player to flip his vehicle right side up through an impulse. However, how can I tell if it is upside down?

#1
07/19/2007 (12:26 am)
You could obtain this by checking the transform of the vehicle.

You don't actually need to check if the car is upside down though, just do something like this:
function serverCmdflipCar(%client)
{
   %car = %client.getControlObject();

   if (%car.getClassName() $= "WheeledVehicle")
   {
      %carPos = %car.getPosition();
      %carPos = VectorAdd(%carPos, "0 0 3");

      %car.setTransform(%carPos SPC "0 0 1 0");
   }
}

moveMap.bindCmd(keyboard, "ctrl e", "commandToServer(\'flipCar\');", "");
If the car is upside down when the command is called, you will be flipped right side up. If the car is already right side up and the command is called nothing bad happens, you will just be gently dropped to the ground from a small height.
#2
07/19/2007 (12:42 am)
Well I would also like an upside down check to apply damage to the vehicle, but thanks a ton for this Tim.
#3
07/19/2007 (9:23 am)
@Ron - We added this feature to the C++ side. We check the vehicle up vector against world up using a dot product. When then call an onFlipped() and onRighted() script callback to do whatever "effects" we want.
#4
07/19/2007 (4:45 pm)
Tom I would really be interested in seeing how you did that.
#5
07/19/2007 (5:56 pm)
I added the following at the end of Vehicle::processTick()...

// Detect vehicle flip over.
   if ( isServerObject() )
   {
      // We dot product with the up vector and 0,0,1
      // to tell if the vehicle is flipped.
      VectorF up;
      getWorldTransform().getColumn( 2, &up );
      bool flipped = up.z < 0.0f;
 
      // calculate if the vehicle is flipped this tick, 
      // if there is a change, call the script function onFlipped   
      if ( mIsFlipped != flipped )
      {
         mIsFlipped = flipped;
         Con::executef( this, 3, "onFlipped", flipped ? "1" : "0" );
      }
   }

You'll need to also add mIsFlipped to the vehicle class and initialize it properly.