Should be simple tile collision

Well, simple tile based collision... Why can't I do This?!?!?


White Rectangle Represents The Player
http://puu.sh/2TBhT //Floats in air
http://puu.sh/2TBif //Yet sometimes Sinks in blocks???

Green Square is a 10x10 box around the player using the player as the center.
http://puu.sh/2TBkO //About 5 Blocks from origin
http://puu.sh/2TBlT //About 50 Blocks from origin???

Please reply if you need more code

What here am I doing terribly wrong?
I don't need a solution, just a lead (however a solution would be greatly desired)

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
void Tq::Player::updatePosition(){

	Tq::Player::XDirection Xdir;
	Tq::Player::YDirection Ydir;

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
		Xchange -= 2;
		Xdir = Left;
	}
	else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
		Xchange += 2;
		Xdir = Right;
	}
	else
		Xdir = Player::XDirection::NoneX;

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
		Ychange += 2;
		Ydir = Down;
	}
	else if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
		Ychange -= 2;
		Ydir = Up;
	}
	else
		Ydir = Player::YDirection::NoneY;

	updateGravity(); //Simple gravity Really just Ychange -= 1;

	updateCollisions(Xdir, Ydir); //Seen in code block below

	Xposition += Xchange;
	Yposition += Ychange;

	Xchange = 0;
	Ychange = 0;

}


This is for the basic collisions with the tiles
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
	if(Xdir == Tq::Player::XDirection::Left)
		{
		
			if(Player_Map.accessData((Xposition / Tq::BLOCK_SIZE + Tq::BLOCK_SIZE) - 1,((Yposition) / Tq::BLOCK_SIZE)) != 0)
				Xchange = 0;
		
		}
	else if(Xdir == Tq::Player::XDirection::Right)
		{

			if(Player_Map.accessData((Xposition / Tq::BLOCK_SIZE) + 1,((Yposition + Tq::BLOCK_SIZE) / Tq::BLOCK_SIZE)) != 0)
				Xchange = 0;
		
		}

			if(Ydir == Tq::Player::YDirection::Up){

				if(Player_Map.accessData((Xposition / Tq::BLOCK_SIZE),(Yposition / Tq::BLOCK_SIZE)) != 0 || 
				   Player_Map.accessData((Xposition / Tq::BLOCK_SIZE) + 1,(Yposition / Tq::BLOCK_SIZE)) != 0)
					Ychange = 0;

			}
			else{

			if(Player_Map.accessData((Xposition / Tq::BLOCK_SIZE),((Yposition + Tq::PLAYER_HEIGHT) / Tq::BLOCK_SIZE)) != 0 || 
			Player_Map.accessData((Xposition / Tq::BLOCK_SIZE) + 1,((Yposition + Tq::PLAYER_HEIGHT) / Tq::BLOCK_SIZE)) != 0)
				Ychange = 0;

			}


This is for the green square (Block type 2 is green)
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
	//Calculate iBounds
		int iLowerBound;
		iLowerBound = (double(Xposition + (Tq::BLOCK_SIZE / 2)) / Tq::BLOCK_SIZE);
			for(int a = 4; a > 0; a--){
				if(!((double(Xposition + (Tq::BLOCK_SIZE / 2)) / Tq::BLOCK_SIZE) - a < 0)){
					iLowerBound -= a;
					break;
				}
			}

		int iHigherBound;
		iHigherBound = (double(Xposition + (Tq::BLOCK_SIZE / 2)) / Tq::BLOCK_SIZE);
			for(int a = 6; a > 0; a--){
				if(!((double(Xposition + (Tq::BLOCK_SIZE / 2)) / Tq::BLOCK_SIZE) + a > Tq::MAP_LENGTH)){
					iHigherBound += a;
					break;
				}
			}

	//Calculate eBounds
		int eLowerBound;
		eLowerBound = (double(Yposition + (Tq::BLOCK_SIZE / 2)) / Tq::BLOCK_SIZE) + 1;
			for(int a = 5; a > 0; a--){
				if(!((double(Yposition + (Tq::BLOCK_SIZE / 2)) / Tq::BLOCK_SIZE) + 1 - a < 0)){
					eLowerBound -= a;
					break;
				}
			}

		int eHigherBound;
		eHigherBound = (double(Yposition + (Tq::BLOCK_SIZE / 2)) / Tq::BLOCK_SIZE) + 1;
			for(int a = 5; a > 0; a--){
				if(!((double(Yposition + (Tq::BLOCK_SIZE / 2)) / Tq::BLOCK_SIZE) + 1 + a > Tq::MAP_HEIGHT)){
					eHigherBound += a;
					break;
				}
			}

			Player_Map.clearBlock(2);

	for(int i = iLowerBound; i < iHigherBound; i++){
		for(int e = eLowerBound; e < eHigherBound; e++){

			Player_Map.editData(i, e, 2);

		}
	}
Last edited on
Mybe this should be an else if?
1
2
3
4
5
6
7
else{

			if(Player_Map.accessData((Xposition / Tq::BLOCK_SIZE),((Yposition + Tq::PLAYER_HEIGHT) / Tq::BLOCK_SIZE)) != 0 || 
			Player_Map.accessData((Xposition / Tq::BLOCK_SIZE) + 1,((Yposition + Tq::PLAYER_HEIGHT) / Tq::BLOCK_SIZE)) != 0)
				Ychange = 0;

			}

The down condition in your collision check.
Last edited on
(Thanks for reply)

Alright I'll check that, the reason I made it an else is because otherwise you would passively sink through the ground because of gravity.

Could anyone just provide me with some psuedo code?
Last edited on
In Player::updatePosition you declare two local variables Xdir and Ydir. These variables are not visible outside the scope of Player::updatePosition, so these are not the same variables you are using in the code snippet you show for basic collision with tiles.
Thanks for the effort and reply

Could anyone just provide me with some basic psuedo code instead of attempting to solve this, It would be way simpler.

And I know that:

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
void Tq::Player::updatePosition(){

	Tq::Player::XDirection Xdir;
	Tq::Player::YDirection Ydir;

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
		Xchange -= 2;
		Xdir = Left;
	}
	else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
		Xchange += 2;
		Xdir = Right;
	}
	else
		Xdir = Player::XDirection::NoneX;

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
		Ychange += 2;
		Ydir = Down;
	}
	else if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
		Ychange -= 2;
		Ydir = Up;
	}
	else
		Ydir = Player::YDirection::NoneY;

	updateGravity();

	updateCollisions(Xdir, Ydir);

	Xposition += Xchange;
	Yposition += Ychange;

	Xchange = 0;
	Ychange = 0;

}


And then in:

1
2
void Tq::Player::updateCollisions(Tq::Player::XDirection Xdir, Tq::Player::YDirection Ydir){
...


I passed them into this function as the same name...
Last edited on
What would a (smooth) tile based collision system look like?
Found out the issue, the collisions were right, the rendering was wrong...
Last edited on
Topic archived. No new replies allowed.