0
\$\begingroup\$

I have problems with collision detection in my isometric game - the collision is on a different spot then the actual object is, its like the SDL_Rect is somewhere else then the object itself which is impossible. I checked the values with the debugger it seems there is everything right.

My player has Isometric movement. This is the calculation for the screenworld to isometric

Vector2 Vector2::PosToIso()
{
    return Vector2(x - y, (y + x) / 2);
}

When the player doesnt have the isometric movement the collision works fine. I tried to calculate the the objec's x and y pos to iso and i tried with the normal 2D screen values same result.

Collision code:

    bool checkCollision( SDL_Rect a, SDL_Rect b )
{
    int leftA, leftB;
    int rightA, rightB;
    int topA, topB;
    int bottomA, bottomB;

    leftA = a.x;
    rightA = a.x + a.w;
    topA = a.y;
    bottomA = a.y + a.h;

    leftB = b.x;
    rightB = b.x + b.w;
    topB = b.y;
    bottomB = b.y + b.h;

  if( bottomA < topB )
    {
        return false;
    }

    if( topA > bottomB )
    {
        return false;
    }

    if( rightA < leftB )
    {
        return false;
    }

    if( leftA > rightB )
    {
        return false;
    }
    return true;
}
\$\endgroup\$
  • 2
    \$\begingroup\$ There is no such thing as "isometric collision" (which is what your question was titled before I renamed it). Collision is a physical occurrence. Isometry is simply a view point. When developing isometric games, you should always have two views: the regular old top-down view which is easy to use for debugging game logic (like collisions), and your more complicated isometric view. You should be able to switch between them e.g by pressing a key. Else when things go wrong, you won't know whether it is the collision code that is wrong, or the isometric rendering code. Keep it simple! \$\endgroup\$ – Engineer May 5 at 13:51
1
\$\begingroup\$

First of even you have isometric movement or place your objects in isometric space the Objects stay still in normal "topdown" 2D so the collision happens the same way as you would have a top down game (if your work in 2D space only) - the shape of SDL_Rect doesn´t change. You didn`t provide the code where you keep track of your collision Rect so its hard to help you. You can try to use SDL_RenderDrawRect to draw your collisionbox around your player and so on and see how the rects behave when you move your player. Also you can use SDL_HasIntersection to check if there is a collision between 2 rects instead of your check collision function you have.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Exactly SDL_HasIntersection() is the way to go with simple rects, IIRC it is SSE accelerated. \$\endgroup\$ – Lin Jun 20 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.