Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have searched for this question but was unable to turn up any results - 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 the player fall those extra 4 pixels?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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. Heres what you have to do, lets 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)
{
//reaction
}

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

if you like my answer please mark it as the accepted answer and +rep

share|improve this answer
    
Thanks, I accepted! That helps a lot! –  CodeNMore Apr 8 '14 at 23:39
    
That's graat to hear. Did it work for you? –  UselessQuestionMaster Apr 8 '14 at 23:40
    
Worked perfectly, I modified the way the player gets pushed up for the way I do collision, but using your method of moving first then pushing the player up! –  CodeNMore Apr 8 '14 at 23:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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