Post by MonstaWolf on Jul 4, 2015 21:57:21 GMT
I've gotten enough traffic cars in my game that it's starting to lag. They each use 5 objects each, as they are scripted. What would be the most effective way to disable the script until needed?
Note: When the script is activated, the traffic car will only spawn at one point, meaning if the car deactivated far from it's spawn point, when it re-activates it will spawn back at the one point. I can't make it have 2 possible spawn points, because then it may spawn at the same point of another car on the same path.
I'm thinking of combining the traffic scripts into one, but that will take a lot of time, and I'm not sure it will cut down on the lag, but I know it will shorten the object list.
Here is are 2 script samples from 2 different traffic cars going on the same path, sorry they're so long. Let me know if you need any more info. (The only difference is the spawnpoint and the positions of the wheels.)
1.
2.
Note: When the script is activated, the traffic car will only spawn at one point, meaning if the car deactivated far from it's spawn point, when it re-activates it will spawn back at the one point. I can't make it have 2 possible spawn points, because then it may spawn at the same point of another car on the same path.
I'm thinking of combining the traffic scripts into one, but that will take a lot of time, and I'm not sure it will cut down on the lag, but I know it will shorten the object list.
Here is are 2 script samples from 2 different traffic cars going on the same path, sorry they're so long. Let me know if you need any more info. (The only difference is the spawnpoint and the positions of the wheels.)
1.
// Simple rigid body AI car (requires Jestermon's RadMath2.dll)
/// Object handles:
int SM_WHEEL = OBJ_0; // wheel mesh
int SM_BODY= OBJ_22; // body mesh
int BODY = OBJ_44; // location of the rear axle
int CARBODY= OBJ_66; // car body collision model
///
int direction = 0;
float Velocity;
float throttle=1;
float steering; //steering angle per frame for body
float SteeringAngle; // steering angle for front wheels
float MaxAngle= 30; // max steering angle
float MaxSpeed= 0.1;
float spin= 0;
float MinimumTurnVelocity=2.8;
Quaternion BodyOrientation;
Vector3 SpawnLoc;
float SpawnTimer;
float SpawnPoint;
/// Body&wheels locations
float WheelsSideOffset=0.65;
float FrontWheelsForwardOffset=2.29;
float RearWheelsForwardOffset=-0.24;
float BodyForwardForwardOffset=1.01;
float BodySideForwardOffset=0;
float HeightOffset=0;
// pathfinding
int CurrentWayPoint;
int SpawnLocation;
Vector3[] Waypoints(13);
float tooClose= 2;
float tooFar= 3;
bool isBehind;
bool isTooClose;
Vector3 location, carLocation, targetLocation;
Quaternion qCar, qToTarget;
int MathDLLHandle=0;
////// L2P by Jestermon ////////////
void L2P(int PARENT, int OBJ, Vector3 rotation, Vector3 translation)
{ Vector3 parentLocation; Quaternion parentOrientation; Quaternion newOrientation; Quaternion objectOrientation; Vector3 forward, location, newLocation; float x,y,z,ud,lr,fb; x = rotation.x; y = rotation.y; z = rotation.z; fb = translation.z; lr = translation.x; ud = translation.y; iObjectLocation(PARENT, parentLocation); iObjectOrientation(PARENT,parentOrientation); iObjectOrientationSet(OBJ,parentOrientation); iVectorRotate(forward,Vector3(lr,ud,fb),parentOrientation); newLocation = parentLocation + forward; iObjectLocationSet(OBJ,newLocation); iQuaternionFromEulerAngles(newOrientation,x,y,z,"xyz"); iQuaternionMultiply(newOrientation,newOrientation,parentOrientation); iObjectOrientationSet(OBJ,newOrientation);}
//// L2P for Imposter/////
void ImposterChildTransform(int Parent,int ChildImposterMasterID, int ChildImposterIndex, Vector3 rotation, Vector3 translation)
{
Vector3 parentLocation;
Quaternion parentOrientation;
Quaternion newOrientation;
Quaternion objectOrientation;
Vector3 forward, location, newLocation;
float x,y,z,ud,lr,fb;
x = rotation.x; y = rotation.y; z = rotation.z;
fb = translation.z; lr = translation.x; ud = translation.y;
// get
iObjectOrientation(Parent,parentOrientation);
iObjectLocation(Parent,parentLocation);
iVectorRotate(forward,Vector3(lr,ud,fb),parentOrientation);
newLocation = parentLocation + forward;
iQuaternionFromEulerAngles(newOrientation,x,y,z,"xyz");
iQuaternionMultiply(newOrientation,newOrientation,parentOrientation);
// set
iObjectImposterSet(ChildImposterMasterID,ChildImposterIndex,newOrientation,newLocation);
}
////////// QuaternionGetLookAt ////////////
Quaternion QuatGetLookAt(Vector3 a, Vector3 b)
{
Quaternion orientation;
Vector3 up = Vector3(0,1,0);
Vector3 direction = b - a;
iQuaternionLookAt(orientation,direction,up);
return orientation;
}
////////////////////////////////////////////////////////////
void Main()
{
if(iInitializing())
{
// Waypoints:
Waypoints[10] = Vector3( -138.797,0.12,8.82438 );
Waypoints[11] = Vector3( -137.879,0.12,138.153 );
Waypoints[12] = Vector3( -138.026,0.12,258.928 );
Waypoints[0] = Vector3( -138.172,0.12,368.654 );
Waypoints[1] = Vector3( -137.694,0.12,460.448 );
Waypoints[2] = Vector3( -60.0052,0.12,460.162 );
Waypoints[3] = Vector3( -2.75631,-3.19,460.835 );
Waypoints[4] = Vector3( -3.33709,0.12,382.131 );
Waypoints[5] = Vector3( -3.45288,0.12,282.166 );
Waypoints[6] = Vector3( -3.22019,0.12,210.437 );
Waypoints[7] = Vector3( -3.45782,0.12,69.8568 );
Waypoints[8] = Vector3( -3.79037,0.12,8.82839 );
Waypoints[9] = Vector3( -81.8849,0.12,9.26258 );
// spawn car
SpawnLocation=iFloatRand(0,0);
CurrentWayPoint= SpawnLocation+1;
iObjectLocationSet(BODY, Waypoints[SpawnLocation]);
iObjectOrientationSet(BODY, QuatGetLookAt(Waypoints[SpawnLocation], Waypoints[CurrentWayPoint]));
// create imposters
iObjectImpostersCreate(SM_WHEEL,5);
iObjectImpostersCreate(SM_BODY,1);
// load Rad math
MathDLLHandle = iDLLLoad(".\\3DRad_res\\objects\\Script\\RadMath2.dll");
}
else if (iDeinitializing())
{
if (MathDLLHandle != 0) iDLLUnload(MathDLLHandle);
}
else
{
iObjectOrientation(BODY, qCar);
iObjectLocation(BODY, carLocation);
}
/// START of vehicle script /////
//iObjectImposterShadowEnable(SM_BODY,0,true); //uncomment to enable shadow caster
//iObjectImposterShadowEnable(SM_WHEEL,0,true); //uncomment to enable shadow caster
iObjectLocation(BODY,location);
float speed = IN_44;
/// Move body
// move body
L2P(BODY,BODY,Vector3(0,0,0),Vector3(0,0,Velocity));
// forward
if (throttle==1)
{
Velocity+= 0.001;
}
// backward
if (throttle==-1)
{
Velocity-=0.01;
}
// wheel friction
if (throttle==0 && Velocity>0)
{
Velocity-=0.002;
}
if (throttle==0 && Velocity<0)
{
Velocity+=0.002;
}
// top speed
if (Velocity<-MaxSpeed)
{
Velocity=-MaxSpeed;
}
if (Velocity>MaxSpeed)
{
Velocity=MaxSpeed;
}
// car direction
if (Velocity<0)
{
direction=-1;
}
if (Velocity>0)
{
direction=1;
}
/// Turning
if (iKeyDown(iKeyCode("DIK_LEFT")))
{
//steering=-1;
}
if (iKeyDown(iKeyCode("DIK_RIGHT")))
{
//steering=1;
}
if (!iKeyDown(iKeyCode("DIK_RIGHT"))&& !iKeyDown(iKeyCode("DIK_LEFT")))
{
//steering=0;
}
steering= -(relativeAngleOfTarget()/MaxAngle);
if (steering !=0 && speed > MinimumTurnVelocity)
{
L2P(BODY,BODY,Vector3(0,(steering*direction),0),Vector3(0,0,0));
}
if (steering>1)
{
steering=1;
}
if (steering<-1)
{
steering=-1;
}
// turn wheels
SteeringAngle=MaxAngle*steering;
/// Imposter Locations
// wheel spin
spin += (speed*2*direction);
//Locate and rotate rear wheels based on speed
ImposterChildTransform(BODY,SM_WHEEL, 0,Vector3(spin,0,0), Vector3(WheelsSideOffset , HeightOffset, RearWheelsForwardOffset));
ImposterChildTransform(BODY,SM_WHEEL, 1,Vector3(spin,0,0),Vector3(-WheelsSideOffset ,HeightOffset, RearWheelsForwardOffset));
//Locate and rotate front wheels based on speed & steering angle
ImposterChildTransform(BODY,SM_WHEEL, 2,Vector3(spin,SteeringAngle,0),Vector3(WheelsSideOffset, HeightOffset, FrontWheelsForwardOffset));
ImposterChildTransform(BODY,SM_WHEEL, 3,Vector3(spin,SteeringAngle,0),Vector3(-WheelsSideOffset, HeightOffset, FrontWheelsForwardOffset));
//set car body location
ImposterChildTransform(BODY,SM_BODY, 0,Vector3(0,0,0), Vector3(BodySideForwardOffset, HeightOffset, BodyForwardForwardOffset));
// set collision model location
L2P(BODY,CARBODY,Vector3(0,0,0),Vector3(BodySideForwardOffset, HeightOffset, BodyForwardForwardOffset));
/// AI
/////////////////////////////
float distance= iVectorLength(carLocation-targetLocation);
// if close to waypoint, get next waypoint
if(distance < tooClose && CurrentWayPoint != 13)
{
CurrentWayPoint ++;
}
// If close to waypoint and current waypoint is last waypoint, get first waypoint
if(distance < tooClose && CurrentWayPoint == 13)
{
CurrentWayPoint = 0;
}
// Waypoints:
Waypoints[10] = Vector3( -138.797,0.12,8.82438 );
Waypoints[11] = Vector3( -137.879,0.12,138.153 );
Waypoints[12] = Vector3( -138.026,0.12,258.928 );
Waypoints[0] = Vector3( -138.172,0.12,368.654 );
Waypoints[1] = Vector3( -137.694,0.12,460.448 );
Waypoints[2] = Vector3( -60.0052,0.12,460.162 );
Waypoints[3] = Vector3( -2.75631,-3.19,460.835 );
Waypoints[4] = Vector3( -3.33709,0.12,382.131 );
Waypoints[5] = Vector3( -3.45288,0.12,282.166 );
Waypoints[6] = Vector3( -3.22019,0.12,210.437 );
Waypoints[7] = Vector3( -3.45782,0.12,69.8568 );
Waypoints[8] = Vector3( -3.79037,0.12,8.82839 );
Waypoints[9] = Vector3( -81.8849,0.12,9.26258 );
targetLocation=Waypoints[CurrentWayPoint];
}
///// END VOID MAIN /////
// Steer to taget
float relativeAngleOfTarget()
{
Quaternion qTarget, qDiff;
Vector3 up = Vector3(0,1,0);
Vector3 direction = targetLocation-carLocation;
direction.y=0;
iVectorLengthSet(direction,direction,1);
iQuaternionLookAt(qTarget,direction,up);
iQuaternionMultiply(qDiff, qCar, qInv(qTarget)); // quat subtraction. by AllanF
Vector3 v;
iVectorRotate(v,Vector3(0,0,1),qDiff);
float a= R_atan2(v.x, v.z);
isBehind= (v.z<0);
if (isBehind)
if (v.x<0)
a=-2-a;
else
a=2-a;
return 180*a/2;
}
Quaternion qInv(Quaternion q)
{
Quaternion qi;
qi.x= -q.x; // flip xyz direction
qi.y= -q.y;
qi.z= -q.z;
qi.w= q.w; // same angle axis
return qi;
}
float R_atan2(float value1, float value2)
{
iDLLArraySet(0,value1);
iDLLArraySet(1,value2);
iDLLCall(MathDLLHandle,"R_atan2",0);
float f = iDLLArrayGet(0);
return f;
}
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12pt;"></span>
2.
// Simple rigid body AI car (requires Jestermon's RadMath2.dll)
/// Object handles:
int SM_WHEEL = OBJ_0; // wheel mesh
int SM_BODY= OBJ_22; // body mesh
int BODY = OBJ_44; // location of the rear axle
int CARBODY= OBJ_66; // car body collision model
///
int direction = 0;
float Velocity;
float throttle=1;
float steering; //steering angle per frame for body
float SteeringAngle; // steering angle for front wheels
float MaxAngle= 30; // max steering angle
float MaxSpeed= 0.1;
float spin= 0;
float MinimumTurnVelocity=2.8;
Quaternion BodyOrientation;
Vector3 SpawnLoc;
float SpawnTimer;
float SpawnPoint;
/// Body&wheels locations
float WheelsSideOffset=0.65;
float FrontWheelsForwardOffset=2.29;
float RearWheelsForwardOffset=-0.24;
float BodyForwardForwardOffset=1.01;
float BodySideForwardOffset=0;
float HeightOffset=0;
// pathfinding
int CurrentWayPoint;
int SpawnLocation;
Vector3[] Waypoints(13);
float tooClose= 2;
float tooFar= 3;
bool isBehind;
bool isTooClose;
Vector3 location, carLocation, targetLocation;
Quaternion qCar, qToTarget;
int MathDLLHandle=0;
////// L2P by Jestermon ////////////
void L2P(int PARENT, int OBJ, Vector3 rotation, Vector3 translation)
{ Vector3 parentLocation; Quaternion parentOrientation; Quaternion newOrientation; Quaternion objectOrientation; Vector3 forward, location, newLocation; float x,y,z,ud,lr,fb; x = rotation.x; y = rotation.y; z = rotation.z; fb = translation.z; lr = translation.x; ud = translation.y; iObjectLocation(PARENT, parentLocation); iObjectOrientation(PARENT,parentOrientation); iObjectOrientationSet(OBJ,parentOrientation); iVectorRotate(forward,Vector3(lr,ud,fb),parentOrientation); newLocation = parentLocation + forward; iObjectLocationSet(OBJ,newLocation); iQuaternionFromEulerAngles(newOrientation,x,y,z,"xyz"); iQuaternionMultiply(newOrientation,newOrientation,parentOrientation); iObjectOrientationSet(OBJ,newOrientation);}
//// L2P for Imposter/////
void ImposterChildTransform(int Parent,int ChildImposterMasterID, int ChildImposterIndex, Vector3 rotation, Vector3 translation)
{
Vector3 parentLocation;
Quaternion parentOrientation;
Quaternion newOrientation;
Quaternion objectOrientation;
Vector3 forward, location, newLocation;
float x,y,z,ud,lr,fb;
x = rotation.x; y = rotation.y; z = rotation.z;
fb = translation.z; lr = translation.x; ud = translation.y;
// get
iObjectOrientation(Parent,parentOrientation);
iObjectLocation(Parent,parentLocation);
iVectorRotate(forward,Vector3(lr,ud,fb),parentOrientation);
newLocation = parentLocation + forward;
iQuaternionFromEulerAngles(newOrientation,x,y,z,"xyz");
iQuaternionMultiply(newOrientation,newOrientation,parentOrientation);
// set
iObjectImposterSet(ChildImposterMasterID,ChildImposterIndex,newOrientation,newLocation);
}
////////// QuaternionGetLookAt ////////////
Quaternion QuatGetLookAt(Vector3 a, Vector3 b)
{
Quaternion orientation;
Vector3 up = Vector3(0,1,0);
Vector3 direction = b - a;
iQuaternionLookAt(orientation,direction,up);
return orientation;
}
////////////////////////////////////////////////////////////
void Main()
{
if(iInitializing())
{
// Waypoints:
Waypoints[10] = Vector3(-138.797,0.12,8.82438);
Waypoints[11] = Vector3(-137.879,0.12,138.153);
Waypoints[12] = Vector3(-138.026,0.12,258.928);
Waypoints[0] = Vector3(-138.172,0.12,368.654);
Waypoints[1] = Vector3(-137.694,0.12,460.448);
Waypoints[2] = Vector3(-60.0052,0.12,460.162);
Waypoints[3] = Vector3(-2.75631,-3.19,460.835);
Waypoints[4] = Vector3(-3.33709,0.12,382.131);
Waypoints[5] = Vector3(-3.45288,0.12,282.166);
Waypoints[6] = Vector3(-3.22019,0.12,210.437);
Waypoints[7] = Vector3(-3.45782,0.12,69.8568);
Waypoints[8] = Vector3(-3.79037,0.12,8.82839);
Waypoints[9] = Vector3(-81.8849,0.12,9.26258);
// spawn car
SpawnLocation=iFloatRand(0,0);
CurrentWayPoint= SpawnLocation+1;
iObjectLocationSet(BODY, Waypoints[SpawnLocation]);
iObjectOrientationSet(BODY, QuatGetLookAt(Waypoints[SpawnLocation], Waypoints[CurrentWayPoint]));
// create imposters
iObjectImpostersCreate(SM_WHEEL,5);
iObjectImpostersCreate(SM_BODY,1);
// load Rad math
MathDLLHandle = iDLLLoad(".\\3DRad_res\\objects\\Script\\RadMath2.dll");
}
else if (iDeinitializing())
{
if (MathDLLHandle != 0) iDLLUnload(MathDLLHandle);
}
else
{
iObjectOrientation(BODY, qCar);
iObjectLocation(BODY, carLocation);
}
/// START of vehicle script /////
//iObjectImposterShadowEnable(SM_BODY,0,true); //uncomment to enable shadow caster
//iObjectImposterShadowEnable(SM_WHEEL,0,true); //uncomment to enable shadow caster
iObjectLocation(BODY,location);
float speed = IN_44;
/// Move body
// move body
L2P(BODY,BODY,Vector3(0,0,0),Vector3(0,0,Velocity));
// forward
if (throttle==1)
{
Velocity+= 0.001;
}
// backward
if (throttle==-1)
{
Velocity-=0.01;
}
// wheel friction
if (throttle==0 && Velocity>0)
{
Velocity-=0.002;
}
if (throttle==0 && Velocity<0)
{
Velocity+=0.002;
}
// top speed
if (Velocity<-MaxSpeed)
{
Velocity=-MaxSpeed;
}
if (Velocity>MaxSpeed)
{
Velocity=MaxSpeed;
}
// car direction
if (Velocity<0)
{
direction=-1;
}
if (Velocity>0)
{
direction=1;
}
/// Turning
if (iKeyDown(iKeyCode("DIK_LEFT")))
{
//steering=-1;
}
if (iKeyDown(iKeyCode("DIK_RIGHT")))
{
//steering=1;
}
if (!iKeyDown(iKeyCode("DIK_RIGHT"))&& !iKeyDown(iKeyCode("DIK_LEFT")))
{
//steering=0;
}
steering= -(relativeAngleOfTarget()/MaxAngle);
if (steering !=0 && speed > MinimumTurnVelocity)
{
L2P(BODY,BODY,Vector3(0,(steering*direction),0),Vector3(0,0,0));
}
if (steering>1)
{
steering=1;
}
if (steering<-1)
{
steering=-1;
}
// turn wheels
SteeringAngle=MaxAngle*steering;
/// Imposter Locations
// wheel spin
spin += (speed*2*direction);
//Locate and rotate rear wheels based on speed
ImposterChildTransform(BODY,SM_WHEEL, 0,Vector3(spin,0,0), Vector3(WheelsSideOffset , HeightOffset, RearWheelsForwardOffset));
ImposterChildTransform(BODY,SM_WHEEL, 1,Vector3(spin,0,0),Vector3(-WheelsSideOffset ,HeightOffset, RearWheelsForwardOffset));
//Locate and rotate front wheels based on speed & steering angle
ImposterChildTransform(BODY,SM_WHEEL, 2,Vector3(spin,SteeringAngle,0),Vector3(WheelsSideOffset, HeightOffset, FrontWheelsForwardOffset));
ImposterChildTransform(BODY,SM_WHEEL, 3,Vector3(spin,SteeringAngle,0),Vector3(-WheelsSideOffset, HeightOffset, FrontWheelsForwardOffset));
//set car body location
ImposterChildTransform(BODY,SM_BODY, 0,Vector3(0,0,0), Vector3(BodySideForwardOffset, HeightOffset, BodyForwardForwardOffset));
// set collision model location
L2P(BODY,CARBODY,Vector3(0,0,0),Vector3(BodySideForwardOffset, HeightOffset, BodyForwardForwardOffset));
/// AI
/////////////////////////////
float distance= iVectorLength(carLocation-targetLocation);
// if close to waypoint, get next waypoint
if(distance < tooClose && CurrentWayPoint != 13)
{
CurrentWayPoint ++;
}
// If close to waypoint and current waypoint is last waypoint, get first waypoint
if(distance < tooClose && CurrentWayPoint == 13)
{
CurrentWayPoint = 0;
}
// Waypoints:
Waypoints[10] = Vector3(-138.797,0.12,8.82438);
Waypoints[11] = Vector3(-137.879,0.12,138.153);
Waypoints[12] = Vector3(-138.026,0.12,258.928);
Waypoints[0] = Vector3(-138.172,0.12,368.654);
Waypoints[1] = Vector3(-137.694,0.12,460.448);
Waypoints[2] = Vector3(-60.0052,0.12,460.162);
Waypoints[3] = Vector3(-2.75631,-3.19,460.835);
Waypoints[4] = Vector3(-3.33709,0.12,382.131);
Waypoints[5] = Vector3(-3.45288,0.12,282.166);
Waypoints[6] = Vector3(-3.22019,0.12,210.437);
Waypoints[7] = Vector3(-3.45782,0.12,69.8568);
Waypoints[8] = Vector3(-3.79037,0.12,8.82839);
Waypoints[9] = Vector3(-81.8849,0.12,9.26258);
targetLocation=Waypoints[CurrentWayPoint];
}
///// END VOID MAIN /////
// Steer to taget
float relativeAngleOfTarget()
{
Quaternion qTarget, qDiff;
Vector3 up = Vector3(0,1,0);
Vector3 direction = targetLocation-carLocation;
direction.y=0;
iVectorLengthSet(direction,direction,1);
iQuaternionLookAt(qTarget,direction,up);
iQuaternionMultiply(qDiff, qCar, qInv(qTarget)); // quat subtraction. by AllanF
Vector3 v;
iVectorRotate(v,Vector3(0,0,1),qDiff);
float a= R_atan2(v.x, v.z);
isBehind= (v.z<0);
if (isBehind)
if (v.x<0)
a=-2-a;
else
a=2-a;
return 180*a/2;
}
Quaternion qInv(Quaternion q)
{
Quaternion qi;
qi.x= -q.x; // flip xyz direction
qi.y= -q.y;
qi.z= -q.z;
qi.w= q.w; // same angle axis
return qi;
}
float R_atan2(float value1, float value2)
{
iDLLArraySet(0,value1);
iDLLArraySet(1,value2);
iDLLCall(MathDLLHandle,"R_atan2",0);
float f = iDLLArrayGet(0);
return f;
}