Rotate rotates the camera around the target by the specified angles.
(thetaDelta, phiDelta float32)
| 140 | |
| 141 | // Rotate rotates the camera around the target by the specified angles. |
| 142 | func (oc *OrbitControl) Rotate(thetaDelta, phiDelta float32) { |
| 143 | |
| 144 | const EPS = 0.0001 |
| 145 | |
| 146 | // Compute direction vector from target to camera |
| 147 | tcam := oc.cam.Position() |
| 148 | tcam.Sub(&oc.target) |
| 149 | |
| 150 | // Calculate angles based on current camera position plus deltas |
| 151 | radius := tcam.Length() |
| 152 | theta := math32.Atan2(tcam.X, tcam.Z) + thetaDelta |
| 153 | phi := math32.Acos(tcam.Y/radius) + phiDelta |
| 154 | |
| 155 | // Restrict phi and theta to be between desired limits |
| 156 | phi = math32.Clamp(phi, oc.MinPolarAngle, oc.MaxPolarAngle) |
| 157 | phi = math32.Clamp(phi, EPS, math32.Pi-EPS) |
| 158 | theta = math32.Clamp(theta, oc.MinAzimuthAngle, oc.MaxAzimuthAngle) |
| 159 | |
| 160 | // Calculate new cartesian coordinates |
| 161 | tcam.X = radius * math32.Sin(phi) * math32.Sin(theta) |
| 162 | tcam.Y = radius * math32.Cos(phi) |
| 163 | tcam.Z = radius * math32.Sin(phi) * math32.Cos(theta) |
| 164 | |
| 165 | // Update camera position and orientation |
| 166 | oc.cam.SetPositionVec(oc.target.Clone().Add(&tcam)) |
| 167 | oc.cam.LookAt(&oc.target, &oc.up) |
| 168 | } |
| 169 | |
| 170 | // Zoom moves the camera closer or farther from the target the specified amount |
| 171 | // and also updates the camera's orthographic size to match. |