Basic collision direction detection on 2d objects
- by Osso Buko
I am trying to develop a platform game for Android by using ANdroid GL Engine (ANGLE). And I am having trouble with collision detection. I have two objects which is shaped as rectangular. And no change in rotation. Here is a scheme of attributes of objects. 
What i am trying to do is when objects collide they block each other's movement on that direction. Every object has 4 boolean (bTop, bBottom, bRight, bLeft). For example when bBottom is true object can't advance on that direction.
I came up with a solution but it seems it only works on one dimensional. Bottom and top or right and left.    
public void collisionPlatform (MyObject a, MyObject b) {
    // first obj is player and second is a wall or a platform
    Vector p1 = a.mPosition;  // p1 = middle point of first object
    Vector d1 = a.mPosition2;  // width(mX) and height of first object
    Vector mSpeed1 = a.mSpeed; // speed vector of first object
    Vector p2 = b.mPosition;  // p1 = middle point of second object
    Vector d2 = b.mPosition2;  // width(mX) and height of second object
    Vector mSpeed2 = b.mSpeed;  // speed vector of second object
    float xDist, yDist; // distant between middle of two object
    float width , height; // this is average of two objects measurements  width=(width1+width2)/2
    xDist=(p1.mX - p2.mX); // calculate distance // if positive first object is at the right
    yDist=(p1.mY - p2.mY);   // if positive first object is below
    width = d1.mX + d2.mX; // average measurements calculate
    height = d1.mY + d2.mY;
    width/=2; 
    height/=2;
    if (Math.abs(xDist) < width && Math.abs(yDist) < height) { // Two object is collided
        if(p1.mY>p2.mY) { // first object is below second one
            a.bTop = true;
            if(a.mSpeed.mY<0)  a.mSpeed.mY=0;
            b.bBottom = true;
            if(b.mSpeed.mY>0)  b.mSpeed.mY=0;
        } else {
            a.bBottom = true;
            if(a.mSpeed.mY>0)  a.mSpeed.mY=0;
            b.bTop = true;
            if(b.mSpeed.mY<0)  b.mSpeed.mY=0;
        }
    }
As seen in my code it simply will not work. when object comes from right or left it doesn't work. I tried couple of ways 
other than this one but none worked. I am guessing right method will include mSpeed vector. But I have no idea how to do it. 
 I really appreciate if you could help. Sorry for my bad english.