### SFML tile slopes

I think the problem is nothing difficult i just cant seem to find it. This video illustrates the problem:
http://s276.photobucket.com/albums/kk15/stkguy/?action=view&current=20121008_1502_49.mp4

As you can see, when moving across the map from right to left, all goes well. BUT when going left to right, and the player goes from a slope tile up to a normal tile, he goes through the normal tile!

Im using jnrdev's tutorials btw. Here is the code that i think is relevant
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124`` `````` void Player::CollisionDetectionMap( sf::RenderWindow& window, Map& map) { //check for slopes(only if moving down) if( yVel > 0 ){ int tsx, tsy; // slope tile coordinates int sx = xPos + (PLAYERWIDTH>>1) + xVel; // slope check point x coordinated if( CollisionSlope(sx, (yPos + PLAYERHEIGHT), tsx, tsy, map) ){ // we entered a slope xPos += xVel; // move on //y has been set by CollisionSlope UnlockJump(); //we hit the ground - player may jump again yVel = 1; // test against the ground again in the text frame slope_prevTileX = tsx; slope_prevTileY = tsy; return; } else{ // were not on a slope this frame - check if we left a slope //-1...we diddnt move from slope to slope //0...we left a slope after moving down //1...we left a slope after moving up int what = -1; if(map.ColMapXY( slope_prevTileX, slope_prevTileY) == Tile::t_slopeRight){ if( xVel > 0 ) // sloperight + xvel > 0 = we left a slope after moving up the slope what = 0; else what = 1; // we left after moving down the slope } else if(map.ColMapXY(slope_prevTileX, slope_prevTileY) == Tile::t_slopeLeft){ if( xVel < 0 ) // sloperight + xvel > 0 == we left a slope after moving up the slope what = 0; else what = 1; // we left after moving down the slope } if( what!= 1){ //if we left a slope and now are on a slope int sy; if( what == 1){ yPos = tsy*TILESIZE - PLAYERHEIGHT - 1;// move y to top of the slope tile sy = yPos + PLAYERHEIGHT; } else{ yPos = (tsy+1) * TILESIZE - PLAYERHEIGHT -1; //move y to the bottom of the slope tile sy = yPos + PLAYERHEIGHT + TILESIZE; } //Check for slopes on the new position if( CollisionSlope(sx, sy, tsx, tsy, map ) ){ // slope on new pos (entered a slope after leaving a slope) xPos += xVel; UnlockJump(); yVel = 1; slope_prevTileX = tsx; slope_prevTileY = tsy; return; } } } } //no slope collisions were found ->check for collisions with the map //Check for collisions with the map int tilecoord; //x axis if(xVel > 0){ //if moving right if( CollisionVer(xPos + xVel + PLAYERWIDTH, yPos, tilecoord, map)){ //Collision on the right side of player xPos = tilecoord*TILESIZE -PLAYERWIDTH - 1; }// Move to the left edge of the tile else{ // no collision xPos += xVel; } } else if( xVel < 0){ //if moving left if( CollisionVer(xPos + xVel, yPos, tilecoord, map)) //Collision on the left side of player xPos = (tilecoord+1)*TILESIZE+1; //move to the right edge of the tile else // no collision xPos += xVel; } //y axis if( yVel < 0 ){ //moving up if( CollisionHor_Up( xPos, (yPos + yVel), tilecoord, map)){ //Collision on the top of player yPos = (tilecoord+1) *20 +1;//Set y to bottom of the above tile yVel = 0; // stop moving the player } else{ // no collision yPos += yVel; yVel += GRAVITATION; } } else{ //moving down or on ground if( CollisionHor_Down( xPos, (yPos + yVel + PLAYERHEIGHT), tilecoord, map)){// on ground yPos = (tilecoord*TILESIZE) -PLAYERHEIGHT -1; //yPos = top of tile below yVel = 1; //1-> we test against the ground againt in the next frame //0-> we tvest against the ground in the next+1 frame UnlockJump(); } else{ //falling or in air yPos += yVel; yVel += GRAVITATION; if( yVel >= TILESIZE-1) //might fall through a tile if speed is higher than tile yVel = TILESIZE -1; lockJump = true; // cannot jump while falling } } slope_prevTileX = (xPos + (PLAYERWIDTH>>1)) / TILESIZE; slope_prevTileY = (yPos + PLAYERHEIGHT) / TILESIZE; }``````
Last edited on
Topic archived. No new replies allowed.