Wednesday, August 10, 2022
HomeGame Developmentc++ - Rectangular Collision Decision behaving incorrectly on corners

c++ – Rectangular Collision Decision behaving incorrectly on corners


I am at present writing a easy sport engine and encountered an error whereas making an attempt to resolve rectangular collisions. When an object strikes diagonally and collides with a stable object I solely appropriate it within the axis that will require much less translation to resolve the collision. The issue with that is that when a diagonally transferring object travels “over” a nook the smaller axis flips to the other axis earlier than the 2 objects cease colliding and the non-solid object “teleports” to repair itself.

Here is a bit of the collision decision technique (c++):

/*
    0: UP
    1: UP RIGHT
    2: RIGHT
    3: DOWN RIGHT
    4: DOWN
    5: DOWN LEFT
    6: LEFT
    7: UP LEFT
*/
swap(lastDir){
    case 0:
    {
        rect.y = (other->rect.y+other->rect.top)+fastened(0.0078125); 
        //0.007 is padding quantity, fastened is fastened level quantity class on account of {hardware} constraints, rect is rectangle with x, y, width, top
        break;
    }
                        
    case 1:
    {
        fPoint l1(rect.x, rect.y); //fPoint is Level utilizing fastened
        fPoint r1(rect.x+rect.width, rect.y+rect.top);
        fPoint l2(other->rect.x, other->rect.y);
        fPoint r2(other->rect.x+other->rect.width, other->rect.y+other->rect.top);
        fastened distX = abs(min(r1.x, r2.x)- max(l1.x, l2.x));
        fastened distY = abs(min(r1.y, r2.y) - max(l1.y, l2.y));
                        
        if(distX > distY){
            //if y is shorter appropriate y
            rect.y = (other->rect.y-rect.top)-fixed(0.0078125);
        }
        else if(distY > distX){
            //if x is shorter appropriate x
            rect.x = (other->rect.x-rect.width)-fixed(0.0078125);
        }
        break;
    }
                    
    case 2:
    {
            rect.x = (other->rect.x-rect.width)-fixed(0.0078125);
            break;
    }
//continues for instructions 3-7
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments