Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

C++ tile map collision detection

$
0
0
Hi! I'm trying to implement collision for my 2d platformer game.I understand basic meanings of how to detect it. I don't know how to implement it correctly though. That's how I render my map : void MapScreen::Draw(SDL_Renderer*renderer) { for (int y = 0; y <= map.size(); ++y) { for (int x = 0; x < map[y].size(); ++x) { if(map[y][x] != 0) { SDL_Rect DestRect = {x * 64, y * 64, 64, 64 }; SDL_Rect sourceRect = {map[y][x] * 64, 0, 64, 64 }; tileSet[0]->Draw(renderer, sourceRect, DestRect); } } } } And that's how I would like to detect a collision. GetBlockID returns where on a map the block is set. Getter::getFM() returns instance for fileManager class ->getH() returns window height value. Int x, int y, int hitboxX, int hitboxX - these are player coordinates and width and height of the sprite. Vector2* GameplayScreen::getBlockID(int x, int y) { return new Vector2(x < 0 ? 0 : x / 64, y > Getter::getFM()->getH() ? 0 : (y + 64) / 64); } bool GameplayScreen::checkCollision(Vector2* v2) { } bool GameplayScreen::checkCollisionRB(int x, int y, int hitBoxX, int hitBoxY) { return checkCollision(getBlockID(x + hitBoxX, y + hitBoxY)); } bool GameplayScreen::checkCollisionLB(int x, int y, int hitBoxY) { return checkCollision(getBlockID(x, y + hitBoxY)); } bool GameplayScreen::checkCollisionRT(int x, int y, int hitBoxX) { return checkCollision(getBlockID(x + hitBoxX, y)); } bool GameplayScreen::checkCollisionLT(int x, int y) { return checkCollision(getBlockID(x, y)); } bool GameplayScreen::checkCollisionRC(int x, int y,int hitBoxX, int hitBoxY) { return checkCollision(getBlockID(x + hitBoxX, y + hitBoxY)); } bool GameplayScreen::checkCollisionLC(int x, int y,int hitBoxY) { return checkCollision(getBlockID(x, y + hitBoxY)); } In player class movement method, I want to say : "if not collision you can move, if there is, you can't". Like : if(!Getter::getMap()->checkCollisionLC){ movement things }

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>