Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Tile-Based Collision\Physics Problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 David Zeek   Members   -  Reputation: 101

Like
0Likes
Like

Posted 31 March 2012 - 03:48 PM

Hello guys this is my first post and I hope I'm posting it in the right place..

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?

Sponsor:

#2 kaspe   Members   -  Reputation: 100

Like
0Likes
Like

Posted 31 March 2012 - 04:13 PM

If I understod right: try 8x8 (or similar) hitbox from the center of player?

#3 David Zeek   Members   -  Reputation: 101

Like
0Likes
Like

Posted 31 March 2012 - 04:29 PM

I don't want to reduce the hitbox, I want to keep it the way it is but get the player to somehow still fall through holes without reducing the hitbox

#4 DekuTree64   Members   -  Reputation: 986

Like
0Likes
Like

Posted 31 March 2012 - 08:19 PM

Most games just ignore this problem. If it's a tight fit and you're moving fast, you slide right over. If you want to improve it just a little bit, then you could divide the velocity by 2 and them move twice, effectively doubling the frame rate so you can be moving at twice the speed before missing.

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 Endurion   Crossbones+   -  Reputation: 3687

Like
0Likes
Like

Posted 31 March 2012 - 10:38 PM

Divide and conquer: Move the objects pixel per pixel. If there are more than one possible movement axis (x and y as in your example) check both in a loop.

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
}

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#6 wqking   Members   -  Reputation: 756

Like
1Likes
Like

Posted 01 April 2012 - 12:27 AM

You can't use the velocity to move directly.
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.


#7 wolfscaptain   Members   -  Reputation: 200

Like
-1Likes
Like

Posted 01 April 2012 - 02:37 AM

Why not just make the player a little smaller?

#8 David Zeek   Members   -  Reputation: 101

Like
0Likes
Like

Posted 01 April 2012 - 11:43 AM

I don't want to make the player smaller, and I tried all these stuff like dividing the velocity, like merging the x & y velocity and move 1 pixel at a time but it's like very annoying and no method works.. like I'm always led into problems and I know what causes them but I don't know how to fix it.. i feel like i'm asking for something that was never done before lol i know it's weird that everything is 16x16 and everybody tell me to change the hitbox but i really want everything to be 16x16..

*edit: i tried reducing the hitbox and it still flys above the gaps, i reduced it to about 3 pixels in every side

#9 SloydD   Members   -  Reputation: 102

Like
0Likes
Like

Posted 04 July 2012 - 03:34 AM

Making the player smaller wont help!.. why not make tetris instead of this game..

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 M6dEEp   Members   -  Reputation: 897

Like
0Likes
Like

Posted 05 July 2012 - 02:05 AM

One idea is instead of using one 16x16 hit box, you can use 4 4x4 hit boxes and only react to collisions that occur to 2 or more. This gives you the same size as before and allows you to have "soft" collisions. This would be no more complex than the other "solutions" and allows you finer grain control over collision response.

#11 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 05 July 2012 - 09:13 AM

This is how my collision detection works in the game i'm creating in SDL.

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 Tallkotten   Members   -  Reputation: 288

Like
0Likes
Like

Posted 05 July 2012 - 09:15 AM

I just read your post and i think i might have gotten it wrong in my previous post...

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.

#13 ankhd   Members   -  Reputation: 1364

Like
0Likes
Like

Posted 05 July 2012 - 08:41 PM

Why not just use a hit box on the hole when player hits hole move player to centre of the hole and and let him full through.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS