From delta3d Wiki
Jump to: navigation, search

Object Motion Model Part I

In this tutorial, you will change the previous Hello World application to further your understanding of motion models. Previously you learned how to place a camera in a scene and move the camera position via mouse and keyboard inputs. Now we will reverse this situation and move the object while the camera is static.

Additional Tutorials that will apply:
HelloWorld

The Header File

In your HelloWorldApp header file simply change the name of your mTerrainObject to mTextObject.

class HelloWorldApp : public dtABC::Application
{
   public:
      HelloWorldApp(const std::string& configFilename);

      // Override this function to setup your scene.
      virtual void Config();

   protected:
      // Destructors for subclasses of dtCore::Base must have a protected
      // destructor. Otherwise use of RefPtrs will cause some serious
      // problems if the objects are allocated on the stack.
      virtual ~HelloWorldApp();

   private:
      // dtCore::RefPtr is a template for a smart pointer that takes
      // care of reference counting for objects allocated on the heap.
      // It is good practice to store all objects that derive from
      // dtCore::Base inside a RefPtr.
      dtCore::RefPtr<dtCore::Object> mTextObject;
      dtCore::RefPtr<dtCore::OrbitMotionModel> mMotionModel;
};

The Implementation File

This is a simple change to the existing HelloWorldApp::Config() function:

  1. First open the HelloWorldApp.cpp file.
  2. Load the HelloWorld.flt file to your mTextObject.
  3. Set mTextObject as the target of your mMotionModel. A motion model uses input to alter the transform of its target.
  4. Finally, add the loaded .flt to the scene.

Your code should look like the following:

void HelloWorldApp::Config()
{
   // Call the parent class, in case something important is happening there
   Application::Config();

   GetWindow()->SetWindowTitle("HelloWorldApp2");

   // Adjust the Camera position by instantiating a transform object to
   // store the camera position and attitude.
   dtCore::Transform camPosition(0.f, -100.f, 10.f, 0.f, 0.f, 0.f);
   GetCamera()->SetTransform(camPosition);

   // Setting a motion model for the camera
   mMotionModel = new dtCore::OrbitMotionModel(GetKeyboard(), GetMouse());

   // Allocate a dtCore::Object. This class will be your basic container
   // for 3D meshes.
   mTextObject = new dtCore::Object("Text");

   // Load the model file, in this case an OpenFlight model (.flt)
   mTextObject->LoadFile("HelloWorld.flt");

   // Setting the camera as a target for the motion model. The camera will
   // will be static, and the camera will move the object using the right
   // clicked mouse.
   mMotionModel->SetTarget(mTextObject);

   // Add the Object to the scene. Since the object is a RefPtr, we must
   // pull the internal point out to pass it to the Scene.
   AddDrawable(mTextObject.get());
}

Everything else in the .h .cpp files remains the same.

Running

Now let's compile and run the app! You should see an screen similar to this: Hello World 2 new.png

Now if you right click the mouse you will be able to move the model around.

That's the end of the Delta3D Hello World 2 tutorial. To better learn the application, play with it a little: search the web for others 3D models and change the motion model used to see how it will behave. Remember that you may need to adjust the camera position depending on the model used to get a good initial view!

Complete files to download: Hello_World_2_Models.zip