Sidescrolling Plattforms

Hello.

As Plattforms I use lines. the lines are horizontal at any given height.

  ----------

O

O : player
= : ground (also line)

  • : plattform

Now I used to place a rectangle around the player and ask if the line intersects the rectangle, but this led to the player just touching the plattform and immediatly being placed on the plattform. even if he wanted to walk beneath it and it wasn’t very high.

So I tried telling him, to only place the player on the plattform if the Y-Axis value was the same as the Y-Axis value of the line.

Code:

`
Rectangle r3 = player.getBounds();

	for (int j = 1; j<boxes.size(); j++) {
		Line2D linie = (Line2D) boxes.get(j).makeLine2D();
		
		if (r3.intersectsLine(linie) && player.getY()+player.getImage().getHeight(null) ==  linie.getY1() ) {
			lineIntersected = true; //its a global boolean
			player.setDy(0);         //stop falling
			player.setY(linie.getY1()-player.getImage().getHeight(null)); //place player on plattform
			player.setCurrentY( (int) linie.getY1());                             //this is just for animations, so it knows where to place them
			
		} else if (!r3.intersectsLine(linie) && lineIntersected) {
			lineIntersected = false;
			
			player.setDy(-1); // move down (falling)
		}
		
		
		
		
	}`

This is part of my CheckCollision routine.

It’s total bullox and only works half ways if I delete the “else if”. Can anybody help me? Maybe you got a better idea how to make plattforms. they need to be placeable where I want them.

I’d be so very greatful :smiley:

I think the problem here is that you are checking if you have intercepted the line, moved to your position and then checked the line to see if it has been intercepted again when you remove the else if. I would make a variable that holds the answer if the line is intercepted and use that for both if statements.

jumping upwards, the moment you collide you are teleported to the top.
right now the platform tells the player where the player is.

player should just be a rectangle floating in space, until it gets gravity or movement commands or bumps into stuff.

the idea is to split it up more :

the player is continually affected by gravity Dy - his natural state is to fall
but, If there is floor under the player, he stays where he ( collides and ) stays on the platform
otherwise gravity takes place and he is dropping

you could for example test only the bottom of the player box downwards, to check whether the feet hit ground.
the collision tells you where the ground is, and the player accepts that he is not falling anymore

Okay. I changed that the plattforms tell the player where he is.
Also moved around some of the code.

Now he always falls. But stops falling if he is told to. But then falls again when leaving a plattform.

It works now :slight_smile:

Thanks for the tipps. :smiley: