﻿ opengl es - Android- Vuforia, calculate the real distance between camera to the image target? - DeveloperSite- developersite.org

# opengl es - Android- Vuforia, calculate the real distance between camera to the image target?

Keywords：android  Question:

I'm beginner in Vuforia library, I’m trying to calculate the real distance as cm between the AR camera and an image target via Vuforia on Android studio, I found this code on the vuforia forums but when I try it, I don't get the good results.

``````     TrackableResult result = state.getTrackableResult(tIdx);
Trackable trackable = result.getTrackable();
Matrix44F modelViewMatrix_Vuforia =Tool.convertPose2GLMatrix(result.getPose());
Matrix44F inversMV = SampleMath.Matrix44FInverse(modelViewMatrix_Vuforia);
Matrix44F invTranspMV = SampleMath.Matrix44FTranspose(inversMV);

float cam_x = invTranspMV.getData();
float cam_y = invTranspMV.getData();
float cam_z = invTranspMV.getData();

Log.v("QCV", "Posx=" + cam_x + ",posy=" + cam_y + ",posz=" + cam_z);
float distance = new Float(Math.sqrt(cam_x * cam_x + cam_y * cam_y + cam_z * cam_z));
Log.v("distance ",""+distance);
``````

Can you help me please ? is there another function or code to calculate the real distance as cm?

Thanks 1 Answer:

as far as I know, you should take the cam_x, cam_y and cam_z from the original target matrix, meaning `modelViewMatrix_Vuforia` in your code. The rest of your calculation is fine. You can look here for the formal Vuforia article: Determining target distance for native Android and iOS.

As for your second question - Vuforia has no way of knowing the real size. However, you can find it yourself by the size of the target you've defined in the dataset and the size of the printed target. This is also explained in the link above.