Velocity collision detection (2D)
        Posted  
        
            by 
                ultifinitus
            
        on Game Development
        
        See other posts from Game Development
        
            or by ultifinitus
        
        
        
        Published on 2011-03-17T17:09:51Z
        Indexed on 
            2011/03/18
            0:19 UTC
        
        
        Read the original article
        Hit count: 362
        
Alright, so I have made a simple game engine (see youtube)
And my current implementation of collision resolution has a slight problem, involving the velocity of a platform.
Basically I run through all of the objects necessary to detect collisions on and resolve those collisions as I find them. Part of that resolution is setting the player's velocity = the platform's velocity. Which works great! Unless I have a row of platforms moving at different velocities or a platform between a stack of tiles....
(current system)
bool player::handle_collisions() {
collisions tcol;
bool did_handle = false;
bool thisObjectHandle = false;
for (int temp = 0; temp < collideQueue.size(); temp++) {
 thisObjectHandle = false;
 tcol = get_collision(prevPos.x,y,get_img()->get_width(),get_img()->get_height(),
                collideQueue[temp]->get_position().x,collideQueue[temp]->get_position().y,
                     collideQueue[temp]->get_img()->get_width(),collideQueue[temp]->get_img()->get_height());
if (prevPos.y >= collideQueue[temp]->get_prev_pos().y + collideQueue[temp]->get_img()->get_height())
  if (tcol.top > 0) {
    add_pos(0,tcol.top);
    set_vel(get_vel().x,collideQueue[temp]->get_vel().y);
    thisObjectHandle = did_handle = true;
  }
if (prevPos.y + get_img()->get_height() <= collideQueue[temp]->get_prev_pos().y)
  if (tcol.bottom > 0) {
    add_pos(collideQueue[temp]->get_vel().x,-tcol.bottom);
    set_vel(get_vel().x/*collideQueue[temp]->get_vel().x*/,collideQueue[temp]->get_vel().y);
    ableToJump = true;
    jumpTimes = maxjumpable;
    thisObjectHandle = did_handle = true;
  }
///
/// ADD CODE FROM NEXT CODE BLOCK HERE (on forum, not in code)
///
}
for (int temp = 0; temp < collideQueue.size(); temp++) {
thisObjectHandle = false;
tcol = get_collision(x,y,get_img()->get_width(),get_img()->get_height(),
                     collideQueue[temp]->get_position().x,collideQueue[temp]->get_position().y,
                     collideQueue[temp]->get_img()->get_width(),collideQueue[temp]->get_img()->get_height());
if (prevPos.x + get_img()->get_width() <= collideQueue[temp]->get_prev_pos().x)
  if (tcol.left > 0) {
    add_pos(-tcol.left,0);
    set_vel(collideQueue[temp]->get_vel().x,get_vel().y);
    thisObjectHandle = did_handle = true;
  }
if (prevPos.x >= collideQueue[temp]->get_prev_pos().x + collideQueue[temp]->get_img()->get_width())
  if (tcol.right > 0) {
    add_pos(tcol.right,0);
    set_vel(collideQueue[temp]->get_vel().x,get_vel().y);
    thisObjectHandle = did_handle = true;
  }
}
return did_handle;
}
(if I add the following code {where the comment to do so is}, which is glitchy, the above problem doesn't happen, though it brings others)
if (!thisObjectHandle) {
  if (tcol.bottom > tcol.top) {
    add_pos(collideQueue[temp]->get_vel().x,-tcol.bottom);
    set_vel(get_vel().x,collideQueue[temp]->get_vel().y);
  }
  else if (tcol.top > tcol.bottom) {
    add_pos(0,tcol.top);
    set_vel(get_vel().x,collideQueue[temp]->get_vel().y);
  }
}
How would you change my system to prevent this?
© Game Development or respective owner