SDL/C++ Collision help

Started by
1 comment, last by mmakrzem 14 years ago
Hi guys, im making a simple plattform game. And i have no idea how to make a collision detector for player/tile. this is my tile function: class Tile { private: SDL_Rect box; public: Tile(); void show(); }; Tile::Tile() { //Get the offsets int x, y; box.x = x; box.y = y; //Set the collision box box.w = TILE_WIDTH; box.h = TILE_HEIGHT; } void Tile::show() { for(int v = 0; v < VDOT; v++){ for(int h = 0; h < HDOT; h++){ if(MAP[h][v] == 1){ apply_surface(v*SQUARE_WIDTH, h*SQUARE_HEIGHTtile,screen); } } } } and this is for the square movement: void Square::move() { box.x += xVel; if( ( box.x < 0 ) || ( box.x + SQUARE_WIDTH > SCREEN_WIDTH ) || ( check_collision( box, wall ) ) ) { box.x -= xVel; } //Move the square up or down box.y += yVel; //If the square went too far up or down or has collided with the wall if( ( box.y < 0 ) || ( box.y + SQUARE_HEIGHT > SCREEN_HEIGHT ) || ( check_collision( box, wall ) ) ) { //Move back box.y -= yVel; } } the MAP/tiles is like this: const int MAP[HDOT][VDOT] = {{0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
Advertisement
Please use or [ source ][ /source ] tags (without the spaces) when posting code. That way it will keep your indents and stuff. As posted, your code is kind of hard to read. More details here.

Anyway...
Quote://Get the offsets
int x, y;
box.x = x;
box.y = y;

Is that your actual code? Basically, all it does is use random, uninitialized values in your box.

It seems that your Tile class is inaptly named. I'd say that it needs to be called TileMap, or something to convey that it manages all the tiles, but then the map doesn't really seem to be a part of it. So maybe there shouldn't be a class there at all.

What exactly is a Square, and what is it supposed to do? What is the box it is using? Is it different from Tile's box?

Sorry for the big load of criticism, but it's just hard to tell what's going on. What exactly are you trying to do?
If you look at the collision detection that I do in my Ghost Toast game, you'll see it is exactly what you want.

This topic is closed to new replies.

Advertisement