Code:
groundVehicleLogic()
{
self endon( "death" );
self endon( "destroyed" );
speedTreshold = 3;
for( speedAdd = 0 ; isDefined( self ) ; speedAdd = 0 )
{
wait( 0.1 );
self.anglesF = anglesToForward( self.angles );
if( !isDefined( self.driver ) || self.driver.sessionstate != "playing" )
{
if( self.speed > speedTreshold )
self.speed -= 1;
}
else
{
self.forward = self.driver getPlayerAngles();
if( self.driver meleeButtonPressed() )
{
if( self.driver useButtonPressed() )
{
speedAdd -= 2;
}
else
{
speedAdd += 2;
}
}
else
{
if( self.speed != 0 )
speedAdd += ( -1 * self.gear );
}
}
self vehGiveSpeed( speedAdd ); //self.speed += speed; clamp( speed , minspeed , maxspeed ); if speed < 0 self.gear = -1 < backwards else forwards gear = 1
if( self.speed * self.gear > speedTreshold )
{
speedVec = vectorScale( self.anglesF , self.speed );
traceStart = self.origin + ( 0 , 0 , 40 );
traceEnd = traceStart + speedVec;
trace = bulletTrace( traceStart , traceEnd , true , self );
if( !self checkCollission( trace ) )//check if collision = player or world geometry
{
nextPos = self.origin + speedVec;
trace = bulletTrace( nextPos + ( 0 , 0 , level.groundVehicle_climbhigh ) , ( nextPos[0] , nextPos[1] , self.origin[2] - 256 ) , false , self );//climbhigh, max high of travelling
if( !self checkCollission( trace ) )
nextPos = trace["position"];
}
else //collide, inverse speed
{
nextPos = self.origin;
self.speed = int( self.speed * -0.5 );
}
self moveTo( nextPos , 0.1 , 0 , 0 );
self.yaw = self.forward[1];
self rotateTo( ( self.pitch , self.yaw , self.roll ) , 0.2 , 0 , 0 );
}
}
}
ingame:
http://www.xfire.com/videos/5bda89
if you want to align your vehicle to the ground:
Code:
if( self.rotateVehicle )//no need to rotate every cycle...
{
self.rotateVehicle = false;
if( nextPos != self.origin )
{
self.pitch = vectorToAngles( nextPos - self.origin );
self.pitch = self.pitch[0];
}
else
{
self.pitch = 0;
}
s = vectorScale( self.anglesR , 100 );
l = bulletTrace( self.origin - s + ( 0 , 0 , 64 ) , self.origin - s - ( 0 , 0 , 64 ) , false , self ); //check left
r = bulletTrace( self.origin + s + ( 0 , 0 , 64 ) , self.origin + s - ( 0 , 0 , 64 ) , false , self ); //check right
roll = vectorToAngles( r["position"] - l["position"] );
roll = roll[0];
hDiff = angleDiff( self.angles[1] , self.direction[1] );
hDir = turnDir( hDiff );//left, rght >> -1 , 1
hDiff *= hDir;
self.pitch = self.pitch * self.gear;
self.yaw = self.angles[1] + ( min( hDiff , self.maxYawRate ) * hDir ); //max yaw add per cycle
self.roll = roll;
self rotateTo( ( self.pitch , self.yaw , self.roll ) , 0.2 , 0 , 0 );
}
this is the most light-weighted vehicle code which i have done, i thinks its a good way, but since it uses not this many bullettraces (1-2 for collission, 2 for rotation every 2 cycles = app. 2,25 bullettraces every 0.1 secs)
for comparison:
the un vehicle mod uses like 5 for collision, 4 for every tire rotation, and again more than 5 for player detection per vehicle = app. 15 traces every 0.1 secs