Jump to content
  • Advertisement
Sign in to follow this  
ToRuk MaKto

SDL/C++ Collision help

This topic is 3164 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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},

Share this post


Link to post
Share on other sites
Advertisement
Please use [ code ][ /code ] 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?

Share this post


Link to post
Share on other sites
If you look at the collision detection that I do in my Ghost Toast game, you'll see it is exactly what you want.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!