Tile Collision

Hi,

I got my tile collision working in my sandbox game, but when a player is say moving right and hits a block/platform, I’d like him to automatically move up this block like in Terraria.

                        BBBBB
                 PBBBB

BBBBBBBBBBBBB

B is block, P is player, player has hit block, and they continue pressing right key so now want them to go up the block onto the platform without having to jump, hope this makes sense?

Thanks

so the logic is pretty simple:

if you detect a block on the right of the player, AND the player is pressing right:
you start code to move the player “up and over” (for platformer a simple call to trigger the “jump” action should do)

Minecraft has this too (half-slabs, but stairs are a bit of an exception as they’re specially handled). Each entity has a “stepFactor” which is how much of a block they can step up without jumping. For players and general mobs it’s 0.5F. As orangepascal said, you can detect if the player is pressing right onto the platform. Another option is when you’re resolving collision detection to also move the player onto the top of the platform if you’re colliding directly with the platform.

Got this working with the code below (snippet):


		Vector3 poss = new Vector3(camera.position);
		poss.y-=8;
		// Need to check if not jumping as we need to jump Y amount before any downward force happens
	 	if(!bRight)  // pressing right key, if so, don't check for collision downwards
		{
	 		if(checkCollision(poss))
			{
				camera.position.y -= 3;  // scroll map down
			}
		}
		

		if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
			
			player.moveRight(1);
			Vector3 po = new Vector3(camera.position);
			po.y-=TILEHEIGHT/2;
			
			if(checkCollision(po)) {			
				camera.position.x += 2; // scroll map right
				bRight = false;
			}
			else // collision - how do we go up - go up until no collision
			{	
					bRight = true;				
					camera.position.y += 3; // scroll map up
			}
		} 
	

and collision code:


	private boolean checkCollision(Vector3 position)
	{
		Vector3 screenSpace = toScreenSpace(position);
		int x = Math.round(screenSpace.x);
		int y = Math.round(screenSpace.y);
		BlankEntity entity = this.worldMap.getBlock(x, y);
		if(entity ==null) return true;
			return false;
		
	}
	// Some helper methods
	private Vector3 toScreenSpace(Vector3 position) {
		Vector3 v = new Vector3(0, 0, 0);
		v.x = position.x / TILEWIDTH;
		v.y = position.y / TILEHEIGHT;
		return v;
	}


This now works :slight_smile: Still to put jumping in then digging and some lighting…

Thought it was working well, but now well collide say with a big wall, moves all the way up it, just want it to move up to a maximum amount…

   T
   BBBBBBBBBB
   BBBBBBBBBB
 PBBBBBBBBBB

BBBBBBBBBBBBBB

Above P (player) when pressing right would go all the way up the wall and end up at T, now what I want, just want to go up one block…
Would this require me to save the players position when collsion occured on right key?

Thanks

I guess the only thing you need to do here is to check the Block diagonally above you:

   BBBBBBBBBB
   XBBBBBBBBB
 PBBBBBBBBBB

BBBBBBBBBBBBBB

In this example you need to check the “X”. If “X” is empty or a non-blocking Block, you can move, else you can’t.

Got this working now :slight_smile:


		if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
			bRight = false;

			player.moveRight(1);
			Vector3 po = new Vector3(camera.position);
			po.x-=TILEWIDTH/2;
			po.y-=4;//TILEHEIGHT/2;
			if(checkCollision(po)) {
				camera.position.x -= 2;  // scroll map left
				bLeft = false;	
			}
			else // collision, move up
			{
				if(displayBlockDiagAbove(false,camera.position))
				{
					bLeft = true;
					camera.position.y += 3;
				} else
				{
					bLeft = false;
				}
			}
		}
		
		if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
		
			bLeft = false;
			
			player.moveRight(1);
			Vector3 po = new Vector3(camera.position);
			po.y-=4;//TILEHEIGHT/2;
			
			if(checkCollision(po)) {			
				camera.position.x += 2; // scroll map right
				bRight = false;
			}
			else // collision - how do we go up - go up until no collision
			{	
				// is it clear above block? (water, cave, blank)
				if(displayBlockDiagAbove(true,camera.position))
				{
					bRight = true;				
					camera.position.y += 3; // scroll map up
				}
				else
				{
					bRight = false;
				}
			}
		} 



	private boolean displayBlockDiagAbove(boolean dir, Vector3 position)
	{
		int val = dir == true ? 0 : -1;
		Vector3 screenSpace = toScreenSpace(position);
		int x = Math.round(screenSpace.x)+val;
		int y = Math.round(screenSpace.y)+1;
		BlankEntity entity = this.worldMap.getBlock(x, y);

		if(entity!=null)
		{	
			if(entity.toString().equals(".") || entity.toString().equals("CAVE")
					|| entity.toString().equals("WATER") )
					
			{
				return true;
			}
		}
		else // entity is null
			return true;
				
		return false;
	}


The method displayBlockDiagAbove did the trick - as Springrbua suggested, check block diagnally above.

Thanks