How can I draw and rotate an Arrow at an Orientation in 3D Space?

≡放荡痞女 提交于 2020-01-02 18:55:38

问题


I want to draw an image like the following in a certain orientation.

Assume that the device lies flat on a table the image above shows how the arrow should be drawn. The angle between the device and the table is called alpha and it is zero in this case. The arrow points straight to a predefined location point. If you rotate the device on the table the arrow will point alway to the target direction just like a compass.

I did the update of the image position with the following code in 2D space:

// Create the image for the compass
let arrowImageView: UIImageView = UIImageView(frame: CGRectMake(100, 200, 100, 100))
arrowImageView.image = UIImage(named: "arrow.png")
self.view.addSubview(arrowImageView)
arrowImageView.center = self.view.center


func locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!) {

    var direction: Float = Float(newHeading.magneticHeading)

    if direction > 180 {
      direction = 360 - direction
    }
    else {
      direction = 0 - direction
    }

    // Rotate the arrow image
    if let arrowImageView = self.arrowImageView {
      UIView.animateWithDuration(3.0, animations: { () -> Void in
        arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(self.degreesToRadians(direction) + self.angle))

      })
    }

    let currentLocation: CLLocation = manager.location
}

However if the device is lifted up from the table the value of alpha increases (alpha > 0). The arrow image should still point to the target location but should be drawn in perspective keeping the alpha angle into account. Here is how this looks from the side.

The only thing which changes in 3D space is the alpha angle. All other angles remain constant.

How can I draw and rotate an Arrow image at an Orientation (given alpha angle) in 3D Space?

来源:https://stackoverflow.com/questions/31643371/how-can-i-draw-and-rotate-an-arrow-at-an-orientation-in-3d-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!