### 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???

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

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````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
 ``1234567891011121314151617181920212223242526272829`` `````` 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)
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` `````` //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?
 ``1234567`` ``````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

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:

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````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:

 ``12`` ``````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.