3 Replies - 537 Views - Last Post: 07 October 2012 - 06:25 PM Rate Topic: -----

#1 Tom_Leonardsson  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 07-August 11

[SDL & C++] SDL_Rect won't stay at the right spot.

Posted 05 October 2012 - 05:36 PM

I'm trying to add collision to the walls in my game but the hitbox moves.

Here's where I make the SDL_rect
           SDL_Rect walls[30];
           walls[0].x = mapX;
           walls[0].y = mapY;
           walls[0].w = 32;
           walls[0].h = 228;



Here's my collision code for the wall.
        if(keysHeld[SDLK_RIGHT])
           {
              for(int i=0;i<30;i++)
              if(collision(&playerC,&walls[i]))
              playerX += 3;
           }


The camera and movment
         	if(keysHeld[SDLK_DOWN])
		{
            batY -= 1;
			if(playerY>= 300)
				playerY+=1;
			else
				camera.y+=1;
		}
		if(keysHeld[SDLK_RIGHT])
		{
		    batX -= 1;
			if(playerX<=640/2)
				playerX+=1;
			else
				camera.x+=1;
		}
		if(keysHeld[SDLK_UP])
		{
		    batY += 1;
			if(playerX<=100)
				playerY-=1;
			else
				camera.y-=1;
		}
		if(keysHeld[SDLK_LEFT])
		{
		    batX += 1;
			if(playerX>=640/2)
				playerX-=1;
			else
				camera.x-=1;
		}


(BatX & Y, just ingnore that.)

Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: [SDL & C++] SDL_Rect won't stay at the right spot.

#2 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,758
  • Joined: 19-February 09

Re: [SDL & C++] SDL_Rect won't stay at the right spot.

Posted 05 October 2012 - 06:46 PM

Should this not be -= 3?

5	      playerX += 3;


Was This Post Helpful? 0
  • +
  • -

#3 Tom_Leonardsson  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 07-August 11

Re: [SDL & C++] SDL_Rect won't stay at the right spot.

Posted 06 October 2012 - 03:28 AM

Heres's a picture of when I start and when the Rect is at the right spot. (the blue thinh)
Posted Image

But when I move, it moves
Posted Image

Please help.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,758
  • Joined: 19-February 09

Re: [SDL & C++] SDL_Rect won't stay at the right spot.

Posted 07 October 2012 - 06:25 PM

It looks as if the background (walls etc.) has moved.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1