Tyson
Tyson

Reputation: 1

Why custom created entity does not rotate with BodyAnchor's rotation?

I've created a custom entity named "BodyEntity" which is subclassed from Entity, where i have a update method which constantly updates the sphere entites i have placed on the joints based on person's current movement and this all is working very fine. But the problem is when person rotates himself(i.e. let's say person is facing towards camera then changes it's orientation and is now facing towards his left) the spheres (which i have added at the position of joints) doesn't change its rotation or position based on updated bodyanchor's position

Follwing is my code to update my custom created BodyEntity using didUpdate anchors method

func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
        for anchor in anchors {
            guard let bodyAnchor = anchor as? ARBodyAnchor else { continue }
            if let entity = bodyEntity {
                entity.updateTrackedBodyAnchor(bodyEntity: bodyAnchor)
            } else {
                bodyEntity = BodySkeleton(bodyEntity: bodyAnchor)
                guard let bodyTrackedEntity = bodyEntity else { continue }
                anchorSkeletonEntity.addChild(bodyTrackedEntity)
            }
        }
        anchorSkeletonEntity.orientation = Transform(matrix: bodyAnchor.transform).rotation
        // bodyEntity?.orientation = Transform(matrix: bodyAnchor.transform).rotation
    }

And here's the update method which i have in my BodyEntity class

    func updateTrackedBodyAnchor(bodyEntity: ARBodyAnchor) {
        /// get the position of root(hip) joint
        let hipPosition = Transform(matrix: bodyEntity.transform).translation
        for jointName in ARSkeletonDefinition.defaultBody3D.jointNames {
            ///  get the position of joint
            let jointModelTransform = bodyEntity.skeleton.modelTransform(for: ARSkeleton.JointName(rawValue: jointName))!
            /// Now here when we get the translation of the joint it's actually is not the world position of that joint
            /// It's actually the offset of that joint from the root joint
            let jointModelOffset = Transform(matrix: jointModelTransform).translation
            /// So inorder to get the actual world position of the joint we need to add hip's position & joint's offset
            let jointPosition = hipPosition + jointModelOffset
            guard let jointToUpdate = joints[jointName] else { continue }
            jointToUpdate.transform.translation = jointPosition
//          jointToUpdate.transform.rotation = Transform(matrix: bodyEntity.transform).rotation
        }
    }

what i tried is changing the orientation of my bodyEntity and also for anchorSkeletonEntity but none of them seems to works as i expected as you can see in my didUpdate anchors method also i've tried to set orientation on my sphere entity like you can see in updateTrackedBodyAnchor (last line) but applying this it just rotates sphere on its own axis

I have also tried solution from this link

I just want my BodyEntity's rotation/orientation same as ARBodyAnchor's just like apple provides in it's own demo app

Upvotes: 0

Views: 43

Answers (0)

Related Questions