c++ - 2D Box Collision - Is this right? -


well i've got 2d box collision code loops through every block in list called "blocks" , checks if i'm near sides , whatnot.

it works except bottom of block. when i'm jumping towards bottom want player "bounce" off. this, glitchy. it's hard explain hoping guys possibly spot out what's wrong bottom collision code.

here's entire thing:

for(unsigned int = 0; i<blocks.size(); i++){ block &b = blocks.at(i); if(!b.passable==true){     //check if on 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-2)         {          x = b.worldx + b.width; hspeed = 0;         }         //left side             if(x + width + hspeed >= b.worldx +1 && x + width + hspeed <= b.worldx + hspeed + 2)         {          x = b.worldx - width; hspeed = 0;         }     }      //check if on top or bottom     if(x + width + hspeed >= b.worldx+2 && x + hspeed <= b.worldx+b.width-2)     {         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;         }          }     } } 

my guess should set vspeed = -vspeed; instead of vspeed = 0. elastic bouncing means velocity gets mirrored in box's side.

if set zero, depending on order in perform updates, might not move during frame. , since use <= , >= bounds checking, you'll still inside box on next frame, invoke bouncing behaviour again, , stuck head glued block.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -