SDL collision detection!

Hello, i am trying to make some basic game that are using tiles. Until now it has gone pretty well and forward. But now im just totally stucked. I am trying to write a move funtion to move the player, and after that it checks if there is a collsion and if there is it moves the player back. It works at some parts. But generally it is acting very strange. Could you please take a look at it?

Thanks in advice. I will send all the code i think is necessary and just reply if you need more.

If there is anything else that looks strange just tell me, i want to learn as much as possible.

Here is game.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "base.h"
#include "player.h"
#ifndef GAME_H
#define GAME_H
class game : public base
{
public:
game(void);
~game(void);
private:
bool InitSDL();
void GameLoop();
void HandleEvents();
void UpdateGame();
void Render();
void LoadMap(string filename);
void BlitMap();
bool running;
bool fullscreen;
SDL_Event event;
bool keys[400];
player * Player;
SDL_Surface * screen,*Background,*Blocks;
SDL_Rect BackgroundBox;
vector< vector<int> > map;
};
#endif 


Here is game.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "game.h"
SDL_Rect base::coord;
game::game(void)
{
if(!this->InitSDL()){exit(1);}
BackgroundBox.x = BackgroundBox.y = coord.x = coord.y = 0;
coord.w = BackgroundBox.w = 800;
coord.h = BackgroundBox.h = 600;
fullscreen = false;
Background = Load_Image("Background.png");
Blocks = Load_Image("Blocks.png");
this->LoadMap("test.TILEMAP");
Player = new player;
Player->CurrentMap(map);
this->GameLoop();
}
void game::GameLoop()
{
Uint32 start, end;
running = true;
while(running)
{
  start = SDL_GetTicks();
  this->HandleEvents();
  this->UpdateGame();
  this->Render();
  end = SDL_GetTicks();
  if(1000/FPS > end - start)
  {
   SDL_Delay(1000/FPS - (end - start));
  }
}
}
void game::HandleEvents()
{
while(SDL_PollEvent(&event))
{
  switch(event.type)
  {
  case SDL_QUIT:
   running = false;
   break;
  case SDL_KEYDOWN:
   keys[event.key.keysym.sym] = true;
   break;
  case SDL_KEYUP:
   keys[event.key.keysym.sym] = false;
   break;
  }
}
}
void game::UpdateGame()
{
Player->SetXvel(0);
Player->SetYvel(0);
if(keys[SDLK_a]){
  coord.x-=vel;
  BackgroundBox.x-=vel;
  Player->SetXvel(-3);
  if(BackgroundBox.x <= 0)
  {
   BackgroundBox.x = Background->w - SCREEN_WIDTH;
  }
}
else if(keys[SDLK_d]){
  coord.x+=vel;
  BackgroundBox.x+=vel;
  Player->SetXvel(3);
  if(BackgroundBox.x >= Background->w - SCREEN_WIDTH)
  {
   BackgroundBox.x = 0;
  }
}
if(keys[SDLK_w]){
  Player->SetYvel(-3);
}else if(keys[SDLK_s]){
  Player->SetYvel(3);
}
else if(keys[SDLK_ESCAPE]){
  running = false;
}else if(keys[SDLK_F5]){
  fullscreen = (!fullscreen);
  if(fullscreen)
  {
   screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
  }else{
   screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF);
  }
}
Player->MovePlayer();

}
void game::Render()
{
SDL_BlitSurface(Background,&BackgroundBox,screen,NULL);
this->BlitMap();
Player->Blit(screen);
SDL_Flip(screen);
}
void game::LoadMap(string filename)
{
ifstream in(filename.c_str());
int Width,Height,current;
in >> Width >> Height;
if(!in.is_open())
{
  cout << "Error: could not load "<< filename << endl;
  return;
}
for(int j = 0;j != Height;j++)
{
  vector<int> TempVec;
  for(int k = 0; k != Width;k++)
  {
   if(in.eof())
   {
        cout << "Error: File end reached to early" << endl;
        return;
   }
   in >> current;
   TempVec.push_back(current);
  }
  map.push_back(TempVec);
}
in.close();
}
void game::BlitMap()
{
int start = (coord.x - coord.x % TILE_SIZE)/TILE_SIZE;
if(start < 0){start = 0;}
int end = ((coord.x + coord.w - coord.x % TILE_SIZE)/TILE_SIZE);
if(end > map[0].size()){end = map[0].size();}
for(int j = 0; j != map.size();j++)
{
  for(int k = start; k != end; k++)
  {
   SDL_Rect BlockRect = {(map[j][k]-1)*TILE_SIZE,0,TILE_SIZE,TILE_SIZE};
   SDL_Rect DestRect = {k * TILE_SIZE - coord.x,j*TILE_SIZE,TILE_SIZE,TILE_SIZE};
   SDL_BlitSurface(Blocks,&BlockRect,screen,&DestRect);
  }
}
}
game::~game(void)
{
delete Player;
SDL_FreeSurface(Blocks);
SDL_FreeSurface(Background);
SDL_Quit();
}
bool game::InitSDL()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
  cout << "Error: could not initialize SDL" << endl;
  return false;
}else{
  cout << "SDL initialized!" << endl;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_HWSURFACE|SDL_DOUBLEBUF);
if(screen == NULL)
{
  cout << "Error: could not initialize the screen" << endl;
  return false;
}else{
  cout << "screen initialized!" << endl;
}
if(TTF_Init() == -1)
{
  cout << "Error: could not initialize TTF" << endl;
  return false;
}else{
  cout << "TTF initialized" << endl;
}
for(int i = 0; i != 400; i++)
{
  keys[i] = false;
}
return true;
}


here is player.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "base.h"
#ifndef PLAYER_H
#define PLAYER_H
class player : public base
{
public:
player(void);
void SetXvel(int vel);
void SetYvel(int vel);
void CurrentMap(vector<vector<int> > Map);

void MovePlayer();
void Blit(SDL_Surface * screen);
~player(void);
private:
vector< vector<int> > map;
SDL_Surface * image;
SDL_Rect box;
int Xvel,Yvel;
bool Ground;

};
#endif 


here is player.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "player.h"

player::player(void):
Yvel(3),
Xvel(5)
{
image = Load_Image("Player.png");
SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(image->format,200,0,200));
box.w = image->w;
box.h = image->h;
box.x = SCREEN_WIDTH/2 - box.w/2;
box.y = SCREEN_HEIGHT/2;
Ground = false;
}
void player::SetXvel(int vel)
{
Xvel = vel;
}
void player::SetYvel(int vel)
{
Yvel = vel;
}
void player::MovePlayer()
{
box.x += Xvel;
box.y += Yvel;
int start = (coord.x - coord.x % TILE_SIZE)/TILE_SIZE;
if(start < 0){start = 0;}
int end = ((coord.x + coord.w - coord.x % TILE_SIZE)/TILE_SIZE);
if(end > map[0].size()){end = map[0].size();}
for(int j = 0; j != map.size();j++)
{
  for(int k = start; k != end; k++)
  {
   if(map[j][k] == 0)
   {
        continue;
   }
   SDL_Rect DestRect = {k * TILE_SIZE - coord.x,j*TILE_SIZE,TILE_SIZE,TILE_SIZE};
   if(CheckCollision(box,DestRect))
   {
        if(box.x+box.w >= DestRect.x && box.x + box.w < DestRect.x+DestRect.w)
        {
         box.x-=Xvel;
        }
        if(box.y+box.h >= DestRect.y && box.y + box.h < DestRect.y + DestRect.h)
        {
         box.y-=Yvel;
        }
        if(box.x <= DestRect.x + DestRect.w)
        {
         box.x-=Xvel;
        }
        if(box.y <= DestRect.y + DestRect.h)
        {
         box.y-=Yvel;
        }
   }
  }
}
}
void player::CurrentMap(vector<vector<int> > Map)
{
map = Map;
}
void player::Blit(SDL_Surface * screen)
{
SDL_BlitSurface(image,NULL,screen,&box);
}
player::~player(void)
{
SDL_FreeSurface(image);
}


Here is a class which contains the most basic stuffs that all classes need. its called base

Here is base.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef BASE_H
#define BASE_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
using namespace std;
class base
{
public:
static const int SCREEN_WIDTH = 800;
static const int SCREEN_HEIGHT = 600;
static const int SCREEN_BPP = 32;
static const int FPS = 60;

static const int Gravity = 2;
static const int vel = 2;
static const int TILE_SIZE = 50;
static SDL_Rect coord;
base(void);
SDL_Surface * Load_Image(string filename);
bool CheckCollision(SDL_Rect r1,SDL_Rect r2);
~base(void);
};
#endif 


here is base.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "base.h"

base::base(void)
{
}
SDL_Surface * base::Load_Image(string filename)
{
SDL_Surface * IMG = IMG_Load(filename.c_str());
if(IMG != NULL)
{
  SDL_Surface * Opt = SDL_DisplayFormat(IMG);
  SDL_FreeSurface(IMG);
  cout << "Loading image " << filename << " Succeeded!" << endl;
  return Opt;
}else{
  cout << "Error: image " << filename << " could not be loaded" << endl;
  return NULL;
}
}
bool base::CheckCollision(SDL_Rect r1,SDL_Rect r2)
{
if(r1.x > r2.x+r2.w){
  return false;
}
else if(r1.y > r2.y+r2.h){
  return false;
}
else if(r1.x+r1.w < r2.x){
  return false;
}
else if(r1.y+r1.h < r2.y){
  return false;
}else{
  return true;
}
}
base::~base(void)
{
}
I'm guessing if you intersect something from the left side it will pull you all the way to the opposite side of the object?

and if you intersect something from the bottom it will pull you all the way up? (if y=0 is top of screen)

in Player::movePlayer():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   if(CheckCollision(box,DestRect))
   {
        if(box.x+box.w >= DestRect.x && box.x + box.w < DestRect.x+DestRect.w)
        {
         box.x-=Xvel;
        }
        if(box.y+box.h >= DestRect.y && box.y + box.h < DestRect.y + DestRect.h)
        {
         box.y-=Yvel;
        }
        if(box.x <= DestRect.x + DestRect.w)
        {
         box.x-=Xvel;
        }
        if(box.y <= DestRect.y + DestRect.h)
        {
         box.y-=Yvel;
        }
   }



your check collision is good..

but I think the last two if-statements can use some work.
I think you can figure it out.
I'm guessing if you intersect something from the left side it will pull you all the way to the opposite side of the object?

and if you intersect something from the bottom it will pull you all the way up? (if y=0 is top of screen)

your check collision is good..

but I think the last two if-statements can use some work.
I think you can figure it out.


I have updated the collision some i think you meant like this. Now the Y axis works just fine but i go straight through on X collisions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(box.x+box.w >= DestRect.x && box.x + box.w < DestRect.x+DestRect.w)
	{
	 box.x-=Xvel;
	}
	if(box.x <= DestRect.x + DestRect.w && box.x > DestRect.x)
	{
	 box.x-=Xvel;
	}
	if(box.y+box.h >= DestRect.y && box.y + box.h < DestRect.y + DestRect.h)
	{
	 box.y-=Yvel;
	}
	if(box.y <= DestRect.y + DestRect.h && box.y > DestRect.y)
	{
	 box.y-=Yvel;
	}
Last edited on
in game.cpp:

1
2
3
4
if(keys[SDLK_a]){
  coord.x-=vel;
  BackgroundBox.x-=vel;
  Player->SetXvel(-3);


I noticed you're moving the tiles by two (vel) and and the player by three; they're off sync if I'm not mistaken.

This could have something to do with it.
I noticed you're moving the tiles by two (vel) and and the player by three; they're off sync if I'm not mistaken.

This could have something to do with it.


No it cant, i tried it anyway, Cause in the move function i calculate the relative coordinate from the screen to check all things (collision, bliting etc) I am not using absolute coordinates anywhere so it cant matter
Ok. I haven't looked through your code that well; It just looked like it could be a potential problem.

Rather than re-reading the code, let's try something else.

You said the Y movement works but the X movement does not, is this correct?

How does the X movement not work?

Does the player just not detect X collisions at all? (So you can move strait through a block)

Does the player detect X collisions (when you're moving to the left or the right), but instead of bouncing the player back to where he came, the block pushes him in the wrong direction (either left/right/up/down?)

Or does the player detect X collisions but pull the player all the way to the other side of the object rather than pushing him back to where he came?

Please describe exactly how the collision is working at the moment.

Something you can try in the mean-time is inverting the X and Y checks.

So check the Y first and then check the X.

See if this has any effect on which direction works and which direction does not work.

This will tell us a little bit more about where the problem is and how to fix it.
Ok.

The Y movement works just as you would expect, It goes up and down with no problems and collide with boxes just fine without flickering.

But, When colliding in X axis to a tile i just get slowed and pass through.

I tried inverting them but it didnt help.

I made a picture just to try make the problem clearer.

http://imgur.com/miZ5y,IIM9k Here it is

Please tell me if you need more information

EDIT: excuse me for my misspelling *To the right its meant to be
Last edited on
Ok, you've explained it better. But I'm not seeing where the problem is.

Would you zip or rar up your source files (.cpp and .h) as well as the pictures and upload them to a file hosting site then PM me the link?

We can still discuss the issue on this forum, but it would be easier if you just sent the files rather than posting all the source to the forum.
The collision is way better now, i have tried the best i could myself. But still i go through walls sometimes, i have no idea why, so i would still like you to take a look at it. There is also another problem. I cant move up if i push towards a wall.

I show example of a place where this strange collision happens.

http://imgur.com/zUyCl

By the way, just ignore the background flickering, It is because the picture isnt complete yet.

By the way, i havent tried this site but i hope its working good.

stoffe, I never received a PM from you.
Oh i'm sorry, something must have went wrong. I send it to you again
Problem fixed; I sent you the code although you are going to need to bring it back into visual studio. The files I changed were player.cpp and player.h. I also changed base.h but you shouldn't need that change, it was just so SDL would get included on my system.

Here is the new collision code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// moves movingObject outside of stationaryObject
void player::adjustPosition(SDL_Rect & movingObject, SDL_Rect const & stationaryObject)
{
    if(!CheckCollision(movingObject, stationaryObject))
        return;

    // what side of the stationaryObject does the movingObject collide on?
    bool intersectsTop = false;
    bool intersectsRight = false;

    if(movingObject.x > stationaryObject.x)
        intersectsRight = true;
    // y up is neg
    if(movingObject.y < stationaryObject.y)
        intersectsTop = true;

    // the height & width of the intersection rectangle
    short int height, width;

    if(intersectsTop)
        height = abs(stationaryObject.y - (movingObject.y + movingObject.h));
    else    
        height = abs(stationaryObject.y + stationaryObject.h - movingObject.y);
    if(intersectsRight)
        width = abs(stationaryObject.x + stationaryObject.w - movingObject.x);
    else    
        width = abs(stationaryObject.x - (movingObject.x + movingObject.w));

    bool moveInXDirection = height > width? true : false;

    // adjust moving object's position accordingly
    if(moveInXDirection)
    {   
        if(intersectsRight)
            movingObject.x += width;
        else    
            movingObject.x -= width;
    }           
    else
    {   
        if(intersectsTop)
            movingObject.y -= height;
        else    
            movingObject.y += height;
    }           
}


This is code I wrote for a game back in my game programming class.
It was in c#, I just ported it.

My group and I were having similar problems to what you are having when we tried to get objects to collide. They would get stuck or not go in the right direction. We didn't find the solution until we got out a white board and thought it out.

Since then I've invested in a white board for my room and it's actually sitting right by my computer. I use it all the time to figure things out. It's crucial to be able to visualize what you are programming when you are programming something visual such as a game, and white-boards help tremendously. The code you had worked sort of, but not all the way because it did not correctly account for which direction the moving object was coming from.

This code doesn't either, but it will work better than what you had. I actually had to write new code for a new game I'm making which correctly determines which way to move an object based on it's velocity. If you really want to get the best results, I think you definitely need to take in account the velocity of the moving object.

However, this code should work, and I don't think you will run into any problems with it, although if you do I'd be more than willing to share my new code. I'll just let you port it to your game. It's in c++.
That is so great. It actually sounds like a good idea to have a whiteboard. I often draw my things on paper.

I would probaly never have solved this myself. Thank you very much.

What do you think otherwise about my code. What should i think about in the future?

Is it good to use a base class to store all constants and common functions like i am doing?

What other advices could you give me?

And once again. Thank you so much!
Whiteboards are awesome, but paper will work well too. In fact I use paper for more detailed drawings while I use the whiteboard for about everything else visual.. like launching objects and determining how to bounce them.

You're welcome; everyone needs help at times including myself. That's why we're sharing knowledge with each other on this forum :)

You're code is much better then most of the code that I've seen on this forum. You are structuring your code in an object oriented manner which is vitally important as projects get larger. You are separating definitions in source files and declarations in headers.

I'm unsure why in game::game() you are using the magic data that you are.
base::base()
1
2
3
4
5
6
	coord.w = BackgroundBox.w = 800;
	coord.h = BackgroundBox.h = 600;
	fullscreen = false;
	Background = Load_Image("Background.png");
	Blocks = Load_Image("Blocks.png");
	this->LoadMap("test.TILEMAP");


when you have the definitions of SCREEN_WIDTH and SCREEN_HEIGHT in base.
Also, it would be better if Background.png, Blocks.png, and test.TILEMAP were stored in variables.

Here is how you would add a variable for the file names.

base.h:
1
2
3
4
5
6
7
class base
{
public:
	static const int SCREEN_WIDTH = 800;
	static const int SCREEN_HEIGHT = 600;
        static char const * const FILE_BACKGROUND;
        ....


base.cpp:
1
2
3
#include "base.h"
char const * const base::FILE_BACKGROUND = "Background.png";
...


I've never seen a base class like the one you have implemented.

Typically people pass values and have global/namespace variables. I'm not saying there's anything wrong with what you're doing, it's just different to me. It works, and it's organizing your code.

Best of luck on the rest of your game.
Topic archived. No new replies allowed.