3

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);
}

help 1

enter image description here

mh4
  • 63
  • 3
  • If you guys need more information or clarification, just comment. I'll try to explain more. Im sorry my english is not that good. – mh4 Mar 11 '14 at 14:57

1 Answers1

0

Essentially what you are looking for are non-axis aligned rectangle intersections. Here is some information on how to handle such a scenario:

Area of rectangle-rectangle intersection

Community
  • 1
  • 1
YoungJohn
  • 946
  • 12
  • 18
  • also you can look here: http://stackoverflow.com/questions/2089173/non-axis-aligned-rectangle-intersection – YoungJohn Mar 11 '14 at 16:11
  • hi @YoungJohn. Thanks for the quick reply (and sorry for my late one) and i will look more into it. – mh4 Mar 13 '14 at 12:16