Using Intervals to move the Panda

Intervals and Sequences

Intervals

Intervals are tasks that change a property from one value to another over a specified period of time. Starting an interval effectively starts a background process that modifies the property over the specified period of time.

Sequences

Sequences, sometimes called MetaIntervals, are a type of interval that contains other intervals. Playing a sequence will cause each contained interval to execute in sequence, one after the other.

The Program

Update the Code

The next step is to cause the panda to actually move back and forth. Update the code to the following:

 1#include "pandaFramework.h"
 2#include "pandaSystem.h"
 3
 4#include "cIntervalManager.h"
 5#include "cLerpNodePathInterval.h"
 6#include "cMetaInterval.h"
 7
 8int main(int argc, char *argv[]) {
 9  // Open a new window framework and set the title
10  PandaFramework framework;
11  framework.open_framework(argc, argv);
12  framework.set_window_title("My Panda3D Window");
13
14  // Open the window
15  WindowFramework *window = framework.open_window();
16  NodePath camera = window->get_camera_group(); // Get the camera and store it
17
18  // Load the environment model
19  NodePath scene = window->load_model(framework.get_models(), "models/environment");
20  scene.reparent_to(window->get_render());
21  scene.set_scale(0.25, 0.25, 0.25);
22  scene.set_pos(-8, 42, 0);
23
24  // Load our panda
25  NodePath panda = window->load_model(framework.get_models(), "models/panda-model");
26  panda.set_scale(0.005);
27  panda.reparent_to(window->get_render());
28
29  // Load the walk animation
30  window->load_model(panda, "models/panda-walk4");
31  window->loop_animations(0);
32
33  // Create the lerp intervals needed to walk back and forth
34  PT(CLerpNodePathInterval) pandaPosInterval1, pandaPosInterval2,
35    pandaHprInterval1, pandaHprInterval2;
36  pandaPosInterval1 = new CLerpNodePathInterval("pandaPosInterval1",
37    13.0, CLerpInterval::BT_no_blend,
38    true, false, panda, NodePath());
39  pandaPosInterval1->set_start_pos(LPoint3(0, 10, 0));
40  pandaPosInterval1->set_end_pos(LPoint3(0, -10, 0));
41
42  pandaPosInterval2 = new CLerpNodePathInterval("pandaPosInterval2",
43    13.0, CLerpInterval::BT_no_blend,
44    true, false, panda, NodePath());
45  pandaPosInterval2->set_start_pos(LPoint3(0, -10, 0));
46  pandaPosInterval2->set_end_pos(LPoint3(0, 10, 0));
47
48  pandaHprInterval1 = new CLerpNodePathInterval("pandaHprInterval1", 3.0,
49    CLerpInterval::BT_no_blend,
50    true, false, panda, NodePath());
51  pandaHprInterval1->set_start_hpr(LPoint3(0, 0, 0));
52  pandaHprInterval1->set_end_hpr(LPoint3(180, 0, 0));
53
54  pandaHprInterval2 = new CLerpNodePathInterval("pandaHprInterval2", 3.0,
55    CLerpInterval::BT_no_blend,
56    true, false, panda, NodePath());
57  pandaHprInterval2->set_start_hpr(LPoint3(180, 0, 0));
58  pandaHprInterval2->set_end_hpr(LPoint3(0, 0, 0));
59
60  // Create and play the sequence that coordinates the intervals
61  PT(CMetaInterval) pandaPace;
62  pandaPace = new CMetaInterval("pandaPace");
63  pandaPace->add_c_interval(pandaPosInterval1, 0,
64    CMetaInterval::RS_previous_end);
65  pandaPace->add_c_interval(pandaHprInterval1, 0,
66    CMetaInterval::RS_previous_end);
67  pandaPace->add_c_interval(pandaPosInterval2, 0,
68    CMetaInterval::RS_previous_end);
69  pandaPace->add_c_interval(pandaHprInterval2, 0,
70    CMetaInterval::RS_previous_end);
71  pandaPace->loop();
72
73  // Add our task, which can be any function or lambda that returns DoneStatus.
74  framework.get_task_mgr().add("SpinCameraTask", [=](AsyncTask *task) mutable {
75    // Calculate the new position and orientation (inefficient - change me!)
76    double angledegrees = task->get_elapsed_time() * 6.0;
77    double angleradians = angledegrees * (3.14 / 180.0);
78    camera.set_pos(20 * sin(angleradians), -20.0 * cos(angleradians), 3);
79    camera.set_hpr(angledegrees, 0, 0);
80
81    // Tell the task manager to continue this task the next frame.
82    return AsyncTask::DS_cont;
83  });
84
85  // Add another task to step the interval manager.
86  framework.get_task_mgr().add("intervals", [](AsyncTask *task) {
87    CIntervalManager::get_global_ptr()->step();
88    return AsyncTask::DS_cont;
89  });
90
91  // Run Panda's main loop until the user closes the window.
92  framework.main_loop();
93  return 0;
94}

When the pandaPosInterval1 interval is started, it will gradually adjust the position of the panda from (0, 10, 0) to (0, -10, 0) over a period of 13 seconds. Similarly, when the pandaHprInterval1 interval is started, the heading of the panda will rotate 180 degrees over a period of 3 seconds.

The pandaPace sequence above causes the panda to move in a straight line, turn, move in the opposite straight line, and finally turn again. The code pandaPace.loop() causes the Sequence to be started in looping mode.

Run the Program

The result of all this is to cause the panda to pace back and forth from one tree to the other.