[solved]Gravity/jump collision detection

Hey guys.

I currently have gravity/jumping working in my game etc but I have a few issues. My collision detection usually works by first moving the bounding box of the player, checking for the collision and if it collides then it doesn’t allow the player to move. This has served me pretty well in the past but right now I have these issues.

My currently implementation is pretty bad as the player sometimes gets stuck in the ground when falling. Sometimes the collision detection will make him float above the blocks by a certain amount. Due to this it then become very difficult to implement a jumping collision box.

I was just wondering if anyone has an ideas about how to fix this. As I said, the collision detection usually works find but this is my first platformer style game so I might need to use a different type of system

EDIT: Okay so i’ve found this YouTube tutorial which looks pretty good https://www.youtube.com/watch?v=Zmkuk9OlObw Sadly i’m about to run off to study for my exam this afternoon but i’ll take a look later!

Collision detection usualy has 2 parts: The detection and the response.
Your problems are probably response-related:
You calculate the next position of the player and if there is a collision, you don’t move. Instead of stoping the movement, you could move to the last possible position.
That means, that if you detect a collision on the y-Axis, the y-Movement should be limited to the collision-point, while the x-Movement can be executed completely.
This should solve the “float above the ground”-Problem, as well as the “stuck in floor”-problem.
Also, if you need more physic related things you might think about using a physics engine like Box2D.

You can able your character to jump after check the ground collision.

if (isCharacterCollidedWithGround)
{
	//-- No gravity applied, maybe this can solve your "stuck" problem
	if (button_jump_pressed)
		jump();
}
else
{
	applyGravityToPlayer();
}

//If you have a great collision check with the ground, you don't need to remove the gravity

[quote=“SauronWatchesYou,post:1,topic:53797”]
If you have a boolean that doesn’t allow the player to move, you can’t jump or do anything else. Can you explain a bit more what’s your game/character?

@CraftM I guess by “doesn’t allow the player to move” he means, that the “planned” movement is canceled, as it is not possible / would result in a collision. So in the current frame, where the collision happens, the player won’t move. In future frames he could move, if the “planned” movement is possible.

So I have the code working, I followed the same style of collision as the tutorial that I linked in the first post and that seems actually better than the way I usually do collision detection so I think i’ll stick to using stuff like that, at least I learnt something new!

The only issue I have no is allowing the player to fit under tiles when one is above. The player is 32 in width/height as well as the tiles and as I use padding around the player it means he is too big to fit under. The simplest fix I can think of it just making the player smaller and allowing him to move under like so

Instead of change the player size, you can change the rectangle size/position (assuming you are using it for collisions…).

Of course :smiley: my player just seems a little too big now so it seemed to make sense on making him smaller

Also I like to point out: This is a common issue.
In every game, EVERY GAME, you can get stuck in the walls or floors, intentionally or unintentionally (leading to out of bounds glitches)

When I wrote the physics code for may game I defined gravity, then collision detection, X and Y, which are different… especially walking up and down diagonal terrain is a terrible pain…
So after all that I was like "there is no way you would still get stuck in the wall now. Of course it happened; So then I wrote more code after that algorithm: ok so what if after all that the player is actually stuck in the structure, check for that, push him out.
Very common thing also often misused by speedrunners: if you do manage to get into the wall you will get pushed out, many times up the top.

Eventually now I have: Gravity, Y Logic, X Logic, special code for jumping, sliding, walking down slopes, walking up slopes, jumping while on slopes and finally TWO fail-safes. Its a nightmare and a thing of beauty at the same time.
Also I have moving platforms which can take your vertically and horizontally, and also being able to drop through certain platforms and jump through them and then land on them. All of these special cases add more exceptions to the code.

Just think: gravity pushes the player down, now he is standing on a platform pushing him up, so the kinda cancel each other out, but thats not how it works… so getting the player to fall through the platform or get stuck is very simple. if you now add things like jumping WHILE on the platform… yea.
I am happy not having to do that again. But at the same time I am very much against using a physics engine unless you really know why you want it or your game is about playing around with physics like angry birds or something.
Actually if we Java programmers could use Havocs that would be fine, I just have big problems with Box2D… Basically if you want unrealistic / interesting things to happen, dont use Box2D, which imo are all games minus those that are specifically about playing around with physics

Nice post Cero! I personally don’t like using a physics engine myself, I really like to try and understand what is happening and if things are hidden away from me I just feel so… cheated? Idk haha. Each to their own!

I have an issue that I have no idea about guys and was wondering if anyone here has every come across something like it before. The video below will explain the issue and I’ve also posted some code snippets :slight_smile:

VKS5l8h9OEY

The rectangle bounds creation for testing purposes:


if(tile.getProperties().containsKey("Platform")){
     mapBoundsList.add(new Rectangle(tx, ty + layer.getTileHeight() / 2, layer.getTileWidth(), layer.getTileHeight() / 2));
     }else{
	       mapBoundsList.add(new Rectangle(tx, ty , layer.getTileWidth(), layer.getTileHeight()));}

If the bottom of the players bounds intersects the block bounds:

player.setYPos(mapBoundsList.get(i).y + mapBoundsList.get(i).height / 2 + player.getPlayerHeight() / 2 + 1);

I believe the issue lies somewhere within those snippets :persecutioncomplex: I just can’t figure it out

well first of all draw the rectangle boxes on screen, that would help :smiley:

Your wish is my command ;D

player box would help too

but you have to check in the running game, if either the box is too high or what…

does the player sink INTO the box or does he simply get pushed out too much ?

He gets pushed out by the looks of things. I’ve made a new video showing the boxes and the problem :slight_smile:

rT1ybL5T2z4

EDIT: FIXED! I changed this line of code:

player.setYPos(mapBoundsList.get(i).y + mapBoundsList.get(i).height / 2 + player.getPlayerHeight() / 2 + 1);

to

player.setYPos(mapBoundsList.get(i).y + mapBoundsList.get(i).height);

I have a slight issue with the player being able to get through the right side of that platform but i’m sure I can fix that