Hi cheesy,
The link to RCBot forum below is broken so I have to post here...
Code:
void CBot :: ChangeAngles ( float *fSpeed, float *fIdeal, float *fCurrent, float *fUpdate )
{
float fCurrent180; // current +/- 180 degrees
float fDiff;
// turn from the current v_angle yaw to the ideal_yaw by selecting
// the quickest way to turn to face that direction
// find the difference in the current and ideal angle
fDiff = abs(*fCurrent - *fIdeal);
// check if the bot is already facing the ideal_yaw direction...
if (fDiff <= 0.1)
{
*fSpeed = fDiff;
return; // return number of degrees turned
}
// check if difference is less than the max degrees per turn
if (fDiff < *fSpeed)
*fSpeed = fDiff; // just need to turn a little bit (less than max)
// here we have four cases, both angle positive, one positive and
// the other negative, one negative and the other positive, or
// both negative. handle each case separately...
if ((*fCurrent >= 0) && (*fIdeal >= 0)) // both positive
{
if (*fCurrent > *fIdeal)
*fCurrent -= *fSpeed;
else
*fCurrent += *fSpeed;
}
else if ((*fCurrent >= 0) && (*fIdeal < 0))
{
fCurrent180 = *fCurrent - 180;
if (fCurrent180 > *fIdeal)
*fCurrent += *fSpeed;
else
*fCurrent -= *fSpeed;
}
else if ((*fCurrent < 0) && (*fIdeal >= 0))
{
fCurrent180 = *fCurrent + 180;
if (fCurrent180 > *fIdeal)
*fCurrent += *fSpeed;
else
*fCurrent -= *fSpeed;
}
else // (current < 0) && (ideal < 0) both negative
{
if (*fCurrent > *fIdeal)
*fCurrent -= *fSpeed;
else
*fCurrent += *fSpeed;
}
UTIL_FixFloatAngle(fCurrent);
*fUpdate = *fCurrent;
}
This code has a minor bug (the abs() should be fabs() - you must have learnt BASIC first). Also the speed is depending on FPS value, which causes RCBot is just too easy to beat (they turn very slow) on my old slow machine.
Here is my new code. I've made the turning FPS independent (basically idea from POD-Bot) and simplified the code a lot (IMHO botman's original code is just too complicated). It should work, though I've only tested it on HLDM.
Code:
void CBot :: ChangeAngles ( float *fSpeed, float *fIdeal, float *fCurrent, float *fUpdate )
{
// turn from the current v_angle yaw to the ideal_yaw by selecting
// the quickest way to turn to face that direction
// find the difference in the current and ideal angle
float move = *fIdeal - *fCurrent;
UTIL_FixFloatAngle(&move);
float fSpeedFactor = 72 * gpGlobals->frametime;
if (fSpeedFactor < 1.0)
fSpeedFactor = 1.0;
float speed = *fSpeed * fSpeedFactor;
if (move > 0)
{
if (move > speed)
move = speed;
}
else
{
if (move < -speed)
move = -speed;
}
*fCurrent += move;
UTIL_FixFloatAngle(fCurrent);
*fUpdate = *fCurrent;
}
Hope this helps... (Also the RCBot TK's quite a lot in HL Teamplay mode, this may be a serious bug, I haven't found how to solve it yet)