SDL easy help.

http://pastebin.com/FHdvFyGX

here is the source that i have made.
it works good.The problem is that when the player go up to obstacles nothing happens and he continue to walk.

So the problem is maybe in the checkcollion function or in the if stetments.

i have spend 1 hour in ti.
Could help me?
You don't have any obstacles on the top, so there is nothing to stop the player from going up.

The CheckCollision collision function doesn't work correctly.
Last edited on
May I suggest considering to let the functions take SDL_Rect as parameters? It clears up the code quite a bit
guys what you mean?
What should change?

I appreciate that you help me?
please ...
man the obstacles did not stop him when he goes right or left btw.
so?
CheckCollision() looks ok to me
When you move the player you check for collisions twice. I don't really see a point in doing that
what you mean?
It works good in your pc?
Man can add me in skype:
hepic.hepic

so what should do?
My mistake. CheckCollision seems to work.

The reason you can go outside the screen to the left and up is because you are limiting the cooridnates to negative values instead of 0 on lines 316-324. This should avoid that problem.
1
2
3
4
5
6
7
8
9
if(dstX1<0)
{
	dstX1=0;
}

if(dstY1<0)
{
	dstY1=0;
}


You can't go all the way to the right or down because ScreenWidth and ScreenHeight is smaller than the size of the window.
Peter87 what mean?
I am not going out of the screen.
The problem is that the obstacles dont stop my player to pass from there.
I thought lines 316-338 was an attempt to make the player stay inside the screen.
it works good.The problem is that when the player go up to obstacles nothing happens and he continue to walk.

I just noticed, all your obstacles have an Y of at least 910. Given that you can't go past ScreenHeight because of line 335, you player never collides simply because there are no obstacles in the area in which it can move

I seriously advice to write a CheckCollision that takes two SDL_Rect. It makes the code a lot more readable

1
2
3
4
5
6
7
8
9
10
11
12
13
bool CheckCollision(SDL_Rect& a, SDL_Rect& b)
{
  if(a.x+a.w < b.x)
    return false;
  else if(a.x > b.x+b.w)
    return false;
  else if(a.y+a.h < b.y)
    return false;
  else if(a.y > b.y+b.x)
   return false;

  return true;
}

Make an SDL_Rect out of player coordinates and call the function with it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// After you load the image
SDL_Rect playerRect = {dstX1, dstY1, heroe->w, heroe->h};

// Other code

if(instr.CheckCollision(playerRect, obstacle)
            dstY1+=2;
if(instr.CheckCollision(playerRect, obstacle1)
            dstY1+=2;
if(instr.CheckCollision(playerRect, obstacle2)
            dstY1+=2;
if(instr.CheckCollision(playerRect, obstacle2)
            dstY1+=2;
if(instr.CheckCollision(playerRect, obstacle4)
            dstY1+=2;


I thought lines 316-338 was an attempt to make the player stay inside the screen.

They are. The player sprite can go out of the screen with skarla's code
Last edited on
Hmm it sounds logic,so what should change?
Topic archived. No new replies allowed.