Ok so this is quite difficult for me to word this but here goes.
I'm trying to create a game that is like Crazy Taxi where it is a top down view game and the player controls the car. In the pictures below the red box is my car while i divide the rest of the map into grids. The size of the car and the grids are 25 by 25 so yes a car is the size of 1 grid. Now i tried to research on how to do collision with map grids and i got these link: http://www.emanueleferonato.com/2012/05/24/the-guide-to-implementing-2d-platformers/
So i came up with this in my CheckCollision function:
// which tile are you at?
int tileX = (int) playerPos.x / TILE_SIZE;
int tileY = (int) playerPos.y / TILE_SIZE;
if (right)
{
if (m_Map->GetGrid(tileX + 1, tileY).GetID() == ROAD_BORDER_LEFT ||
m_Map->GetGrid(tileX + 1, tileY).GetID() == ROAD_BORDER_RIGHT ||
m_Map->GetGrid(tileX + 1, tileY).GetID() == ROAD_BORDER_UP ||
m_Map->GetGrid(tileX + 1, tileY).GetID() == ROAD_BORDER_DOWN)
{
return true;
}
}
if (up)
{
if (m_Map->GetGrid(tile_X, tile_Y - 1).GetID() == ROAD_BORDER_LEFT ||
m_Map->GetGrid(tile_X, tile_Y - 1).GetID() == ROAD_BORDER_RIGHT ||
m_Map->GetGrid(tile_X, tile_Y - 1).GetID() == ROAD_BORDER_UP ||
m_Map->GetGrid(tile_X, tile_Y - 1).GetID() == ROAD_BORDER_DOWN)
{
return true;
}
}
And i check for right, left, up and down. Now the problem is that I realised the link was for games that had fixed axis (like side scrolling games). For my game I can rotate the car around and i move forward depending of that axis. Eg. if my car is facing right and i press 'w' the car will go right instead of up.
So my question I think should be: How do I keep track which grids to check depending on the direction my car is facing? I just want to check what's in front, behind and sides of my car. Eg. if there's something in front, i cant move forward.
PS. I can move freely within the map. That means my car can be between two grids.
EXTRA INFO:
My code for moving my car forward:
if (CInputManager::GetInstance()->GetKeyDown('w') &&
!CheckCollision(m_Player->GetPosition(), false, false, true, false))
{
// increase acceleration
m_Player->MoveForward();
My code for turning right:
if (CInputManager::GetInstance()->GetKeyDown('d') /*&&
!CheckCollision(m_Player->GetPosition(), true, false, false, false)*/)
{
m_Player->TurnRight();
// inside the TurnRight function is:
// using polar coordinates as direction
/*
m_fDirAngle+=6; // its plus instead of minus because of the orthoganal axises
if (m_fDirAngle >= 360)
{
m_fDirAngle = 0;
}
*/
And this is how i move the car:
Vector2 CCar::GetDirection(void)
{
// useful link: http://www.mathsisfun.com/polar-cartesian-coordinates.html
// remember to convert to radians and remember to -360 because of the different rotation (cc vs clockwise)
double deltaRadian = m_fDirAngle * (PI/180.0);
m_dir.Set( cos( deltaRadian ), sin( deltaRadian ));
return m_dir;
}
void CCar::Update()
{
Vector2 move;
move.Set(GetDirection().x * (m_fSpeed + m_fCurAcceleration), GetDirection().y * (m_fSpeed + m_fCurAcceleration));
this->m_pos.Set(m_pos.x + move.x, m_pos.y + move.y);
}