如何在ARCore中检查与对象的光线相交 [英] how to check ray intersection with object in ARCore

查看:85
本文介绍了如何在ARCore中检查与对象的光线相交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检查我是否触摸了屏幕上的对象?据我了解,HitResult类可让我检查是否触摸了已识别并映射的表面.但是我想检查一下我是否触摸了该表面上设置的对象.

Is there a way to check if I touched the object on the screen ? As I understand the HitResult class allows me to check if I touched the recognized and maped surface. But I want to check this I touched the object that is set on that surface.

推荐答案

ARCore确实没有对象的概念,因此我们不能直接提供它.我建议您以射线球测试为起点.

ARCore doesn't really have a concept of an object, so we can't directly provide that. I suggest looking at ray-sphere tests for a starting point.

但是,我可以帮助获取射线本身(添加到HelloArActivity中):

However, I can help with getting the ray itself (to be added to HelloArActivity):

/** 
 * Returns a world coordinate frame ray for a screen point.  The ray is
 * defined using a 6-element float array containing the head location
 * followed by a normalized direction vector.
 */
float[] screenPointToWorldRay(float xPx, float yPx, Frame frame) {
    float[] points = new float[12];  // {clip query, camera query, camera origin}
    // Set up the clip-space coordinates of our query point
    // +x is right:
    points[0] = 2.0f * xPx / mSurfaceView.getMeasuredWidth() - 1.0f;
    // +y is up (android UI Y is down):
    points[1] = 1.0f - 2.0f * yPx / mSurfaceView.getMeasuredHeight(); 
    points[2] = 1.0f; // +z is forwards (remember clip, not camera)
    points[3] = 1.0f; // w (homogenous coordinates)

    float[] matrices = new float[32];  // {proj, inverse proj}
    // If you'll be calling this several times per frame factor out
    // the next two lines to run when Frame.isDisplayRotationChanged().
    mSession.getProjectionMatrix(matrices, 0, 1.0f, 100.0f);
    Matrix.invertM(matrices, 16, matrices, 0);
    // Transform clip-space point to camera-space.
    Matrix.multiplyMV(points, 4, matrices, 16, points, 0);
    // points[4,5,6] is now a camera-space vector.  Transform to world space to get a point
    // along the ray.
    float[] out = new float[6];
    frame.getPose().transformPoint(points, 4, out, 3);
    // use points[8,9,10] as a zero vector to get the ray head position in world space.
    frame.getPose().transformPoint(points, 8, out, 0);
    // normalize the direction vector:
    float dx = out[3] - out[0];
    float dy = out[4] - out[1];
    float dz = out[5] - out[2];
    float scale = 1.0f / (float) Math.sqrt(dx*dx + dy*dy + dz*dz);
    out[3] = dx * scale;
    out[4] = dy * scale;
    out[5] = dz * scale;
    return out;
}

如果每帧调用几次,请参阅有关getProjectionMatrixinvertM调用的注释.

If you're calling this several times per frame see the comment about the getProjectionMatrix and invertM calls.

这篇关于如何在ARCore中检查与对象的光线相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆