简体   繁体   中英

How to know if an 3DObject is Looked at in VR

I am using RajawaliVR library.

I have added a plane and applied texture to it. Know I want to know when my object is being looked at so that I can trigger some even. Is there anything in RajawaliVR or google cardboad that can help me achieve this.

    Material cruiserMaterial = new Material();
    cruiserMaterial.setDiffuseMethod(new DiffuseMethod.Lambert());
    cruiserMaterial.setColorInfluence(0);
    cruiserMaterial.enableLighting(true);
    try {
        cruiserMaterial.addTexture(new Texture("spaceCruiserTex",
                R.drawable.image2));
    } catch (TextureException e) {
        e.printStackTrace();
    }

    Object3D leftPlane = new Plane(10f, 10f, 1, 1, 1);

    leftPlane.setMaterial(cruiserMaterial);
    leftPlane.setRotZ(90);

    Object3D container = new Object3D();
    container.addChild(leftPlane);
    container.setRotX(90);
    container.setRotY(90);
    container.setRotZ(90);
    container.setZ(-20);

    getCurrentScene().addChild(container);

Just put this in your renderers main loop (OnDrawFrame), iterate a list with objects and pass the object as parameter. The method will return true if you are currently looking at an object.

private static final float YAW_LIMIT = 0.12f;
private static final float PITCH_LIMIT = 0.12f;

/**
 * Check if user is looking at object by calculating where the object is in eye-space.
 *
 * @return true if the user is looking at the object.
 */
private boolean isLookingAtObject(WorldObject object) {
    float[] initVec = { 0, 0, 0, 1.0f };
    float[] objPositionVec = new float[4];

    // Convert object space to camera space. Use the headView from onNewFrame.
    Matrix.multiplyMM(mModelView, 0, this.getHeadViewMatrix(), 0, object.getModel().getModelMatrix().getFloatValues(), 0);
    Matrix.multiplyMV(objPositionVec, 0, mModelView, 0, initVec, 0);

    float pitch = (float) Math.atan2(objPositionVec[1], -objPositionVec[2]);
    float yaw = (float) Math.atan2(objPositionVec[0], -objPositionVec[2]);

    return Math.abs(pitch) < PITCH_LIMIT && Math.abs(yaw) < YAW_LIMIT;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM