olexale / arkit_flutter_plugin

ARKit Flutter Plugin
MIT License
797 stars 225 forks source link

[Question] How do i get the position of the user? #88

Closed vuk64 closed 3 years ago

vuk64 commented 4 years ago

Hi there,

i would like to know how i can write an app, that places an sphere 0.5 meters in front of the user, whenever he presses a button.

Initially i tried to do this by adding a node to the controller with the position vector.Vector3(0,0,-0.5), but as it turns out, that places an sphere always in the exact same place.

How do i get an updated position?

Beste Regards

olexale commented 4 years ago

Hello,

The current pub version lacks the pointOfView transformation matrix. I've added it to the dev version. You may use it like that:

dependencies:
  arkit_plugin:
    git: git://github.com/olexale/arkit_flutter_plugin.git

Could you please check?

I've tested it with the following modified sample

import 'package:arkit_plugin/arkit_plugin.dart';
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math_64.dart';

class EarthPage extends StatefulWidget {
  @override
  _EarthPageState createState() => _EarthPageState();
}

class _EarthPageState extends State<EarthPage> {
  ARKitController arkitController;

  @override
  void dispose() {
    arkitController?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(title: const Text('Earth Sample')),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: () async {
            final material = ARKitMaterial(
              lightingModelName: ARKitLightingModel.lambert,
              diffuse: ARKitMaterialProperty(image: 'earth.jpg'),
            );
            final sphere = ARKitSphere(
              materials: [material],
              radius: 0.1,
            );

            final pointOfView = await arkitController.pointOfViewTransform();
            final translation = Matrix4.zero();
            translation.setTranslationRaw(0, 0, -0.5);

            final updatedTransform = pointOfView.multiplied(translation);

            final node = ARKitNode(
              geometry: sphere,
              position: updatedTransform.getTranslation(),
            );
            this.arkitController.add(node);
          },
        ),
        body: Container(
          child: ARKitSceneView(
            onARKitViewCreated: (arkitController) =>
                this.arkitController = arkitController,
          ),
        ),
      );
}

It looks I've messed up with coordinates a bit, but I hope you'll figure it out. Kind Regards, Oleksandr