简体   繁体   中英

Gaze at SCNNode using SceneKit and CoreMotion

I'm creating a Virtual Reality application using SceneKit and CoreMotion. Everything is working fine, but now I'd like to write a function to check if a user is looking at object by calculating where the object is in eye-space.

I'm using SCNSceneRendererDelegate to delegate the renderer function in my ViewController

func renderer(aRenderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) {

    var motion = motionManager?.deviceMotion

    if (motion != nil) {
        let currentAttitude = motion!.attitude
        let roll = Float(currentAttitude.roll)
        let pitch = Float(currentAttitude.pitch)
        let yaw = Float(currentAttitude.yaw)

        cameraRollNode.eulerAngles = SCNVector3Make(roll, 0.0, 0.0)
        cameraPitchNode.eulerAngles = SCNVector3Make(0.0, 0.0, pitch)
        cameraYawNode.eulerAngles = SCNVector3Make(0.0, yaw, 0.0)        }
}

I wrote the following code in Android (which supports the official Cardboard SDK).

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;
}

Is there a way to calculate the object in eye-space using CoreMotion and SceneKit

If all you're looking for is whether an object is in view, you don't need to do any coordinate conversion math yourself. Just ask the view — there's a method for that .

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