Game Development Reference
In-Depth Information
3.
Calculate the texture coordinates of the tile and store them in FRect :
float TileWf = 1.0f / Columns, TileHf = 1.0f / Rows;
float X1f = TileWf * ( OriginX + 0 );
float X2f = TileWf * ( OriginX + 1 );
float Y1f = TileHf * ( OriginY + 0 );
float Y2f = TileHf * ( OriginY + 1 );
FRect = LRect( X1f, Y1f, X2f, Y2f );
FTarget = FCur = vec2( OriginX, OriginY );
}
4.
The next two methods set the target and current coordinates:
void SetTarget( int X, int Y )
{ FTarget = vec2( X, Y ); }
void MoveTo( float X, float Y )
{ FCur.x = X; FCur.y = Y; };
5.
The tile moves to the target coordinates smoothly. We update the tile position using
the time counter, and for each time step, the coordinates are recalculated:
void Update( float dT )
{
vec2 dS = FTarget - FCur;
const float c_Epsilon = 0.001f;
if ( fabs( dS.x ) < c_Epsilon )
{
dS.x = 0;
FCur.x = FTarget.x;
}
if ( fabs( dS.y ) < c_Epsilon )
{
dS.y = 0;
FCur.y = FTarget.y;
}
const float Speed = 10.0f;
FCur += Speed * dT * dS;
}
};
 
Search WWH ::




Custom Search