Panda3D Manual: Loading and Animating the Panda Model

Contents

Actors

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).

#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 environ = window->load_model(framework.get_models(), "models/environment");
environ.reparent_to(window->get_render());
environ.set_scale(0.25 , 0.25, 0.25);
environ.set_pos(-8, 42, 0);
 
// Load our panda
NodePath pandaActor = window->load_model(framework.get_models(), "panda-model");
pandaActor.set_scale(0.005);
pandaActor.reparent_to(window->get_render());
 
// Load the walk animation
window->load_model(pandaActor, "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, (void*) NULL));
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:

Tutorial3.jpg