Advertisement

Collision math beating me up.

Started by December 21, 2018 06:21 AM
2 comments, last by SpaceCadet.Steve 5 years, 11 months ago

So this is kicking me a little bit. Works from -x and +x direction. -z works, -y works. But if I collide in the +z or +y it goes haywire (pushes the cube to the nearest corner)

Edit to add. Reversing the operators works as I originally intended. Pull the cube in, and trap it. Though the same directions would free the cube.


	float DirXdistP1andWH = initPosTransCubeX - initPosWormHoleX;
        float DirYdistP1andWH = initPosTransCubeY - initPosWormHoleY;
        float DirZdistP1andWH = initPosTransCubeZ - initPosWormHoleZ;

        float XdistP1andWH = std::fabs(initPosTransCubeX - initPosWormHoleX);
        float YdistP1andWH = std::fabs(initPosTransCubeY - initPosWormHoleY);
        float ZdistP1andWH = std::fabs(initPosTransCubeZ - initPosWormHoleZ);

        if (XdistP1andWH <= P1Width && YdistP1andWH <= P1Height && ZdistP1andWH <= P1Depth ) {

            if ((DirXdistP1andWH >= -(P1Width)) && (DirXdistP1andWH >= (DirYdistP1andWH || DirZdistP1andWH))) {
                initPosTransCubeX += (P1Width - XdistP1andWH);
            }
            else if ((DirYdistP1andWH >= -(P1Height)) && (DirYdistP1andWH >= (DirXdistP1andWH || DirZdistP1andWH)) ) {
                initPosTransCubeY += (P1Height - YdistP1andWH);
            }
            else if ((DirZdistP1andWH >= -(P1Depth)) && (DirZdistP1andWH >= (DirXdistP1andWH || DirYdistP1andWH)) ) {
                initPosTransCubeZ += (P1Depth - ZdistP1andWH);
            }
            else if ((DirXdistP1andWH <= P1Width) && (DirXdistP1andWH <= (DirYdistP1andWH || DirZdistP1andWH)) ) {
                initPosTransCubeX -= (P1Width - XdistP1andWH);
            }
            else if ((DirYdistP1andWH <= P1Height) && (DirYdistP1andWH <= (DirXdistP1andWH || DirZdistP1andWH)) ) {
                initPosTransCubeY -= (P1Height - YdistP1andWH);
            }
            else if ((DirZdistP1andWH <= P1Depth) && (DirZdistP1andWH <= (DirXdistP1andWH || DirYdistP1andWH)) ) {
                initPosTransCubeZ -= (P1Depth - ZdistP1andWH);
            }
       }

 

Solved it... The answer was right in front of me. And this could probably be simplified significantly. But it works fantastically :

 


        if (XdistP1andWH <= P1Width && YdistP1andWH <= P1Height && ZdistP1andWH <= P1Depth ) {

            if ((DirXdistP1andWH < 0.0f && DirXdistP1andWH >= -(P1Width)) && (XdistP1andWH > YdistP1andWH && XdistP1andWH > ZdistP1andWH)) {
                initPosTransCubeX -= (P1Width - XdistP1andWH);
            }
            if ((DirYdistP1andWH < 0.0f && DirYdistP1andWH >= -(P1Height)) && (YdistP1andWH > XdistP1andWH && YdistP1andWH > ZdistP1andWH) ) {
                initPosTransCubeY -= (P1Height - YdistP1andWH);
            }
            if ((DirZdistP1andWH < 0.0f && DirZdistP1andWH >= -(P1Depth)) && (ZdistP1andWH > XdistP1andWH && ZdistP1andWH > YdistP1andWH) ) {
                initPosTransCubeZ -= (P1Depth - ZdistP1andWH);
            }
            if ((DirXdistP1andWH > 0.0f && DirXdistP1andWH <= P1Width) && (XdistP1andWH > YdistP1andWH && XdistP1andWH > ZdistP1andWH) ) {
                initPosTransCubeX += (P1Width - XdistP1andWH);
            }
            if ((DirYdistP1andWH > 0.0f && DirYdistP1andWH <= P1Height) && (YdistP1andWH > XdistP1andWH && YdistP1andWH > ZdistP1andWH) ) {
                initPosTransCubeY += (P1Height - YdistP1andWH);
            }
            if ((DirZdistP1andWH > 0.0f && DirZdistP1andWH <= P1Depth) && (ZdistP1andWH > XdistP1andWH && ZdistP1andWH > YdistP1andWH) ) {
                initPosTransCubeZ += (P1Depth - ZdistP1andWH);
            }
       }

 

Advertisement

//wall or solid collision (prevent movement beyond) TODO detect and handle rotational changes in size and shape and where
// OneX, OneY, OneZ, OneWidth, OneHeight, OneDepth, TwoX, TwoY, TwoZ, TwoWidth, TwoHeight, TwoDepth
void ObjectIsSolidVS(float objectOneX, float objectOneY, float objectOneZ, float objectOneXWidth, float objectOneYHeight, float objectOneZDepth, float objectTwoX, float objectTwoY, float objectTwoZ, float objectTwoXWidth, float objectTwoYHeight, float objectTwoZDepth ){

        float WidthDifference = (objectOneXWidth / 2.0f) + (objectTwoXWidth / 2.0f);
		float HeightDifference = (objectOneXWidth / 2.0f) + (objectTwoXWidth / 2.0f);
		float DepthDifference = (objectOneXWidth / 2.0f) + (objectTwoXWidth / 2.0f);

		float DirectionX = objectOneX - objectTwoX;
        float DirectionY = objectOneY - objectTwoY;
        float DirectionZ = objectOneZ - objectTwoZ;

        float DistanceX = std::fabs(objectOneX - objectTwoX);
        float DistanceY = std::fabs(objectOneY - objectTwoY);
        float DistanceZ = std::fabs(objectOneZ - objectTwoZ);

		float XWidthMinusYHeight = objectTwoXWidth - objectTwoYHeight;
		float XWidthMinusZDepth = objectTwoXWidth - objectTwoZDepth;
		float YHeightMinusZDepth = objectTwoYHeight - objectTwoZDepth;
		float YHeightMinusXWidth = objectTwoYHeight - objectTwoXWidth;
		float ZDepthMinusXWidth = objectTwoZDepth - objectTwoXWidth;
		float ZDepthMinusYHeight = objectTwoZDepth - objectTwoYHeight;

            //Collision Detection
        if (DistanceX <= WidthDifference && DistanceY <= HeightDifference && DistanceZ <= DepthDifference ) {
                // if approaching from -x push back to border
            if ((DirectionX < 0.0f && DirectionX >= -(WidthDifference)) && (DistanceX > (DistanceY + XWidthMinusYHeight) && DistanceX > (DistanceZ + XWidthMinusZDepth)) ) {
                objectOneX -= (WidthDifference - DistanceX);
            }
                // if approaching from -y push back to border
            if ((DirectionY < 0.0f && DirectionY >= -(HeightDifference)) && (DistanceY > (DistanceX + YHeightMinusXWidth) && DistanceY > (DistanceZ + YHeightMinusZDepth)) ) {
                objectOneY -= (HeightDifference - DistanceY);
            }
                // if approaching from -z push back to border
            if ((DirectionZ < 0.0f && DirectionZ >= -(DepthDifference)) && (DistanceZ > (DistanceX + ZDepthMinusXWidth) && DistanceZ > (DistanceY + ZDepthMinusYHeight)) ) {
                objectOneZ -= (DepthDifference - DistanceZ);
            }
                // if approaching from +x push back to border
            if ((DirectionX > 0.0f && DirectionX <= WidthDifference) && (DistanceX > (DistanceY + XWidthMinusYHeight) && DistanceX > (DistanceZ + XWidthMinusZDepth)) ) {
                objectOneX += (WidthDifference - DistanceX);
            }
                // if approaching from +y push back to border
            if ((DirectionY > 0.0f && DirectionY <= HeightDifference) && (DistanceY > (DistanceX + YHeightMinusXWidth) && DistanceY > (DistanceZ + YHeightMinusZDepth)) ) {
                objectOneY += (HeightDifference - DistanceY);
            }
                // if approaching from +z push back to border
            if ((DirectionZ > 0.0f && DirectionZ <= DepthDifference) && (DistanceZ > (DistanceX + ZDepthMinusXWidth) && DistanceZ > (DistanceY + ZDepthMinusYHeight)) ) {
                objectOneZ += (DepthDifference - DistanceZ);
            }
       }


}

Function in progress.

This topic is closed to new replies.

Advertisement