Vehicle Matrix - Complex Math
#1

I'm using Gamer_Z's code but it only has this:

stock PositionFromVehicleOffset(vehicle,Float:offX,Float :offY,Float:offZ,&Float:x,&Float:y,&Float:z)

I need the oposit to this function, I need to get the Vehicle Offset FROM Position, not the other way around.

Can anyone help me please?

The code is:

Код:
stock PositionFromVehicleOffset(vehicle,Float:offX,Float:offY,Float:offZ,&Float:x,&Float:y,&Float:z)
{
//initial processing step - gather information
	new
		Mat4x3[MatrixParts][MatrixIndicator];

    GetVehicleMatrix(vehicle,Mat4x3);

//offset calculation math
	x = offX * Mat4x3[mp_PITCH][mi_X] + offY * Mat4x3[mp_ROLL][mi_X] + offZ * Mat4x3[mp_YAW][mi_X] + Mat4x3[mp_POS][mi_X];
	y = offX * Mat4x3[mp_PITCH][mi_Y] + offY * Mat4x3[mp_ROLL][mi_Y] + offZ * Mat4x3[mp_YAW][mi_Y] + Mat4x3[mp_POS][mi_Y];
	z = offX * Mat4x3[mp_PITCH][mi_Z] + offY * Mat4x3[mp_ROLL][mi_Z] + offZ * Mat4x3[mp_YAW][mi_Z] + Mat4x3[mp_POS][mi_Z];

	return 1;
}
The include is here: http://gpb.******code.com/svn/trunk/...icleMatrix.inc

Regards
Reply
#2

It's not that I didn't tried to use these vector matrix after I failed with rotations

Thats what I created, both ways I know how to solve a System of linear equations
pawn Код:
stock Float: Det3x3({ Float, _ }: mat[][]) {
    return
        (mat[0][0] * mat[1][1] * mat[2][2] + mat[0][1] * mat[1][2] * mat[2][0] + mat[0][2] * mat[1][0] * mat[2][1]) -
        (mat[2][0] * mat[1][1] * mat[0][2] + mat[2][1] * mat[1][2] * mat[0][0] + mat[2][2] * mat[1][0] * mat[0][1])
    ;
}
pawn Код:
stock OffsetFromVehiclePosition(vehicle, Float: x, Float: y, Float: z, & Float: offX, & Float: offY, & Float: offZ) {
    //initial processing step - gather information
    new
        Mat4x3[MatrixParts][MatrixIndicator]
    ;
    GetVehicleMatrix(vehicle, Mat4x3);
    // offset calculation
    x -= Mat4x3[mp_POS][mi_X];
    y -= Mat4x3[mp_POS][mi_Y];
    z -= Mat4x3[mp_POS][mi_Z];
#if 1 // Cramer's rule
    new
        Float: Mat3x3_1[3][3],
        Float: Mat3x3_2[3][3],
        Float: Mat3x3_3[3][3]
    ;
    Mat3x3_1[0][0] = Mat4x3[mp_PITCH][mi_X];
    Mat3x3_1[1][0] = Mat4x3[mp_PITCH][mi_Y];
    Mat3x3_1[2][0] = Mat4x3[mp_PITCH][mi_Z];
    Mat3x3_1[0][1] = Mat4x3[mp_ROLL][mi_X];
    Mat3x3_1[1][1] = Mat4x3[mp_ROLL][mi_Y];
    Mat3x3_1[2][1] = Mat4x3[mp_ROLL][mi_Z];
    Mat3x3_1[0][2] = Mat4x3[mp_YAW][mi_X];
    Mat3x3_1[1][2] = Mat4x3[mp_YAW][mi_Y];
    Mat3x3_1[2][2] = Mat4x3[mp_YAW][mi_Z];

    offZ = Det3x3(Mat3x3_1);

    if(offZ != 0.0) {
        Mat3x3_2 = Mat3x3_1;
        Mat3x3_3 = Mat3x3_1;

        Mat3x3_1[0][0] = Mat3x3_2[0][1] = Mat3x3_3[0][2] = x;
        Mat3x3_1[1][0] = Mat3x3_2[1][1] = Mat3x3_3[1][2] = y;
        Mat3x3_1[2][0] = Mat3x3_2[2][1] = Mat3x3_3[2][2] = z;

        offX = Det3x3(Mat3x3_1) / offZ;
        offY = Det3x3(Mat3x3_2) / offZ;
        offZ = Det3x3(Mat3x3_3) / offZ;
    }
#else // Gaussian elimination
    new
        Float: pX = Mat4x3[mp_PITCH][mi_X],
        Float: pY = Mat4x3[mp_PITCH][mi_Y],
        Float: pZ = Mat4x3[mp_PITCH][mi_Z],
        Float: rX = Mat4x3[mp_ROLL][mi_X],
        Float: rY = Mat4x3[mp_ROLL][mi_Y],
        Float: rZ = Mat4x3[mp_ROLL][mi_Z],
        Float: yX = Mat4x3[mp_YAW][mi_X],
        Float: yY = Mat4x3[mp_YAW][mi_Y],
        Float: yZ = Mat4x3[mp_YAW][mi_Z]
    ;
    offZ = ((z * pX - pZ * x) * rX - (rZ * pX - pZ * rX) * x) / ((yZ * pX - pZ * yX) * rX - (rZ * pX - pZ * rX) * yX);
    offY = (y * pX - pY * x - offZ * (yY * pX - pY * yX)) / (rY * pX - pY * rX);
    offX = (x - offZ * yX - offY * rX) / pX;
#endif
}
Well in both ways there were special cases if the vehicle was on a hill that it returned an invalid z value
It could be possible that I made a mistake with Gauss but I doubt it because it works fine on normal roads
Reply
#3

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
It's not that I didn't tried to use these vector matrix after I failed with rotations

Thats what I created, both ways I know how to solve a System of linear equations
pawn Код:
stock Float: Det3x3({ Float, _ }: mat[][]) {
    return
        (mat[0][0] * mat[1][1] * mat[2][2] + mat[0][1] * mat[1][2] * mat[2][0] + mat[0][2] * mat[1][0] * mat[2][1]) -
        (mat[2][0] * mat[1][1] * mat[0][2] + mat[2][1] * mat[1][2] * mat[0][0] + mat[2][2] * mat[1][0] * mat[0][1])
    ;
}
pawn Код:
stock OffsetFromVehiclePosition(vehicle, Float: x, Float: y, Float: z, & Float: offX, & Float: offY, & Float: offZ) {
    //initial processing step - gather information
    new
        Mat4x3[MatrixParts][MatrixIndicator]
    ;
    GetVehicleMatrix(vehicle, Mat4x3);
    // offset calculation
    x -= Mat4x3[mp_POS][mi_X];
    y -= Mat4x3[mp_POS][mi_Y];
    z -= Mat4x3[mp_POS][mi_Z];
#if 1 // Cramer's rule
    new
        Float: Mat3x3_1[3][3],
        Float: Mat3x3_2[3][3],
        Float: Mat3x3_3[3][3]
    ;
    Mat3x3_1[0][0] = Mat4x3[mp_PITCH][mi_X];
    Mat3x3_1[1][0] = Mat4x3[mp_PITCH][mi_Y];
    Mat3x3_1[2][0] = Mat4x3[mp_PITCH][mi_Z];
    Mat3x3_1[0][1] = Mat4x3[mp_ROLL][mi_X];
    Mat3x3_1[1][1] = Mat4x3[mp_ROLL][mi_Y];
    Mat3x3_1[2][1] = Mat4x3[mp_ROLL][mi_Z];
    Mat3x3_1[0][2] = Mat4x3[mp_YAW][mi_X];
    Mat3x3_1[1][2] = Mat4x3[mp_YAW][mi_Y];
    Mat3x3_1[2][2] = Mat4x3[mp_YAW][mi_Z];

    offZ = Det3x3(Mat3x3_1);

    if(offZ != 0.0) {
        Mat3x3_2 = Mat3x3_1;
        Mat3x3_3 = Mat3x3_1;

        Mat3x3_1[0][0] = Mat3x3_2[0][1] = Mat3x3_3[0][2] = x;
        Mat3x3_1[1][0] = Mat3x3_2[1][1] = Mat3x3_3[1][2] = y;
        Mat3x3_1[2][0] = Mat3x3_2[2][1] = Mat3x3_3[2][2] = z;

        offX = Det3x3(Mat3x3_1) / offZ;
        offY = Det3x3(Mat3x3_2) / offZ;
        offZ = Det3x3(Mat3x3_3) / offZ;
    }
#else // Gaussian elimination
    new
        Float: pX = Mat4x3[mp_PITCH][mi_X],
        Float: pY = Mat4x3[mp_PITCH][mi_Y],
        Float: pZ = Mat4x3[mp_PITCH][mi_Z],
        Float: rX = Mat4x3[mp_ROLL][mi_X],
        Float: rY = Mat4x3[mp_ROLL][mi_Y],
        Float: rZ = Mat4x3[mp_ROLL][mi_Z],
        Float: yX = Mat4x3[mp_YAW][mi_X],
        Float: yY = Mat4x3[mp_YAW][mi_Y],
        Float: yZ = Mat4x3[mp_YAW][mi_Z]
    ;
    offZ = ((z * pX - pZ * x) * rX - (rZ * pX - pZ * rX) * x) / ((yZ * pX - pZ * yX) * rX - (rZ * pX - pZ * rX) * yX);
    offY = (y * pX - pY * x - offZ * (yY * pX - pY * yX)) / (rY * pX - pY * rX);
    offX = (x - offZ * yX - offY * rX) / pX;
#endif
}
Well in both ways there were special cases if the vehicle was on a hill that it returned an invalid z value
It could be possible that I made a mistake with Gauss but I doubt it because it works fine on normal roads
Thank you.

Will the rotations work with it too?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)