1. 程式人生 > >第十七回 路徑動畫

第十七回 路徑動畫

//time是0..1之間的值i_math::vector3df GetPositionOnCubic(const i_math::vector3df &startPos, const i_math::vector3df &startVel, 
            
const i_math::vector3df &endPos, const i_math::vector3df &endVel, float time)
{
    
static i_math::matrix44f hermite;
    
static BOOL bInit=FALSE;
    
if (!bInit)
    { 
        hermite.
set(     2.f,-2.f, 1.f, 1.f,
                
-3.f, 3.f,-2.f,-1.f,
                
0.f, 0.f, 1.f, 0.f,
                
1.f, 0.f, 0.f, 0.f);

        bInit
=TRUE;
    };

    i_math::matrix44f m;
    m.
set(startPos.x,startPos.y,startPos.z,1,
        endPos.x,endPos.y,endPos.z,
1,
        startVel.x,startVel.y,startVel.z,
0,
        endVel.x,endVel.y,endVel.z,
0);

    m
=hermite*m;

    i_math::vector4df timeVector;
    timeVector.
set(time*time*time, time*time, time, 1.0f);
    m.transformVect(timeVector);
    timeVector.x
/=timeVector.w;
    timeVector.y
/=timeVector.w;
    timeVector.z
/=timeVector.w;
    
return FORCE_TYPE(i_math::vector3df,timeVector);
}


//time是0..1之間的值i_math::vector3df GetVelocityOnCubic(const i_math::vector3df &startPos, const i_math::vector3df &startVel, 
                
const i_math::vector3df &endPos, const i_math::vector3df &endVel, float time)
{
    
static i_math::matrix44f hermiteVelocity;
    
static BOOL bInit=FALSE;
    
if (!bInit)
    { 
        hermiteVelocity.
set(     0.0f,0.0f,0.0f,0.0f,
                    
6.0f,-6.0f,3.0f,3.0f,
                    
-6.0f,6.0f,-4.0f,-2.0f,
                    
0.0f,0.0f,1.0f,0.0f);

        bInit
=TRUE;
    };

    i_math::matrix44f m;
    m.
set(startPos.x,startPos.y,startPos.z,1,
        endPos.x,endPos.y,endPos.z,
1,
        startVel.x,startVel.y,startVel.z,
0,
        endVel.x,endVel.y,endVel.z,
0);

    m
=hermiteVelocity*m;

    i_math::vector4df timeVector;
    timeVector.
set(time*time*time, time*time, time, 1.0f);
    m.transformVect(timeVector);
    
return FORCE_TYPE(i_math::vector3df,timeVector);
}