it's not that black and white, since the spring doesnt allow you to bring all speed on the 'cursor' instantly, unless you have a infinite high spring constant.
anyway ...
I couldnt stop me from working also on this, so here a little info on the little I did ( works really nice btw
)
Idea ( that's almost the standard spring with damping differential equation ) :
m x'' + k ( x' - v0 ) + d ( x - x0 ) = 0 o_O
where we have
- x : the position of the mass ( in our case, the cursor ) or better, a function depending of time for the position.
- v0 : the velocity of the target to follow
- x0 : the position of the target to follow
- ... I hope you know what the ' are meaning. if not, that are the derivatives of x. x' is the change of position over time, i.e. the speed. x'' is accordingly the acceleration.
As always with differential equations, we search a function x which fits in that equation above, where that relation is true. This is usually done by searching the solution of the homogenous DEq, in our case that's
m x'' + k x' + d x = 0. You can solve this with an exponential approach, i.e. you set
x = exp( l * t ), put it into the hom DEq and divide by
exp ( l * t ). This way we get a quadratic expression for l :
m l^2 + k l + d = 0 which can be easily solved :
l = -k/2m +- Sqrt( k^2 - 4 d m )/2m
Here the oscillations come into play : We know that
exp ( i x ) = i sin ( x ) + cos( x ) and that
exp(a) exp(b) = exp (a + b). Thus, if we get a l which contains i (
i = Sqrt(-1), called imaginary number ( dunno the english term )), we'd get oscillations. Since we don't wanna get them, we have to chose
d and
k accordingly. when we have
k^2 = 4 d m, this is called the aperiodic limit, which means that if this is the case, this is the fastest way to get to 0 ( in our hom DEq ). if
k^2 > 4 d m we'd still have no oscillations, but approaching will be slower. ( for aiming in the bot a little bit of oscillations might be acceptable since we have bullet spread, the punchangle stuff etc. )
the solution of the inhomogenous part of the DEq isnt of interest here since it's constant if v0 and x0 are constant. x0 isnt of course constant, since v0 is normally nonzero, but solving the DEq with x0 and v0 = x0' only gives nasty stuff ( at least that's what mathematica tells me
) which looks pretty nonoscillating, and the main interest here were the oscillations.
OK, enough ! I know how you like math, but I hope this is understandable, at least I tried 8D
Code:
float m = 1,
k = -1,
d = k*k / 4.f / m;
if(abs(m_VIdealAngles.y - m_VCurrentAngles.y) > 180){
if(m_VIdealAngles.y > 0){
m_VIdealAngles.y -= 360.0f;
}
else{
m_VIdealAngles.y += 360.0f;
}
}
m_VAnglesSpeed.y += (k/m * (m_VAnglesSpeed.y - m_VAngLookSpeed.y) + d/m * (m_VIdealAngles.y - m_VCurrentAngles.y))*m_fmsecCount / 1000.f;
m_VCurrentAngles.y += m_fmsecCount / 1000.f * m_VAnglesSpeed.y;
m_VAnglesSpeed.x += (k/m * (m_VAnglesSpeed.x - m_VAngLookSpeed.x) + d/m * (m_VIdealAngles.x - m_VCurrentAngles.x)) * m_fmsecCount / 1000.f;
m_VCurrentAngles.x += m_fmsecCount / 1000.f * m_VAnglesSpeed.x;
//---------------------------
if(abs(m_VIdealVAngle.y - m_VCurrentVAngle.y) > 180){
if(m_VIdealVAngle.y > 0){
m_VIdealVAngle.y -= 360.0f;
}
else{
m_VIdealVAngle.y += 360.0f;
}
}
m_VVAngleSpeed.y += (k/m * (m_VVAngleSpeed.y - m_VAngLookSpeed.y) + d/m * (m_VIdealVAngle.y - m_VCurrentVAngle.y))*m_fmsecCount / 1000.f;
m_VCurrentVAngle.y += m_fmsecCount / 1000.f * m_VVAngleSpeed.y;
m_VVAngleSpeed.x += (k/m * (m_VVAngleSpeed.x - m_VAngLookSpeed.x) + d/m * (m_VIdealVAngle.x - m_VCurrentVAngle.x))*m_fmsecCount / 1000.f;
m_VCurrentVAngle.x += m_fmsecCount / 1000.f * m_VVAngleSpeed.x;
m_VOutVAngle = m_VCurrentVAngle;
m_VOutAngles = m_VCurrentAngles;
( I thought tobias algorithm was based on this one, but if it is, it's a very rough approximation I think. anyway, that code is also used in other circumstances, e.g. in learning algorithms for neural networks etc. )