Loading and Animating the Panda Model

Update the Code

The Actor class which is available to Python users is not available to C++ users. you should create your own Actor class which at least should do the following:

  • load the Actor Model

  • load the animations

  • bind the model and the animations using AnimControl or AnimControlCollection

The next sample will load the panda model and the walk animation. the call: window->loop_animations(0); does the magic of binding all the loaded models and their animations under the node path: render . it’s very important to note that any animations loaded after the above call will not show until the same method is called again. also any animations loaded under a node path which doesn’t belong to render (for example: render_2d) will not show even if the call: window->loop_animations(0); is made. For such animations to show, other steps must be applied (more on this later).

 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
#include "pandaFramework.h"
#include "pandaSystem.h"

#include "genericAsyncTask.h"
#include "asyncTaskManager.h"

// Global stuff
PT(AsyncTaskManager) taskMgr = AsyncTaskManager::get_global_ptr();
PT(ClockObject) globalClock = ClockObject::get_global_clock();
NodePath camera;

// Task to move the camera
AsyncTask::DoneStatus SpinCameraTask(GenericAsyncTask *task, void *data) {
  double time = globalClock->get_real_time();
  double angledegrees = time * 6.0;
  double angleradians = angledegrees * (3.14 / 180.0);
  camera.set_pos(20 * sin(angleradians), -20.0 * cos(angleradians), 3);
  camera.set_hpr(angledegrees, 0, 0);

  return AsyncTask::DS_cont;
}

int main(int argc, char *argv[]) {
  // Open a new window framework and set the title
  PandaFramework framework;
  framework.open_framework(argc, argv);
  framework.set_window_title("My Panda3D Window");

  // Open the window
  WindowFramework *window = framework.open_window();
  camera = window->get_camera_group(); // Get the camera and store it

  // Load the environment model
  NodePath scene = window->load_model(framework.get_models(), "models/environment");
  scene.reparent_to(window->get_render());
  scene.set_scale(0.25, 0.25, 0.25);
  scene.set_pos(-8, 42, 0);

  // Load our panda
  NodePath pandaActor = window->load_model(framework.get_models(), "models/panda-model");
  pandaActor.set_scale(0.005);
  pandaActor.reparent_to(window->get_render());

  // Load the walk animation
  window->load_model(pandaActor, "models/panda-walk4");
  window->loop_animations(0); // bind models and animations
                              //set animations to loop

  // Add our task do the main loop, then rest in peace.
  taskMgr->add(new GenericAsyncTask("Spins the camera", &SpinCameraTask, nullptr));
  framework.main_loop();
  framework.close_framework();
  return 0;
}

We are first loading the model file and the animation file like ordinary models. Then, we are simply calling loop_animations(0) to loop all animations.

Run the Program

The result is a panda walking in place as if on a treadmill:

../../../_images/tutorial3.jpg