2

I'm have some problem to move/drag an AR object in my scene view.

// This is my object that needs to move around

@property (nonatomic, strong) SCNNode *movedObject;

// This is my method to move object around the scene view

-(void)handleMoveObjectFrom:(UIPanGestureRecognizer *)recognizer {

if (recognizer.state == UIGestureRecognizerStateBegan) {
    NSLog(@"Pan state began");
    CGPoint tapPoint = [recognizer locationInView:self.sceneView];
    NSArray <SCNHitTestResult *> *result = [self.sceneView hitTest:tapPoint options:nil];

    if ([result count] == 0) {
        return;
    }
    SCNHitTestResult *hitResult = [result firstObject];
    self.movedObject = [hitResult.node parentNode];
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
    NSLog(@"Pan State Changed");
    if (self.movedObject) {

        CGPoint tapPoint = [recognizer locationInView:self.sceneView];
        NSArray <ARHitTestResult *> *hitResults = [self.sceneView hitTest:tapPoint types:ARHitTestResultTypeFeaturePoint];
        ARHitTestResult *result = [hitResults lastObject];

        //Attempt 1
        SCNVector3 vector = SCNVector3Make(result.worldTransform.columns[3].x,
                                           result.worldTransform.columns[3].y,
                                           result.worldTransform.columns[3].z
                                           );
        self.movedObject.position = vector;

        //Attempt 2
        simd_float4x4 cameraTransform = self.sceneView.session.currentFrame.camera.transform;
        simd_float3 cameraWorldPosition = simd_make_float3(cameraTransform.columns[3]);
        NSLog(@"vector: %f, %f, %f", cameraWorldPosition.x, cameraWorldPosition.y, cameraWorldPosition.z);
        [self.movedObject setSimdPosition:cameraWorldPosition];

        //Attempt3
        SCNMatrix4 matrix = SCNMatrix4FromMat4(result.worldTransform);
        SCNVector3 vector = SCNVector3Make(matrix.m41, matrix.m42, matrix.m43);
        NSLog(@"vector: %f, %f, %f", vector.x, vector.y, vector.z);
        self.movedObject.position = vector;
        NSLog(@"Moving object position");
    }
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
    NSLog(@"Done moving object");
    self.movedObject = nil;
}
}

Any help would be highly appreciated.

Thanks.

  • What is the problem specifically? Are you unable to grab the object when you pan? Or is the drag being really laggy and odd sometimes? like not responsive really. – Alan Sep 05 '17 at 09:41
  • Most of the it does not work at all. It does work very rarely and when it work, drag is too laggy and odd. Like the apple example, moving is so smooth but this is nothing compared to that. – Md.Harish-Uz-Jaman Mridha Raju Sep 05 '17 at 11:33
  • I think your problem is that you're not actually grabbing the full parent. I had this issue before. Try grabbing the parent of the parent, or the parent's parent's parent, etc... and see if the dragging works any better. – Alan Sep 05 '17 at 11:34
  • I did try to grab the parent's parent and when i did i got nil for that SCNNode. So i'm sure if it's the main problem here. If you have any example with 3D objects where dragging works perfectly, please share if possible. – Md.Harish-Uz-Jaman Mridha Raju Sep 05 '17 at 11:41
  • I know it might seem counter-intuitive, but keep trying to go up parent levels. I had a case where I got nil for a parent and then the whole object for the parent's parent. I don't have any cases online aside from a question I posted about this matter around a month ago. I posted an answer which is basically what I've been telling you. It's here https://stackoverflow.com/questions/45509973/placing-dragging-and-removing-scnnodes-in-arkit – Alan Sep 05 '17 at 11:49
  • I did check your code earlier and the code seems same to me. But dragging is not working for me. – Md.Harish-Uz-Jaman Mridha Raju Sep 05 '17 at 11:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153691/discussion-between-md-harish-uz-jaman-mridha-raju-and-alan-s). – Md.Harish-Uz-Jaman Mridha Raju Sep 05 '17 at 13:03
  • any solutions on this? – Araib karim Oct 12 '17 at 12:01

0 Answers0