[All]
A Quick and Dirty VCL Sprite Engine - Part IV
Por: John Ray Thomas
Resumo: Update sprites to a new locations
Update sprites to a new locations
The orgin of a Windows HDC coordinate system is at the top-left of the device. It extends to the right in the positive x
and down, in the positive y. Any position on the HDC can be described with a positive (x,y) coordinate.
The VCL has a TPoint structure that contains two int's, x and y, and is perfect for keeing track of the poition. I also suggest a TRect for keeping track of the image's clipping area.
Movement is as simple as updating the position to a new x and y in the direction and distance you want sprite to move in one frame. Don't forget to update the clipping rectangle also. The following code example first updates the sprite, the checks a clip against the playing area. If it clips it resets it to it's last state.
void TankSprite::Move(TPoint NewPosition)
{
lastPos = Position;
Position = NewPosition;
clipRect.Top = Position.y;
clipRect.Bottom = Position.y + spriteHeight;
clipRect.Left = Position.x;
clipRect.Right = Position.x + spriteWidth;
if(ClipPlayingArea(TRect(0,0,640,480)))
{
Position = lastPos;
clipRect.Top = Position.y;
clipRect.Bottom = Position.y + spriteHeight;
clipRect.Left = Position.x;
clipRect.Right = Position.x + spriteWidth;
}
}
//---------------------------------------------------------------------------
void TankSprite::TurnLeft()
{
dir -= 1;
if(dir < NORTH)
dir = NORTHEAST;
}
//---------------------------------------------------------------------------
void TankSprite::TurnRight()
{
dir += 1;
if(dir > NORTHEAST)
dir = NORTH;
}
//---------------------------------------------------------------------------
void TankSprite::MoveForward()
{
if(dir == NORTH)
Move(TPoint(Position.x, Position.y - velocity));
else if(dir == WEST)
Move(TPoint(Position.x + velocity, Position.y));
else if(dir == SOUTH)
Move(TPoint(Position.x, Position.y + velocity));
else if(dir == EAST)
Move(TPoint(Position.x - velocity, Position.y));
else
Move(TPoint(Position.x + velocity * sin(angleTable[dir]),
Position.y - velocity * cos(angleTable[dir])));
}
//---------------------------------------------------------------------------
void TankSprite::MoveBackward()
{
if(dir == NORTH)
Move(TPoint(Position.x, Position.y + velocity));
else if(dir == WEST)
Move(TPoint(Position.x - velocity, Position.y));
else if(dir == SOUTH)
Move(TPoint(Position.x, Position.y - velocity));
else if(dir == EAST)
Move(TPoint(Position.x + velocity, Position.y));
else
Move(TPoint(Position.x - velocity * sin(angleTable[dir]),
Position.y + velocity * cos(angleTable[dir])));
}
//---------------------------------------------------------------------------
|
Connect with Us