10 Replies - 1063 Views - Last Post: 08 October 2012 - 02:56 PM Rate Topic: -----

#1 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

SFML tile simple slopes

Posted 08 October 2012 - 12:16 PM

I think the answer is really simple, but i just cant seem to find it. This video illustrates the problem:
http://s276.photobuc...008_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 the jrndev tutorials btw. Here is the code that i think is relevant

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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: SFML tile simple slopes

#2 rethc  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 76
  • Joined: 23-April 12

Re: SFML tile simple slopes

Posted 08 October 2012 - 01:10 PM

Post the code for CollisionSlope()
Was This Post Helpful? 0
  • +
  • -

#3 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: SFML tile simple slopes

Posted 08 October 2012 - 01:44 PM

View Postrethc, on 08 October 2012 - 01:10 PM, said:

Post the code for CollisionSlope()


No problem
bool Player::CollisionSlope(int sx, int sy, int &tsx, int &tsy,  Map& map)
{
	tsx = sx / TILESIZE; //map coordinates of the tile we check against
	tsy = sy / TILESIZE;

	Tile::TileType t = map.ColMapXY(tsx, tsy);

	//if we found a sloop we set aling y to the slope
	if( t== Tile::t_slopeRight){
		//sloperight -> /
		yPos = (tsy + 1) * TILESIZE - sx % TILESIZE - PLAYERHEIGHT - 1;
		return true;
	}
	else if(t == Tile::t_slopeLeft){
		//slopeleft -> \//
		yPos = (tsy+1) * TILESIZE - (TILESIZE - (sx % TILESIZE)) - PLAYERHEIGHT - 1;
		return true;
	}

	return false;
}



I can't seem to find the edit button and i hope double posting isnt too bad an offense, but i thought i should tell you Tile::TileType is an enum for the tile types(solid, non solid, right slope, etc).

Also, just let me know if you need CollisionVer, CollisionHor_Up, or CollisionHor_Down.
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: SFML tile simple slopes

Posted 08 October 2012 - 02:09 PM

line 76

if( CollisionVer(xPos + xVel + PLAYERWIDTH, yPos, tilecoord, map)){ //Collision on the right side of player


should it not be
 if( CollisionVer(xPos + xVel, yPos, tilecoord, map)){ //Collision on the right side of player




why add PLAYERWIDTH to that bit?

line 121

not sure why you are bitshifting there caan you explain that bit ?

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#5 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: SFML tile simple slopes

Posted 08 October 2012 - 02:24 PM

To be honest i dont know why i am bitshifting. The tutorial has that written so i just went with it. I looked up bitshifting and i coudlnt understand why it was being used, but i also dont know what operator should be used so i left it.

As for adding the playerwidth, its because i want to test the right side of the player against whatever.

After adding a column and testing that, ive discovered that collisionVer is never returning true. I dont think that affects slopes though because it works when the player goes from right to left and that part of the code is under:

"No slope collisions were found ->check for collisions with the map"
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: SFML tile simple slopes

Posted 08 October 2012 - 02:32 PM

View PostNano511, on 08 October 2012 - 09:24 PM, said:

To be honest i dont know why i am bitshifting. The tutorial has that written so i just went with it. I looked up bitshifting and i coudlnt understand why it was being used, but i also dont know what operator should be used so i left it.

As for adding the playerwidth, its because i want to test the right side of the player against whatever.

After adding a column and testing that, ive discovered that collisionVer is never returning true. I dont think that affects slopes though because it works when the player goes from right to left and that part of the code is under:

"No slope collisions were found ->check for collisions with the map"


Ahh I get it now,

this line again

Try

if( CollisionVer(xPos + xVel + (PLAYERWIDTH-1), yPos, tilecoord, map)){ //Collision on the right side of player

you may just be a pixel or two out when moving your sprite as you say moving left is no problem
as the hotspot is picking up nicely in this direction

It is just when you move right then there are problems probably because the hotspot is being missed somewhere maybe
hitting sometimes and sometimes not try the -1 thing see if that helps any..

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#7 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: SFML tile simple slopes

Posted 08 October 2012 - 02:33 PM

Okay, after fixing CollsionVer( I had parentheses in the wrong places ) I have a new but related problem.

There is a problem entering slopes upwards, from the left or right. Going down they are treated as slopes, going up they seem to be treated as solid and can not be entered unless you enter from above. Also, you cannot leave a slope upwards. Im thinking this has to do with the bitshifting being out of place but i havent looked into it yet.
Was This Post Helpful? 0
  • +
  • -

#8 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: SFML tile simple slopes

Posted 08 October 2012 - 02:38 PM

Ahah! after changing
int sx = xPos + (PLAYERWIDTH>>1) + xVel;


to

int sx = xPos + (PLAYERWIDTH-1) + xVel;


I can now enter slopes from the bottom, from left to right only, but still cannot leave them or enter them from right to left. I suspect i need to change all the >>'s to -'s.
Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: SFML tile simple slopes

Posted 08 October 2012 - 02:44 PM

Possibly... try it and see
Was This Post Helpful? 0
  • +
  • -

#10 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: SFML tile simple slopes

Posted 08 October 2012 - 02:48 PM

Nope. It diddnt make anything worse though :).

I think the problem is that when leaving a slope up towards a normal tile, the y is set to the bottom of the slope tile. Im not sure why though.
Was This Post Helpful? 0
  • +
  • -

#11 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: SFML tile simple slopes

Posted 08 October 2012 - 02:56 PM

Lol this is confusing but i may have found the actual answer. tilecoord is the Y of the tilecoordinate on the map(i think), and the problem is since the slopetile is y, and the above tile is on y-1, im just hitting the side of the tile at y-1. Im very close...

btw i still havent found the edit but i think if im quick it will add this to my previous post. I am getting an error when i try to subject one from tilecoord.

"int tile coord. Error: initial value of refernce to non-const must be an Ivalue"

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

Page 1 of 1