Archived

This topic is now archived and is closed to further replies.

Does this look right to you?

This topic is 5309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m making a driving game and this is my torquelook up function i have an array of torque (don''t be fooled by its name, hp, ) values and an rpm, the torques are every 500 rpm so im interpolated between the 2 clossest torque values, how does this look?
double cCar::engtorque()
{
	if (rpm == revlimit) return hp[revlimit];
	else
	{
		return hp[rpm/500] + (hp[(rpm/500)+1] - hp[rpm/500]) * (rpm%500)/500;
	}
}
It would probably run faster if i put rpm/500 in a variable but optimization is later.

Share this post


Link to post
Share on other sites
Bug:
	if (rpm == revlimit) return hp[ revlimit ];  

should be
	if (rpm > revlimit) return hp[ NUM_TORQUES-1 ];  


Optimization note: using 512 rather than 500 might make a difference.


Share this post


Link to post
Share on other sites
Sorry i didn't post the code right before it but in the function that calls this it says if (rpm > revlimit) rpm = revlimit; But, i realise that calling hp[revlimit] will over run the allocation by ALOT it needs to be hp[revlimit/500], or hp[rpm/500] since they are the same.

But i assume u mean everything else looks good, thanks.

edit:


void cCar::update(double deltat)
{
rpm = (long) /*wheel rotation rate **/ ((gear == -1) ? 0 : params->gearratios[gear]) * params->transratio * .7 * 60 / 2* pi; //60/2pi if in rads, 60/360 if in degrees

if (rpm < 1000) rpm = 1000; //don't stall

if (rpm > params->revlimit) rpm = params->revlimit; //dont' blow

double torque = engtorque() * ((gear == -1) ? 0 : params->gearratios[gear]) * params->transratio * .7; //guess on transeff (from .7-1)

double maxspeed = rpm * ((gear == -1) ? 0 : params->gearratios[gear]) * params->transratio * .7; //rpm * gear * trans * efficiency


//######DRIVE################################################################

if (control & 1) //gas on

{
for (int i = ((params->fourwheel) ? 0 : 2); i < 4; i ++)
{
dJointSetHinge2Param(wheeljoints[i], dParamFMax2, torque);
dJointSetHinge2Param(wheeljoints[i], dParamVel2, ((i % 2) == 0) ? -maxspeed : maxspeed);
}
}
else if (control & 2) //brake on

{
for (int i = 0; i < 4; i ++)
{
dJointSetHinge2Param(wheeljoints[i], dParamFMax2, params->brakepower);
dJointSetHinge2Param(wheeljoints[i], dParamVel2, 0);
}
}
else //free wheel

{
for (int i = 0; i < 4; i ++)
dJointSetHinge2Param(wheeljoints[i], dParamFMax2, 0); //set it to have zero power

}
}


[edited by - honayboyz on June 5, 2003 12:04:57 PM]

Share this post


Link to post
Share on other sites