'Collision between gamecharacter and wall
I am currently making maze game with SDL, I create the character and the wall with:
SDL_Rect character{
static_cast<int>(charPos.x),
static_cast<int>(charPos.y - charH / 2),
static_cast<int>(charW),
static_cast<int>(charH)
};
SDL_RenderCopy(mRenderer, charTexture, NULL, &character);
and the wall one by one by using:
// Draw walls
SDL_SetRenderDrawColor(mRenderer, 0, 0, 0, 255);
// Draw top wall
SDL_Rect wall{
0, // Top left x
0, // Top left y
800, // Width
10 // Height
};
SDL_RenderFillRect(mRenderer, &wall);
// Draw bottom wall
wall.y = 800 - 10;
SDL_RenderFillRect(mRenderer, &wall);
// Draw right wall
SDL_RenderFillRect(mRenderer, &wall);
// Draw left wall
SDL_RenderFillRect(mRenderer, &wall);
Any ideas for the collision detection so that the character won't pass the wall ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|