15
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ARKitでタップした座標を検出する方法

Posted at

ARKitでタップした座標を検出する方法

ARSCNView

ARを使う大前提のビュー。これがないと始まらないです。

storyboard上で紐付けを行ってください

class ViewController: UIViewController, ARSCNViewDelegate {
    @IBOutlet var sceneView: ARSCNView!

UITapGestureRecognizer

現実世界の座標には、SCNVector3を用いて取得を行います。

UITapGestureRecognizerでタップ判定を登録し、タップしたポイントを取得します。

@IBAction func tappedSceneView(_ sender: UITapGestureRecognizer) {
// sceneView上でタップした座標を検出
let tapPoint = sender.location(in: sceneView)
let results = sceneView.hitTest(tapPoint, types: .existingPlaneUsingExtent)
let hitPoint = results.first
// 現実世界の座標に変換
let point = SCNVector3.init(hitPoint.worldTransform.columns.3.x,
                          hitPoint.worldTransform.columns.3.y,
                          hitPoint.worldTransform.columns.3.z)

認識した平面に対して座標を検出できるようになります。

※3次元なので、奥行きがz軸になりますので注意してください。

15
17
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?