May 14, 2013 at 10:36pm UTC
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 May 14, 2013 at 10:50pm UTC
May 15, 2013 at 2:00am UTC
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 May 15, 2013 at 2:00am UTC