Game Development Community

Keyboard vehicle control

by J.F Desmaison · in Hardware Issues · 03/31/2007 (11:24 pm) · 1 replies

Ok, I'm very new to TGE and as many wanted to have a keyboard control for my car that centers wheels when no key is pressed. Here is what I did.

In vehicle.cc,

- VehicleData::VehicleData() section, added this:
steeringSpeed = 0.5; //default value

- void VehicleData::packData(BitStream* stream) section, added this:
stream->write(steeringSpeed);

- void VehicleData::unpackData(BitStream* stream) section, added this:
stream->read(&steeringSpeed);

- void VehicleData::initPersistFields() section, added this:
addField("steeringSpeed", TypeF32, Offset(steeringSpeed, VehicleData));

- void Vehicle::updateMove(const Move* move) section, changed //Steering sub-section from what it was to:
if (move != &NullMove)   
  {	   
	if(move->yaw)
		{ 	    
		F32 y = move->yaw;		
		mSteering.x = mClampF(mSteering.x + y,-mDataBlock->maxSteeringAngle, 
							  mDataBlock->maxSteeringAngle);		
		F32 p = move->pitch;		
		mSteering.y = mClampF(mSteering.y + p,-mDataBlock->maxSteeringAngle,
		                      mDataBlock->maxSteeringAngle);		
		}		
		else		
		{		
			if (mSteering.x > 0)
			{
				mSteering.x -= (mSteering.x > mDataBlock->steeringSpeed) ? mDataBlock->steeringSpeed : mSteering.x;
		    }
		    else if (mSteering.x < 0)
			{
				mSteering.x += (mSteering.x < -mDataBlock->steeringSpeed) ? mDataBlock->steeringSpeed : mSteering.x;
			}
			else
			{
				mSteering.x = 0;
			}
			mSteering.y = 0;   		 
		}	   
	}   else   
  {      
    mSteering.x = 0;      
	mSteering.y = 0;         
  }
(Notice, I didn't really care about pitch which is kinda logical for a car).

In vehicle.h,

- struct VehicleData: public ShapeBaseData section added this:
F32 steeringSpeed;
--------------------------------------------------------------------------------
well, that's ok, in car.cs, datablock WheeledVehicleData(DefaultCar) section I just had to add a line like that: steeringSpeed = 0.1; and my wheels return gently to center when I dont press left or right keyboard buttons. But if I understand well how the Torque thing is working, this is not ok, as:
1. I would like this steeringSpeed value to be the one defined by: $pref::Input::KeyboardTurnSpeed variable.
2. as far as I understand, putting something in car.cs will make all clients to use this value, which is not what I want.
So my question is: is the way I used totally wrong and I'd better throw this code by the window and how to pass to C++ code a $pref variable instead of a datablock variable ???...

#1
04/15/2007 (11:15 pm)
Sorry J.F. I'm not a coder, but from your description it sounds like it might be worth your while to check out this resource:

www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=6305

which causes steering to re-center when not in use.