Orbit the camera around an object

View inQMLC++
View on GitHub
Sample viewer app

Fix the camera to point at and rotate around a target object.

screenshot

Use case

The orbit geoelement camera controller provides control over the following camera behaviors:

  • automatically track the target
  • stay near the target by setting a minimum and maximum distance offset
  • restrict where you can rotate around the target
  • automatically rotate the camera when the target's heading and pitch changes
  • disable user interactions for rotating the camera
  • animate camera movement over a specified duration
  • control the vertical positioning of the target on the screen
  • set a target offset (e.g.to orbit around the tail of the plane) instead of defaulting to orbiting the center of the object

How to use the sample

The sample loads with the camera orbiting an plane model. The camera is preset with a restricted camera heading and pitch, and a limited minimum and maximum camera distance set from the plane. The position of the plane on the screen is also set just below center.

Use the "Camera Heading" slider to adjust the camera heading. Select the "Allow camera distance interaction" checkbox to allow zooming in and out with the mouse/keyboard: when the checkbox is deselected the user will be unable to adjust with the camera distance.

Use the "Plane Pitch" slider to adjust the plane's pitch. When in Center view, the plane's pitch will change independently to that of the camera pitch.

Use the "Cockpit view" button to offset and fix the camera into the cockpit of the plane. Use the "Plane pitch" slider to control the pitch of plane: the camera will follow the pitch of the plane in this mode. In this view adjusting the camera distance is disabled. Use the "Center view" button to exit cockpit view mode and fix the camera controller on the center of the plane.

How it works

  1. Instantiate an OrbitGeoElementCameraController with GeoElement and camera distance as parameters.
  2. Use sceneView::setCameraController(OrbitCameraController) to set the camera to the scene view.
  3. Set the heading, pitch and distance camera properties with:
  • orbitCameraController::setCameraHeadingOffset(double)
  • orbitCameraController::setCameraPitchOffset(double)
  • orbitCameraController::setCameraDistance(double)
  1. Set the minimum and maximum angle of heading and pitch, and minimum and maximum distance for the camera with:
  • orbitCameraController::setMinCameraHeadingOffset(double) or setMaxCameraHeadingOffset(double)
  • orbitCameraController::setMinCameraHeadingOffset(double) or setMaxCameraPitchOffset(double)
  • orbitCameraController::setMinCameraHeadingOffset(double) or setMaxCameraDistance(double)
  1. Set the distance from which the camera is offset from the plane with:
  • orbitCameraController::setTargetOffsets(x, y, z, duration)
  • orbitCameraController::setTargetOffsetX(double)
  • orbitCameraController::setTargetOffsetY(double)
  • orbitCameraController::setTargetOffsetZ(double)
  1. Set the vertical screen factor to determine where the plane appears in the scene:
  • orbitCameraController::setTargetVerticalScreenFactor(float)
  1. Animate the camera to the cockpit using orbitCameraController::setTargetOffsets(x, y, z, duration)
  2. Set if the camera distance will adjust when zooming or panning using mouse or keyboard (default is true):
  • orbitCameraController::setCameraDistanceInteractive(boolean)
  1. Set if the camera will follow the pitch of the plane (default is true):
  • orbitCameraController::setAutoPitchEnabled(boolean)

Relevant API

  • OrbitGeoElementCameraController

Offline data

Read more about how to set up the sample's offline data here.

Link Local Location
Bristol Plane Model <userhome>/ArcGIS/Runtime/Data/3D/Bristol/Collada/Bristol.dae

Tags

3D, camera, object, orbit, rotate, scene

Sample Code

OrbitCameraAroundObject.cppOrbitCameraAroundObject.cppOrbitCameraAroundObject.hOrbitCameraAroundObject.qml
Use dark colors for code blocksCopy
                                                                                                                                                                                                                                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// [WriteFile Name=OrbitCameraAroundObject, Category=Scenes]
// [Legal]
// Copyright 2019 Esri.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "OrbitCameraAroundObject.h"

#include "ArcGISTiledElevationSource.h"
#include "Graphic.h"
#include "ModelSceneSymbol.h"
#include "OrbitGeoElementCameraController.h"
#include "Scene.h"
#include "SceneQuickView.h"
#include "SimpleRenderer.h"

#include <QDir>

#ifdef Q_OS_IOS
#include <QStandardPaths>
#endif // Q_OS_IOS

using namespace Esri::ArcGISRuntime;

namespace
{
  // helper method to get cross platform data path
  QString defaultDataPath()
  {
    QString dataPath;

    #ifdef Q_OS_ANDROID
      dataPath = "/sdcard";
    #elif defined Q_OS_IOS
      dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
    #else
      dataPath = QDir::homePath();
    #endif

    return dataPath;
  }
}

OrbitCameraAroundObject::OrbitCameraAroundObject(QObject* parent /* = nullptr */):
  QObject(parent),
  m_scene(new Scene(BasemapStyle::ArcGISImageryStandard, this))
{
  // create a new elevation source from Terrain3D REST service
  ArcGISTiledElevationSource* elevationSource = new ArcGISTiledElevationSource(
        QUrl("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"), this);

  // add the elevation source to the scene to display elevation
  m_scene->baseSurface()->elevationSources()->append(elevationSource);
}

OrbitCameraAroundObject::~OrbitCameraAroundObject() = default;

void OrbitCameraAroundObject::init()
{
  // Register classes for QML
  qmlRegisterType<SceneQuickView>("Esri.Samples", 1, 0, "SceneView");
  qmlRegisterType<OrbitCameraAroundObject>("Esri.Samples", 1, 0, "OrbitCameraAroundObjectSample");
}

SceneQuickView* OrbitCameraAroundObject::sceneView() const
{
  return m_sceneView;
}

// Set the view (created in QML)
void OrbitCameraAroundObject::setSceneView(SceneQuickView* sceneView)
{
  if (!sceneView || sceneView == m_sceneView)
  {
    return;
  }

  m_sceneView = sceneView;
  m_sceneView->setArcGISScene(m_scene);

  /* Scene Rendering setup */
  // create a new graphics overlay and add it to the sceneview
  GraphicsOverlay* sceneOverlay = new GraphicsOverlay(this);
  sceneOverlay->setSceneProperties(LayerSceneProperties(SurfacePlacement::Relative));
  m_sceneView->graphicsOverlays()->append(sceneOverlay);

  // create a renderer to control the plane's orientation by its attributes
  SimpleRenderer* renderer3D = new SimpleRenderer(this);
  {
    //Set the renderer pitch/heading expressions, so the plane can be oriented via properties.
    RendererSceneProperties properties = renderer3D->sceneProperties();
    properties.setPitchExpression("[PITCH]");
    properties.setHeadingExpression("[HEADING]");
    renderer3D->setSceneProperties(properties);
  }
  sceneOverlay->setRenderer(renderer3D);


  /* Plane model Setup */
  //Create a plane model.
  QUrl planeFileURl = QUrl(defaultDataPath() + "/ArcGIS/Runtime/Data/3D/Bristol/Collada/Bristol.dae");
  ModelSceneSymbol* planeModel = new ModelSceneSymbol(planeFileURl, 1.0, this);

  //Create a graphic from the plane model, positioned atop a runway.
  Point runwayPos = Point{6.637, 45.399, 100, SpatialReference::wgs84()};
  m_planeGraphic = new Graphic(runwayPos, planeModel, this);

  //Create the plane orientation attributes that are going to be used. Orient the heading 45 degs so the plane faces down the runway.
  m_planeGraphic->attributes()->insertAttribute("PITCH", 0.0);
  m_planeGraphic->attributes()->insertAttribute("HEADING", 45.0);

  //Add the plane graphic to the scene.
  m_sceneView->graphicsOverlays()->at(0)->graphics()->append(m_planeGraphic);


  /* Orbiting Camera setup */
  // create the camera controller to follow the plane
  m_orbitCam = new OrbitGeoElementCameraController(m_planeGraphic, 50, this);

  // restrict the camera's heading to stay behind the plane
  // Note : Since this is exposed and hooked up as a property, the heading slider in the UI will automatically adjust its bounds, try changing these values.
  m_orbitCam->setMinCameraHeadingOffset(-45);
  m_orbitCam->setMaxCameraHeadingOffset(45);
  emit cameraHeadingBoundsChanged();

  // restrict the camera's pitch so it doesn't go completely vertical or collide with the ground
  m_orbitCam->setMinCameraPitchOffset(10);
  m_orbitCam->setMaxCameraPitchOffset(100);

  // restrict the camera to stay between 10 and 1000 meters from the plane
  m_orbitCam->setMinCameraDistance(10);
  m_orbitCam->setMaxCameraDistance(100);

  // position the plane a third from the bottom of the screen
  m_orbitCam->setTargetVerticalScreenFactor(0.33f);
  // don't pitch the camera when the plane pitches
  m_orbitCam->setAutoPitchEnabled(false);

  // Forward the camera heading changed signal on to the QML property signal.
  // This allows the "camera heading" slider to react to changing the camera heading via mouse motion
  // See "onMoved" signal handler in OrbitCameraAroundObject.qml
  connect(m_orbitCam, &OrbitGeoElementCameraController::cameraHeadingOffsetChanged, this, &OrbitCameraAroundObject::cameraHeadingChanged);

  //Apply our new orbiting camera to the scene view.
  m_sceneView->setCameraController(m_orbitCam);

  emit sceneViewChanged();
}

void OrbitCameraAroundObject::cockpitView()
{
  /*
   * Animates the camera to a cockpit view. The camera's target is offset to the cockpit (instead of the plane's
   * center). The camera is moved onto the target position to create a swivelling camera effect. Auto pitch is
   * enabled so the camera pitches when the plane pitches.
   */

  //No zooming in/out by default in cockpit view
  setCamDistanceInteractionAllowed(false);

  //allow the camera to get closer to the target
  m_orbitCam->setMinCameraDistance(0);

  // animate the camera target to the cockpit instead of the center of the plane
  m_orbitCam->setTargetOffsets(0, -2, 1.1, 1);

  //The animation may rotate us over the set camera bounds based on the plane pitch, so unlock them.
  m_orbitCam->setMinCameraPitchOffset(-180.0);
  m_orbitCam->setMaxCameraPitchOffset(180.0);

  //Camera move-to-cockpit callback.
  connect(m_orbitCam, &OrbitGeoElementCameraController::moveCameraCompleted, this,
          [this](QUuid, bool succeeded)
  {
    if (succeeded)
    {
      //once the camera is in the cockpit, only allow the camera's heading to change
      m_orbitCam->setMinCameraPitchOffset(90);
      m_orbitCam->setMaxCameraPitchOffset(90);

      // pitch the camera when the plane pitches
      m_orbitCam->setAutoPitchEnabled(true);
    }
  });

  //Start the camera move into the cockpit
  //If the camera is already tracking object pitch, don't want to animate the pitch any further, we're exactly where we should be.
  m_orbitCam->moveCamera(0 - m_orbitCam->cameraDistance(),
                         0 - m_orbitCam->cameraHeadingOffset(),
                         m_orbitCam->isAutoPitchEnabled() ? 0.0 : (90 - m_orbitCam->cameraPitchOffset()) + planePitch(), 1.0);

}

void OrbitCameraAroundObject::centerView()
{
  /*
   * Configures the camera controller for a "follow" view. The camera targets the center of the plane with a default
   * position directly behind and slightly above the plane. Auto pitch is disabled so the camera does not pitch when
   * the plane pitches.
   */

  setCamDistanceInteractionAllowed(true);
  m_orbitCam->setAutoPitchEnabled(false);
  m_orbitCam->setTargetOffsetX(0);
  m_orbitCam->setTargetOffsetY(0);
  m_orbitCam->setTargetOffsetZ(0);
  m_orbitCam->setCameraHeadingOffset(0);
  m_orbitCam->setMinCameraPitchOffset(10);
  m_orbitCam->setMaxCameraPitchOffset(100);
  m_orbitCam->setCameraPitchOffset(45);
  m_orbitCam->setMinCameraDistance(10.0);
  m_orbitCam->setCameraDistance(50.0);
}

// QML Property methods, (see the Q_PROPERTY declarations in header file,) for allowing the model to interact with the UI
// We need to do nullptr checks here as it's entirely possible, and even expected, that these getter properties will be called by the UI
// before setSceneView has been called. Objects like the camera are not initialised until then, so return sensible defaults if the object is null.
bool OrbitCameraAroundObject::camDistanceInteractionAllowed() const
{
  return m_orbitCam != nullptr ? m_orbitCam->isCameraDistanceInteractive() : true;
}

void OrbitCameraAroundObject::setCamDistanceInteractionAllowed(bool allowed)
{
  //Check the allowed flags arn't the same on set to avoid a binding loop
  if ((m_orbitCam != nullptr) && (allowed != m_orbitCam->isCameraDistanceInteractive()))
  {
    m_orbitCam->setCameraDistanceInteractive(allowed);
    emit camDistanceInteractionAllowedChanged();
  }
}

double OrbitCameraAroundObject::cameraHeading() const
{
  return m_orbitCam != nullptr ? m_orbitCam->cameraHeadingOffset() : 0.0;
}

void OrbitCameraAroundObject::setCameraHeading(double heading)
{
  if (m_orbitCam != nullptr)
  {
    m_orbitCam->setCameraHeadingOffset(heading);
    emit cameraHeadingChanged();
  }
}

float OrbitCameraAroundObject::planePitch() const
{
  if(!m_planeGraphic->attributes()->containsAttribute("PITCH"))
  {
    return 0.0;
  }

  return m_planeGraphic->attributes()->attributeValue("PITCH").toFloat();
}

void OrbitCameraAroundObject::setPlanePitch(float pitch)
{
  if(!m_planeGraphic->attributes()->containsAttribute("PITCH"))
  {
     m_planeGraphic->attributes()->insertAttribute("PITCH", pitch);
  }
  else
  {
     m_planeGraphic->attributes()->replaceAttribute("PITCH", pitch);
  }
  emit planePitchChanged();
}

QPointF OrbitCameraAroundObject::cameraHeadingBounds() const
{
  return m_orbitCam != nullptr ? QPointF{m_orbitCam->minCameraHeadingOffset(), m_orbitCam->maxCameraHeadingOffset()}
                               : QPointF{-45.0, 45.0};
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.