• Sort Posts • 7 replies

I am using this api to develop an AR game.
In this game, I use a tracker (Tracker A)  to determine a battle field and some game unit,
I have another tracker (Tracker B) to determine another game unit and all of the unit are not do any translation.

Tracker A
- multi unit
- translation

Tracker B
- single unit
- no translation

Now, i want to calculate the distance between tracker A unit and tracker B unit.
But the API just return the modelview matrix and projection matrix of the tracker A and tracker B.
I don't know how to get the world location of these units.

I try to use following method to get the distance between tracker A and tracker B:
Math.sqrt(
Math.pow(trackerA.modelViewMatrix- trackerB.modelViewMatrix, 2) +
Math.pow(trackerA.modelViewMatrix- trackerB.modelViewMatrix, 2) +
Math.pow(trackerA.modelViewMatrix- trackerB.modelViewMatrix, 2)
)

I try to move the tracker B to test it and it seem ok, but i am not sure is it correct.

I suppose above method is correct, but i cannot get the distance between tracker A unit and tracker B unit.
Because the tracker A unit had been translate.

Is it possible to get the world location of the game unit?

Hi, the corrrect mathematical way of solving your problem is as follows:

1.   get the Modelview matrix of the unit you are interested in:  for example, if you have a unit which is below TrackableB, and this unit is also translated with respect to the parent trackable, you can compute the Modelview matrix of that unit as the Modelview matrix of its parent Trackable, multiplied by the local translation matrix of that unit.

2.   get the modelview matrix of a unit in the other Trackable (e.g. TrackableA, if previously you were doing TrackableB), with same logic as above (note: if the unit is not translated, then just take the modelview matrix of the parent Trackable)

3.   compute the relative matrix of the local reference frame of one unit (e.g. the unit in TrackableB) with respect to the reference frame of the unit in TrackableA; this can be done by multiplying one modelview matrix by the "inverse transpose" of the second matrix.

4. Then extract the distance from the resulting matrix (components 12, 13, 14 of the matrix data array).

https://developer.vuforia.com/resources/dev-guide/pose-matrix-explained

https://developer.vuforia.com/resources/dev-guide/positioning-3d-content

Hi AlessandroB,

Thank you for your quick reply. I cannot test the program at this moment, but I have draft the code follow your step.
Please tell me if i have any misunderstanding.

1. Get the Modelview matrix of the unitA in TrackableA:
//TrackableA Modelview matrix
Matrix44F modelViewMatrix_Vuforia = Tool.convertPose2GLMatrix(result.getPose());
float[] modelViewMatrixA = modelViewMatrix_Vuforia.getData();

//apply the translation
float[] translateMatrix = new float{1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
x, y, z, 1};
// modelViewMatrixA * translateMatrix
modelViewMatrixA = MathUtil.matrix4x4Multiply(modelViewMatrixA, translateMatrix);

2. Get the Modelview matrix of the unitB in TrackableB:
//TrackableB Modelview matrix
Matrix44F modelViewMatrix_Vuforia = Tool.convertPose2GLMatrix(result.getPose());
float[] modelViewMatrixB = modelViewMatrix_Vuforia.getData();

3.  Multiplying modelview matrix A by the "inverse transpose" of the modelview matrix B.

//get the "inverse transpose" of the modelview matrix B. is the sequence correct?
modelViewMatrixB = SimpleMath.Matrix44FInverse(modelViewMatrixB);
modelViewMatrixB = SimpleMath.Matrix44FTranspose(modelViewMatrixB);

// modelViewMatrixA * modelViewMatrixB
resultMatrix = MathUtil.matrix4x4Multiply(modelViewMatrixA, modelViewMatrixB);

4. extract the distance from the resulting matrix (components 12, 13, 14 of the matrix data array).
distance = Math.sqrt(
Math.pow(resultMatrix, 2) +
Math.pow(resultMatrix, 2) +
Math.pow(resultMatrix, 2)
)

Thanks you

Hi, yes, looks correct, although you are saving the inverse transpose into the same matrix "modelViewMatrixB";

I would suggest introducing a new variable and call it "modelViewMatrixB_invTransposed", so to make the code more readable; also, if you are going to reuse the modelview matrix for rendering, it's better not to change their original values, but rather store the modified matrices into new variables.

Hi AlessandroB,

After I test the program, it seem not work very well.

The problem is occurred on the translation.

If I have not do any translation on step 1, the distance between TrackableA and TrackableB seem correct.

After I apply the translation on step 1 and move the TrackableB near the unit on TrackableA, the distance is greater than before.

Here is some testing data:

Before moving the TrackableB:

ModelView Matrix of TrackableB:
0.9902878, 0.1155278, -0.077352464, 0.0,
0.13899194, -0.8091668, 0.57090306, 0.0,
0.0033641309, -0.5761097, -0.81736547, 0.0,
110.228165, 34.529446, 301.3136, 1.0

ModelView Matrix of TrackableA - before translation:
0.9999266, -0.011587993, 0.003531636, 0.0,
-0.011308877, -0.7883872, 0.6150755, 0.0,
-0.004343195, -0.6150704, -0.7884603, 0.0,
-11.824994, 23.137814, 306.09253, 1.0,

translation location - move to top right hand side
x: 21.0, y: 190.0

ModelView Matrix of TrackableA - after translation:
0.9999266, -0.011587993, 0.003531636, 0.0,
-0.011308877, -0.7883872, 0.6150755, 0.0,
-0.004343195, -0.6150704, -0.7884603, 0.0,
9.175006, 213.13782, 306.09253, 1.0,

Distance: 205.2694174084345

After moving the TrackableB near the unit on TrackableA:

ModelView Matrix of TrackableB:
0.99414, -0.073208705, 0.079536825, 0.0,
-0.10571994, -0.8119849, 0.57402426, 0.0,
0.02255913, -0.57906914, -0.81496626, 0.0,
54.741623, -74.90674, 347.16843, 1.0,

ModelView Matrix of TrackableA - before translation:
0.9966287, -0.070717745, 0.04159838, 0.0,
-0.08141434, -0.78966707, 0.6081099, 0.0,
-0.010155291, -0.60944647, -0.7927622, 0.0,
18.172586, 19.184374, 277.9756, 1.0,

translation location - move to top right hand side
x: 21.0, y: 190.0

ModelView Matrix of TrackableA - after translation:
0.9966287, -0.070717745, 0.04159838, 0.0,
-0.08141434, -0.78966707, 0.6081099, 0.0,
-0.010155291, -0.60944647, -0.7927622, 0.0,
39.172585, 209.18437, 277.9756, 1.0,

Distance: 292.8101743548454

Sorry I cannot debug your code,

but one thing you can do is to check the order of multiplication of matrices (you might need to multiply the matrices in the opposite order, as matrix multiplication is not order-independent°;

try comparing your code with this:

https://developer.vuforia.com/forum/faq/android-how-can-i-bring-multiple-targets-common-space

Hi AlessandroB,

It is very helpful, I can get the translation value of target B base on the target A, then I can calculate the distance very easy.

It also solve other problem what i want to ask. Thank you very much :)

Ken

Great!