Latest  | Search | Go
Edit this page   |   Attach file 

  Home | Tutorials | Technical Reference | Runtime | API Documentation | ContentCreation > CreatingWeapons > CreatingVehicles  

Creating new Vehicles via script

Author:David Sleeper

An Explanation of the Basic Reality Engine Vehicle

The vehicle is a extentension of a destructable network actor. The new members of this class adding functionality for seats, models, sounds, etc. The vehicle class is broad enough to cover your single/multi seated vehicles needs. The vehicle class can then be extended to your final vehicle. This coupled with a tokamak rigid body object allows for a multi-seating, realtime, complex, and user driven vehicle.

Using Input to Control a Rigid Body Vehicle

We know we want 2 things. A rigid body and it to be controlled by us. Therefore, we need to have an extension of the TokamakRigidBody (this being a member of our original vehicle) and pulling in our keyboard input on each tick for the vehicle (seeing as it is a network actor afterall).


(Ex. of a generic vehicle class with your basic updates)

public class MyVehicle : Vehicle
{
   public class MyVehicleRigidBody : TokamakRigidBody   
   {
      // Inputs

      public RigidBodyController controller;

      public void UpdateInput( /*input in*/)
      {
         // Game Inputs Stored for Callback
      }

      public void Create()
      {
         // rigidBody is a member of TokamakRigidBody

         //initiate rigid body and link up the controller callback
         rigidBody = MPhysicsEngine.TokSimulator.CreateRigidBody();
         rigidBody.SetSleepingParameter(0.01f);
         rigidBody.SetParent(parent);

         controller = new RigidBodyController(rigidBody, 1);
         controller.OnControllerCallback += new ControllerEvent(controller_OnControllerCallback);
      }
      
      void controller_OnControllerCallback(RigidBodyController Controller)
      {
         // Use Inputs Recieved from Game Update Input And Create Some New Forces
         // Corresponding To These New Inputs
         Controller.SetControllerForce(TranslatedForce);
         Controller.SetControllerTorque(TranslatedTorque);
      }
   }

   public MyVehicleRigidBody myVehicleRigidBody;

   public override void Tick()
   {
      Location = myVehicleRigidBody.Location;
      Rotation = myVehicleRigidBody.Rotation;

      myVehicleRigidBody.UpdateInput( /* inputs in */ );
   }
} 

This is important now. You must take note that the vehicle is updated through the physics system. It does not running on the same "time" as the game. It is ticked at a different rate. This is due required for the stepping of a physics engine. The engine must stay going at a stable rate regardless of changes in FPS. Therefore, you simply update the input, store the values and the controller will recieve them when required. Make sure however not to use delta time inside the callback as it will not be the correct delta time.

Setting up the Vehicle Rigid Body

This is pretty much like any other rigid body in reality engine. You setup the bounding geometry, inertial tensors, and the various other members. The main difference is the controller callbacks which is discussed above in "An Explanation of the Basic Reality Engine Vehicle". Once the callback is in place is it basically adding forces to move around a box, sphere, capsule, or combinations or any combination of the three.

Using Sensors with Our Vehicle

Sensors are very basic but require you to pay attention to a few key issues. A sensor has 2 components a direction and a positions. However, the direction is not a normalized vector, in fact it creates a segment. A position is one endpoint with the segment ending at position + direction. So when adding a sensor remeber this! This will be a problem when returning sensor depth if you use a normalized vector. Another important note is once a sensor is added the physics system must know about it by you update the rigid body bounding information.

(Ex. Wrapping the points all together and using the sensors).


////////////////////////////////////////
// member of the MyVehicleRigidBody 
////////////////////////////////////////

Sensor[] sn = new Sensor[1];

////////////////////////////////////////
// In the rigid body create function
////////////////////////////////////////

MVector snPos = new MVector();
MVector snDir = new MVector();

snDir.Set(0.0f, -1.0f, 0.0f);
snPos.Set(1.0f, 0.0f, 0.0f);

sn[0] = rigidBody.AddSensor();
sn[0].SetLineSensor(snPos, snDir);

snPos.Set(-1.0f, 0.0f, 0.0f);
sn[1] = rigidBody.AddSensor();
sn[0].SetLineSensor(snPos, snDir);

rigidBody.UpdateBoundingInfo();

////////////////////////////////////////
// Inside your Controller Callback
////////////////////////////////////////

rigidBody.BeginIterateSensor();
sn = rigidBody.GetNextSensor();
// Iterates through sensors
while (sn != null)
{
float depth = sn.DetectDepth;

// Use depth to alter forces on vehicle

sn = rigidBody.GetNextSensor();
}


Vehicle's Seat Class

Each vehicle needs with itself a set of corresponding seats. The seats will control the input and camera recieved from the player. The seat can also hold important information as to which animation is played to enter the seat, how the players leaves the seat, etc. The seat also has a link back to pawn (a subclass of player) to update if the player needs to recieve any animation updates or various message that may need to be passed on.

Here is a list of the common virtual functions to give a basic functional view of the vehicle.

public virtual bool BoardPawn(Pawn pawn)
public virtual bool UnBoardPawn(MVector ExitLocation)
public virtual void Tick()
public virtual void UpdateCamera()
public virtual void Server_HandleNetworkKeyInput(bool isDown, int NetworkKeyHandle)
public virtual void Server_HandleNetworkMouseUpdate(float mouseYaw, float mousePitch)

This should give you an idea of what the extended class will deal with. The input is recieved the Using these main function one can setup a dynamic and robust seating arrangement.

Brief Discussion of "Hovercraft" Physics

The first attempt at setting up a vehicle might be best to start with the most basic vehicle. A hovercraft (for our game purposes) really on needs to deal with a few things.

1. Floating force
2. Some way to turn
4. A way to stay stabilized
3. Acceleration (forwards or backwards)

Floating force
Lets start off with looking at what will make this thing float. By adding some line sensors (2-3 should be good enough depending on how many thrusters) we will be able to detect how close it is to contacting the ground. Using the sensors we then can apply a force based on the distance to the ground. Since the sensors are segments and most hovercraft can leave the ground for breif periods of time we will only worry about when the line sensor segment is penetrating the ground. The most simple force would be a basic spring force for pushing against the ground (just a linear equation depending on the ground penetration).

ForceUp = Depth * ThrusterPower * GroundNormal; // a force pushing away from the ground.

This can be then combined with a dampening force on the spring once the vehicle actually start to move upward to give a pretty convincing hovercraft feel.

Some way to turn
This is second easiest part of the hovercraft. You can simply use keyboard input (which would be the easiest by far) or have a mouse driven turning. The keyboard is straightfoward in which you take the keyboard input and add a torque in the controller around the vehicles up direction. The mouse is similiar but I prefer to use a approach to have a great torque the further the mouse is away from its overall ending direction. Then once the mouse begins to go in the right direction the angular turning velocity can be dampened a bit to prevent oversteering.

A way to stay stabilized
This is definitely the hardest and most annoying part of a real physics hovercraft. The best approach in my opinion is very similiar to the spring forces on bottom. If you are going in the right direction dampen your force, if you are going in the wrong direction add more force. This can also be broken down. You will have a x rotation and a z rotation in which your hovercraft is offset from its correct angles (due to grenading or colliding with an objects). Then you may even want to use a basic linear equation using the angle of which it is off by to rotate it back dampening the whole time(whatever works, works). It sounds not to difficult but it is mostly in the tweaking and fine turning of the overall feel. Also this is a good place to make sure you cap some forces.

Acceleration (forwards or backwards)
Ok this is the easiest part of the hovercraft. Just give it a force going in the direction of the vehicle depending on your acceleration or reverse button. Simple as that.

CreatingVehicles   Edit | Attach | Ref-By | Printable | Diffs | r1.9 | > | r1.8 | > | r1.7 | More