SCNCamera limit arcball rotation

前端 未结 2 1537
天涯浪人
天涯浪人 2020-12-09 13:08

I have a scene setup with SCNCamera that rotates around an object.

What would be the best way to limit the extents of rotation the camera can achieve around the obj

2条回答
  •  情话喂你
    2020-12-09 13:32

    It looks like you're almost there, using just the @Rickster code from the answer you cited.

    The change you could make would be in these lines:

    self.cameraOrbit.eulerAngles.y = Float(-2 * M_PI) * widthRatio
    self.cameraOrbit.eulerAngles.x = Float(-M_PI) * heightRatio
    

    which implicitly allow pitch and yaw to cover the entire sphere. That's where you can do your limiting. For instance, instead of allowing the pitch (eulerAngles.x) to vary from 0 to -π, you could do

    self.cameraOrbit.eulerAngles.x = Float(-M_PI_2) + Float(-M_PI_2) * heightRatio
    

    to vary smoothly between -π/2 and -π, using full screen vertical scrolling to cover that range. Or you could put hard min/max limits/checks in those two lines to constrain to a particular area of the globe.

    (Edit to address the inertia comment)

    For rotational damping, or inertia, I'd approach it by using the built in SceneKit Physics, and perhaps put the camera on an invisible (no geometry) SCNNode. That camera node becomes a gimbal, similar to the approach taken in this project: An interactive seven-foot globe created entirely in RubyMotion and SceneKit.

    The virtual gimbal then gets an SCNPhysicsBody (you add that, it doesn't come with one by default) with some damping. Or perhaps you put the physics on your central object, and give that object some angularDamping.

提交回复
热议问题