I am making a Java platform game (I have created many top-down games, but nothing with gravity like a platformer) and I have implemented terminalVelocity, etc.

My problem is that, say when the player is falling at a rate of 8 pixels, I check collision for the player 8 pixels ahead before it moves, and if there is a collision the player does not move. But, say that there are 4 pixels until a block but the player is moving at 8 pixels, the collision detection stops the player because 8 pixels ahead there is a block, but the player hovers because there is a 4 pixel gap.

How would I make the player fall those extra 4 pixels?


Well what I do for my 2D game is that inside of checking before the players moves, I deal with collision after the player moves. Before I used to run into problems like yours because of the fact that when you check before hand, you stop the player from moving. Here's what you have to do, let's say that there is a block at some x, y, width and height

........o.......//o is the player
.......[].......//The block

We'll keep making the player fall and after we do so we check if it is inside of a block

player.y += 8;
if(player.x < block.x + block.width && block.x < player.x + player.width && player.y < block.y + block.height && block.y < player.y + player.height)

if the player is inside of the block, we'll push it upwards

player.y -= (block.y + block.height);

Now it's right onto of the block and no gap :D

Am currently making a platform - sandbox game with java for android and this is how I deal with the collision.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.