c++ - AABB collision detection glitches when using more than one key press -
c++ - AABB collision detection glitches when using more than one key press -
currently designing 2d platformer , have implemented collision detection code follows:
if (keys['w']){ checkcollision(vector of bounded boxes, direction; } if (keys['a']){ checkcollision(vector of bounded boxes, direction; }
etc...
checkcollision(vector, direction){ for(each boundary box){ if (dir == 'up'){ if (aabb collision check){hit = true;} else{hit = false;} walk(velocity, direction); } else if (dir == 'right'){ if (aabb collision check){hit = true;} else{hit = false;} walk(velocity, direction); } } }
etc...
walk(velocity, direction){ if (dir == 'up'){ if (hit){ y -= 2; } else{ y += velocity; } } else if (dir == 'right'){ if (hit){ x -= 2; } else{ x -= velocity; } }
etc...
which seems work when colliding object horizontally, if maintain holding downwards right key , press key, function recognizes object still colliding, applies same forcefulness applied if object in-game had jumped , collided above, in-game object gets pushed downwards though there no force.
i have tried implementing separate boolean values collision in x , y has not affected outcome.
the prepare have found if when processing keys, utilize else if's instead of if's. makes linear motion , object cannot move diagonally.
any suggestions?
the problem facing because tied in input , collision systems. input scheme should move player per key presses. collision detection/handling scheme should solely take care of collisions. 1 time decouple them things become much cleaner.
// main loop while(1) { handleinput(); ... handlecollisions(); ... }
if have many objects in game can utilize spacial partitioning cut down number of collision checks have do. otherwise don't have worry it.
c++ collision-detection aabb
Comments
Post a Comment