在相机位置添加ARAnchor,而不是World Origin

时间:2017-10-19 08:49:37

标签: swift 3d scenekit augmented-reality arkit

默认情况下,添加ARAnchor时会在{​​{1}}位置添加World Origin。我正在添加地图锚点,当位置发生变化时,这些锚点被更新,锚点被推出一定距离并放在方位上。如果距离很远,则图像太小,因此其上限为14米。如果我继续走向销钉,我可以最终通过它,因为该位置再次从世界原点推出。 我已经使用下面的代码在相机原点添加引脚但它似乎仍然是世界起源,任何想法为什么?

public class MapAnchor: ARAnchor {

    public var calloutString: String?
        public var distance: Float?
        public var location: CLLocation?
        public var originLocation: CLLocation?

        public convenience init(originLocation: CLLocation, location: CLLocation, pointOfView: SCNNode) {

            pointOfView.rotation = SCNVector4(0,0,0,0)
            let povTransform = pointOfView.simdWorldTransform


            let transform = povTransform.transformMatrix(originLocation: originLocation, location: location)
            self.init(transform: transform)

            self.distance = Float(location.distance(from: originLocation))
            self.location = location
            self.originLocation = originLocation
        }         
    }

    internal extension simd_float4x4 {

        func transformMatrix(originLocation: CLLocation, location: CLLocation) -> simd_float4x4 {
            // Determine the distance and bearing between the start and end locations
            let bearing = GLKMathDegreesToRadians(Float(originLocation.coordinate.direction(to: location.coordinate)))

            var distance = Float(location.distance(from: originLocation))
            distance = distance/10
            if(distance>14) {distance=14}
            //if(computedDistance<2) {computedDistance=2}
            let position = vector_float4(0.0, 0.0, -distance, 0.0)
            let translationMatrix = matrix_identity_float4x4.translationMatrix(position)
            let rotationMatrix = matrix_identity_float4x4.rotationAroundY(radians: bearing)
            let transformMatrix = simd_mul(rotationMatrix, translationMatrix)
            return simd_mul(self, transformMatrix)
        }           
    }

    internal extension matrix_float4x4 {

        func rotationAroundY(radians: Float) -> matrix_float4x4 {
            var m : matrix_float4x4 = self;

            m.columns.0.x = cos(radians);
            m.columns.0.z = -sin(radians);

            m.columns.2.x = sin(radians);
            m.columns.2.z = cos(radians);

            return m.inverse;
        }

        func translationMatrix(_ translation : vector_float4) -> matrix_float4x4 {
            var m : matrix_float4x4 = self
            m.columns.3 = translation
            return m
        }
    }
}

0 个答案:

没有答案