var temp_var;
float temp_float;
ENTITY* car;
ENTITY* FLwheelPtr;
ENTITY* FRwheelPtr;
ENTITY* BLwheelPtr;
ENTITY* BRwheelPtr;
ENTITY* tireId;
ENTITY* tireModel;
D3DXMATRIX m;
void tireUpdate(NewtonJoint* vehicle)
{
// FOR EACH TIRE APPLY THESE VALUES
for (tireId = NewtonVehicleGetFirstTireID (vehicle); tireId!=NULL; tireId = NewtonVehicleGetNextTireID (vehicle, tireId))
{
NewtonVehicleSetTireSideSleepCoeficient(vehicle,tireId,0.0);
NewtonVehicleSetTireMaxSideSleepSpeed (vehicle,tireId,999.0);
NewtonVehicleSetTireLongitudinalSlideCoeficient(vehicle,tireId,0.0);
NewtonVehicleSetTireMaxLongitudinalSlideSpeed (vehicle,tireId,999.0);
}
// CALCULATE AVERAGE TIRE SPEED
tireId = NewtonVehicleGetFirstTireID(vehicle);
temp_var = NewtonVehicleGetTireLongitudinalSpeed(vehicle,tireId);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId);
temp_var += NewtonVehicleGetTireLongitudinalSpeed(vehicle,tireId);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId);
temp_var += NewtonVehicleGetTireLongitudinalSpeed(vehicle,tireId);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId);
temp_var += NewtonVehicleGetTireLongitudinalSpeed(vehicle,tireId);
temp_var *= 0.25;
if(key_w)
{
if(temp_var<-2) //if tires average speed is smaller than -2 and key W pressed >>>>>> APPLY BRAKE TO ALL TIRES
{
tireId = NewtonVehicleGetFirstTireID(vehicle); //tireId 0 //Front Left
temp_float = NewtonVehicleTireCalculateMaxBrakeAcceleration(vehicle,tireId) * 0.7;
NewtonVehicleTireSetBrakeAcceleration(vehicle,tireId,temp_float,temp_float);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 1 //Front Right
temp_float = NewtonVehicleTireCalculateMaxBrakeAcceleration(vehicle,tireId) * 0.7;
NewtonVehicleTireSetBrakeAcceleration(vehicle,tireId,temp_float,temp_float);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 2 //Back Left
temp_float = NewtonVehicleTireCalculateMaxBrakeAcceleration(vehicle,tireId) * 0.7;
NewtonVehicleTireSetBrakeAcceleration(vehicle,tireId,temp_float,temp_float);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 3 //Back Right
temp_float = NewtonVehicleTireCalculateMaxBrakeAcceleration(vehicle,tireId) * 0.7;
NewtonVehicleTireSetBrakeAcceleration(vehicle,tireId,temp_float,temp_float);
}
else //else >>>>>> APPLY 1500 TORQUE FORWARD TO REAR TIRES
{
tireId = NewtonVehicleGetFirstTireID(vehicle); //tireId 0 //Front Left
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 1 //Front Right
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 2 //Back Left
NewtonVehicleSetTireTorque(vehicle,tireId,1500);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 3 //Back Right
NewtonVehicleSetTireTorque(vehicle,tireId,1500);
}
}
else if(key_s)
{
if(temp_var<2) //if tires average speed is smaller than 2 >>>>>> APPLY 1500 TORQUE BACKWARD TO REAR TIRES
{
tireId = NewtonVehicleGetFirstTireID(vehicle); //tireId 0 //Front Left
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 1 //Front Right
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 2 //Back Left
NewtonVehicleSetTireTorque(vehicle,tireId,-1500);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 3 //Back Right
NewtonVehicleSetTireTorque(vehicle,tireId,-1500);
}
else //else >>>>>> APPLY BRAKE TO ALL TIRES
{
tireId = NewtonVehicleGetFirstTireID(vehicle); //tireId 0 //Front Left
temp_float = NewtonVehicleTireCalculateMaxBrakeAcceleration(vehicle,tireId) * 0.7;
NewtonVehicleTireSetBrakeAcceleration(vehicle,tireId,temp_float,temp_float);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 1 //Front Right
temp_float = NewtonVehicleTireCalculateMaxBrakeAcceleration(vehicle,tireId) * 0.7;
NewtonVehicleTireSetBrakeAcceleration(vehicle,tireId,temp_float,temp_float);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 2 //Back Left
temp_float = NewtonVehicleTireCalculateMaxBrakeAcceleration(vehicle,tireId) * 0.7;
NewtonVehicleTireSetBrakeAcceleration(vehicle,tireId,temp_float,temp_float);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 3 //Back Right
temp_float = NewtonVehicleTireCalculateMaxBrakeAcceleration(vehicle,tireId) * 0.7;
NewtonVehicleTireSetBrakeAcceleration(vehicle,tireId,temp_float,temp_float);
}
}
if(key_a) //if key A >>>>>> SET FRONT TIRES ANGLE TO 0.35 RADIAN
{
tireId = NewtonVehicleGetFirstTireID(vehicle); //tireId 0 //Front Left
NewtonVehicleSetTireSteerAngle(vehicle,tireId,0.35);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 1 //Front Right
NewtonVehicleSetTireSteerAngle(vehicle,tireId,0.35);
}
else if(key_d) //if key D >>>>>> SET FRONT TIRES ANGLE TO -0.35 RADIAN
{
tireId = NewtonVehicleGetFirstTireID(vehicle); //tireId 0 //Front Left
NewtonVehicleSetTireSteerAngle(vehicle,tireId,-0.35);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 1 //Front Right
NewtonVehicleSetTireSteerAngle(vehicle,tireId,-0.35);
}
else //else >>>>>> SET FRONT TIRES ANGLE TO 0.0 RADIAN
{
tireId = NewtonVehicleGetFirstTireID(vehicle); //tireId 0 //Front Left
NewtonVehicleSetTireSteerAngle(vehicle,tireId,0.0);
tireId = NewtonVehicleGetNextTireID(vehicle, tireId); //tireId 1 //Front Right
NewtonVehicleSetTireSteerAngle(vehicle,tireId,0.0);
}
}
action Newton_Car()
{
car = me;
while(!newton_running){wait(1);}
NewtonBody* car_body = newton_addentity(me, 1000, NEWTON_CONVEXHULL, tireRubberID, onforceandtorque);
NewtonBodySetAutoFreeze( car_body, 0);
NewtonBodySetLinearDamping(car_body,0.005);
// NewtonBodySetAngularDamping(car_body,vectorf(0.5,0.5,0.5));
float upDir[3] = {0.0, 0.0, 1.0};
NewtonJoint* car_joint = NewtonConstraintCreateVehicle(nworld, upDir[0], car_body);
int tireMass = 30;
float suspensionLength = 0.25;
float suspensionSpring =700.0;
float suspensionShock = 60.0;
float wheelradius = 0.4;
float tirePin[3] = {0.0, 1.0, 0.0};
D3DXMATRIX tire_offset;
VECTOR tirepos;
vec_for_bone(tirepos,me,"FLBone");
vec_sub(tirepos,my.x);
D3DXMatrixTranslation(&tire_offset, tirepos.x * QUANTTOMETER,tirepos.y * QUANTTOMETER,tirepos.z * QUANTTOMETER);
NewtonVehicleAddTire(car_joint, &tire_offset, tirePin[0], tireMass, wheelradius, wheelradius, suspensionShock,suspensionSpring,suspensionLength, FLwheelPtr, 5);
vec_for_bone(tirepos,me,"FRBone");
vec_sub(tirepos,my.x);
D3DXMatrixTranslation(&tire_offset, tirepos.x * QUANTTOMETER,tirepos.y * QUANTTOMETER,tirepos.z * QUANTTOMETER);
NewtonVehicleAddTire(car_joint, &tire_offset, tirePin[0], tireMass, wheelradius, wheelradius, suspensionShock,suspensionSpring,suspensionLength, FRwheelPtr, 5);
vec_for_bone(tirepos,me,"BLBone");
vec_sub(tirepos,my.x);
D3DXMatrixTranslation(&tire_offset, tirepos.x * QUANTTOMETER,tirepos.y * QUANTTOMETER,tirepos.z * QUANTTOMETER);
NewtonVehicleAddTire(car_joint, &tire_offset, tirePin[0], tireMass, wheelradius, wheelradius, suspensionShock,suspensionSpring,suspensionLength, BLwheelPtr, 5);
vec_for_bone(tirepos,me,"BRBone");
vec_sub(tirepos,my.x);
D3DXMatrixTranslation(&tire_offset, tirepos.x * QUANTTOMETER,tirepos.y * QUANTTOMETER,tirepos.z * QUANTTOMETER);
NewtonVehicleAddTire(car_joint, &tire_offset, tirePin[0], tireMass, wheelradius, wheelradius, suspensionShock,suspensionSpring,suspensionLength, BRwheelPtr, 5);
NewtonVehicleSetTireCallback(car_joint, tireUpdate);
while(me!=NULL)
{
// SET ALL TIRES POSITION AND ANGLE [ THESE LINES ARE NOT AT TIRE CALLBACK FUNC. BECAUSE MAKING THAT CAUSES VISUAL TIRES TO UPDATE 1 FRAME LATER THAN NEWTON TIRES ]
for (tireId = NewtonVehicleGetFirstTireID (car_joint); tireId!=NULL; tireId = NewtonVehicleGetNextTireID (car_joint, tireId))
{
tireModel = NewtonVehicleGetTireUserData (car_joint, tireId);
NewtonVehicleGetTireMatrix(car_joint,tireId, m);
ent_setmatrix_rb(tireModel,m);
}
wait(1);
}
}