Log in or register to post comments

Virtual button and Touch Interection

September 28, 2012 - 4:17am #1

I m new Bee in QCar

i m using Virtual Button as a sample project and i want to do the following things

when i touch the specific triangle from Device Screen, i want to show specif image on touch in triangle and when i touch on the screen (other then triangle then that iamge disappears

i m following this example

ksiva wrote:

The problem is that the x,y coordinates returned by a tap are in screen space. You'll want to project that to target space to determine whether or not a target was touched.

Here are the steps for determining whether a target was touched in the ImageTargets sample.

1. Copy SampleMath.h and SampleMath.cpp from Dominoes/jni to ImageTargets/jni.
2. Add SampleMath.cpp to the LOCAL_SRC_FILES variable in ImageTargets/jni/Android.mk.

3. Make the following changes to ImageTargets.cpp:

#include "SampleMath.h"
#include <QCAR/ImageTarget.h>
#include <math.h>

QCAR::Matrix44F inverseProjMatrix;
QCAR::Matrix44F modelViewMatrix;

bool tap = false;
float tapX = 0;
float tapY = 0;

JNIEXPORT int JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargets_nativeTapEvent(JNIEnv *, jobject, jfloat x, jfloat y)
{
    tapX = x;
    tapY = y;
    tap = true;
}

// Copy this from Dominoes.cpp
bool
linePlaneIntersection(QCAR::Vec3F lineStart, QCAR::Vec3F lineEnd,
                      QCAR::Vec3F pointOnPlane, QCAR::Vec3F planeNormal,
                      QCAR::Vec3F &intersection)
{
    ...
}

// Copy this from Dominoes.cpp
void
projectScreenPointToPlane(QCAR::Vec2F point, QCAR::Vec3F planeCenter, QCAR::Vec3F planeNormal,
                          QCAR::Vec3F &intersection, QCAR::Vec3F &lineStart, QCAR::Vec3F &lineEnd)
{
    ...
}

// Edit existing method
JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_renderFrame(JNIEnv *, jobject)
{
    ...
    
    // Did we find any trackables this frame?
    for(int tIdx = 0; tIdx < state.getNumActiveTrackables(); tIdx++)
    {
        // Get the trackable:
        const QCAR::Trackable* trackable = state.getActiveTrackable(tIdx);
        //QCAR::Matrix44F modelViewMatrix =
        //    QCAR::Tool::convertPose2GLMatrix(trackable->getPose());
        
        // Cache the modelViewMatrix
        modelViewMatrix =
            QCAR::Tool::convertPose2GLMatrix(trackable->getPose());
        
        if (tap)
        {
            QCAR::Vec3F intersection, lineStart, lineEnd;
            projectScreenPointToPlane(QCAR::Vec2F(tapX, tapY), QCAR::Vec3F(0, 0, 0), QCAR::Vec3F(0, 0, 1), intersection, lineStart, lineEnd);
            
            const QCAR::ImageTarget* imageTarget = static_cast<const QCAR::ImageTarget*>(trackable);
            QCAR::Vec2F trackableSize = imageTarget->getSize();
            
            LOG("tap coordinates (screen space): %.2f, %.2f", tapX, tapY);
            LOG("tap coordinates (target space): %.2f, %.2f", intersection.data[0], intersection.data[1]);
            
            if (fabs(intersection.data[0]) < (trackableSize.data[0] / 2) &&
                fabs(intersection.data[1]) < (trackableSize.data[1] / 2))
            {
                LOG("tapped inside the target!");
                // do something here
            }
            
            tap = false;
        }
        
        ...
    }
    ...
}

// Edit existing method
JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargets_startCamera(JNIEnv *, jobject)
{
    ...
    
    // Cache the projection matrix:
    const QCAR::Tracker& tracker = QCAR::Tracker::getInstance();
    const QCAR::CameraCalibration& cameraCalibration =
                                    tracker.getCameraCalibration();
    projectionMatrix = QCAR::Tool::getProjectionGL(cameraCalibration, 2.0f,
                                            2000.0f);
    
    // Cache the inverse projection matrix:
    inverseProjMatrix = SampleMath::Matrix44FInverse(projectionMatrix);
}

as a result if this i just get the screen Co-ordinates
but not able to get Target co-ordinate
how to get the target co-ordinate
need help .

resault is Following
GetX GetY(16748): 153.53879 140.625
 tap coordinates (screen space): 153.54, 140.62
 tap coordinates (target space): NaN, NaN

Virtual button and Touch Interection

October 9, 2012 - 1:18am #2

If you get NaN values, one reason could be that either the modelview matrix or the projection matrix are not correctly set;

I would suggest to debug the problem by logging the values of those matrices to be sure that they look something 'reasonable' (e.g. not all zeros or not wierd huge numbers); to this aim you can use the code in SampleUtils.cpp:

SampleUtils::printMatrix(const float* mat)

Log in or register to post comments