开发者

2D Box-Collisions, Platformer, my player "hovers" over blocks

If I set my player's "hspeed" variable (horizontal speed) to a slow enough speed, then when he goes over a gap of 16x16 blocks, he will fall through as planned. However if I set the 开发者_JAVA技巧"hspeed" variable high enough, he will go so fast that he hovers over it. All though it works when I set it to a slow speed, it's too slow.

Here's an example: http://img199.imageshack.us/img199/2685/demondj.png

Here's my collision code (loops through a list of blocks):

for(unsigned int i = 0; i<blocks.size(); i++){
Block &b = blocks.at(i);
if(!b.passable){
    //Check if we are on the sides
    if(y + height + vspeed >= b.worldY+2 && y + vspeed <= b.worldY+b.height)
    {
        //Right side
        if(x + hspeed <= b.worldX+b.width+1  && x + hspeed > b.worldX+b.width + hspeed-1)
        {
         x = b.worldX + b.width; hspeed = 0;
        }
        //Left side    
        if(x + width + hspeed >= b.worldX +1 && x + width + hspeed <= b.worldX + hspeed + 1)
        {
         x = b.worldX - width; hspeed = 0;
        }
    }

    //Check if we are on the top or the bottom
    if(x + width + hspeed >= b.worldX+1 && x + hspeed <= b.worldX+b.width-1)
    {
        if(y + height + vspeed >= b.worldY && y + height + vspeed <= b.worldY + vspeed + 1 && jumpstate=="falling")
            {
            y = b.worldY - height; jumpstate.assign("ground"); vspeed = 0;
            }

        if(y + vspeed <= b.worldY + b.height && y + vspeed >= b.worldY + b.height + vspeed + 1 && jumpstate=="jumping")
        {
        y = b.worldY + b.height; jumpstate.assign("falling"); vspeed = 0;
        }
    }

}

}

Do I have a problem in my collision code? Another problem is when I hit the bottom of a block, it's a bit glitchy. He's supposed to bounce right off it, and he does, but he jitters if he's moving and he hits the bottom of a block.


Ok, a number of things here:

1) Too many magic numbers. Not sure exactly what the +2, +1, etc. truely mean. I'd like to see those as constants. Also, I'm paranoid about order of operations so I would surround everything in parenthesis, just in case :)

2) Unsure of what types your vars are. Assuming floats, but they feel like ints.

3) The bigger problem, your code as designed will not properly handle "large" velocity well. Something traveling faster than the width/height of your collidable area won't be reliably detected. Nor can you easily with this type of collision code. This is one side effect you are already seeing and my guess is the fudge factor adds you have contribute to the rest.

Consider using a vector based collision system instead. Treat each edge of the box as a line vector. You can then do very simple math to determine if a point starts one one side of the line and ends on the other (sign of cross product) and sign of dot will tell whether the point was within the line segment. Also, it allows your collision objects to be any shape and even makes the math extendable to 3D rather easily. This sort of system also is parallel processing friendlier as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜