Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / 2d gravity in platform game

Author
Message
edge ogg
14
Years of Service
User Offline
Joined: 20th Feb 2010
Location: cornwall
Posted: 2nd May 2010 23:09 Edited at: 3rd May 2010 18:09
Hi i'm having problems with the 2d gravity in my 32x32 tile based platform game
when the player sprite falls onto first platform my collision code stops the player ok but as the player falls onto the
next platform down it does'nt stop until 2 pixels into the platform by the time the player sprite falls onto the
forth platform down it is half way through the platform
I assume this is to do with the the time it takes the program to loop. my code below

spy being the player sprite y
I have based my game on the 2d tile scrolling tutorial by Mike Shah
but he dosent say anything about gravity in his tutorial
so my question is what is the best way to apply simple gravity to
a tile based game ?
thanks in advance
FIXED IT THANKS FOR YOUR HELP
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 31st May 2010 01:10
I know you said you fixed it, but this is a common problem, so I will provide a way to fix the problem for anyone looking at this thread later.

You see, when you find collision, you stop moving, but when you check for collision, you have already gone 'velocity' pixels into the tile(thats why the more tiles you passed, the farther you went. Your velocity was increasing). So when you find collision with a tile, set the y position to the top of that tile. That should solve your problem. Here is code example:

y = y + velocity; // Add the velocity to the y position
if( getMapCollision( x/TILE_SIZE, y/TILE_SIZE ) ) // Check for collision
y = (int(y/TILE_SIZE)*32); // if there is collision, set the position to the top of the tile

New Site! Check it out \/

Login to post a reply

Server time is: 2024-03-28 11:58:07
Your offset time is: 2024-03-28 11:58:07