Tile-Based Collision\Physics Problem
#1 Members - Reputation: 101
Posted 31 March 2012 - 03:48 PM
so I'm working on a tile-based game: 16x16 player, 16x16 tiles. the hitbox for the player is 16x16.
I got a problem that I have no idea how to solve, I tried different type of collisions to do it, basically the player's velocity is so high that he just flys above 1-tile holes..
let's say i'm trying to jump into 1 space hole vertically horizontally or whatever and I can't because of my velocity, even if it's very low, 1 pixel off is enough to make him not fit inside the hole, and I have no idea what to do, help?
#4 Members - Reputation: 527
Posted 31 March 2012 - 08:19 PM
Alternatively, you could do some kinda funky stuff, like, if you're going down and the bottom edge of the collision box is going to cross a tile boundary, then move down to that tile boundary (without bothering to check collisions since you couldn't hit anything without changing tiles), and then do the X motion, and then do the rest of the Y motion. That way regardless of how fast you're falling, you'll always be lined up properly to squeeze into small gaps in walls. Not sure how to apply this to gaps in floors/ceilings at the same time though.
#5 Members - Reputation: 2142
Posted 31 March 2012 - 10:38 PM
Pseudo code:
DeltaX, DeltaY hold the intended movement.
while ( ( DeltaX != 0 ) || ( DeltaY != 0 ) )
{
if ( DeltaX > 0 ) Move1PixelRight
if ( DeltaX < 0 ) Move1PixelLeft
if ( DeltaY > 0 ) Move1PixelDown
if ( DidNotFallYet ) CheckIfPlayerCanMoveDown1Pixel
}
#6 Members - Reputation: 689
Posted 01 April 2012 - 12:27 AM
You need to use smaller step, such as "tileSize - 1", to move, until the total steps meet the velocity.
If the velocity is 20, and your tile size is 16, you first move 15 pixels, then move another 5 pixels.
@Endurion,
Isn't moving one pixel too small and may hit the performance? Any special reason?
http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.
#8 Members - Reputation: 101
Posted 01 April 2012 - 11:43 AM
*edit: i tried reducing the hitbox and it still flys above the gaps, i reduced it to about 3 pixels in every side
#9 Members - Reputation: 102
Posted 04 July 2012 - 03:34 AM
wqking got the best answer.. I think you have to test if you must use "tileSize - 1" instead of "tileSize"..
now when u find a hole underneath, align the player to it and make it fall, whatever gravity u have.
Edited by SloydD, 04 July 2012 - 03:38 AM.
#10 Members - Reputation: 690
Posted 05 July 2012 - 02:05 AM
#11 Members - Reputation: 288
Posted 05 July 2012 - 09:13 AM
bool MasterEntity::collisionDetection(SDL_Rect *A, SDL_Rect *B)
{
bool colFlag = false;
int aX, aXW, aY, aYH, bX, bXW, bY, bYH;
//collision points of object A
aX = A->x;
aXW = A->x + A->w;
aY = A->y;
aYH = A->y + A->h;
//collision points of object B
bX = B->x;
bXW = B->x + B->w;
bY = B->y;
bYH = B->y + B->h;
if(aXW > bX && aX < bXW && aYH > bY && aY < bYH)
{
colFlag = true;
}
return colFlag;
}
From your initial post when you wrote "
basically the player's velocity is so high that he just flys above 1-tile holes.." i'm brought back to a problem i had on a game i developed a few month back. Are you sure that you are hit testing the NEXT position the character till take and not the one it currently has.
[left]Lets say you char has these values. X = 5; Y = 5; and you press your left key which will increment X by 5 and Y by 0. To have a successful collision-detection you need to make sure that you are searching for a collision in X = 10 and Y = 5 BEFORE you let your character move. If there is a collision simply don't move him, if not feel free to let him move.#12 Members - Reputation: 288
Posted 05 July 2012 - 09:15 AM
If I've gotten it right this time i'd suggest you make a function to detect if the character has 2 blocks near him (with further collision detection) and if he has just let him slip through and don't care about the collision restriction.






