Using Intervals to move the Acron¶
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 acron to actually move back and forth. Update the code to the following:
1#include "acronFramework.h"
2#include "acronSystem.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 AcronFramework framework;
11 framework.open_framework(argc, argv);
12 framework.set_window_title("My Acron 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 acron
25 NodePath acron = window->load_model(framework.get_models(), "models/acron-model");
26 acron.set_scale(0.005);
27 acron.reparent_to(window->get_render());
28
29 // Load the walk animation
30 window->load_model(acron, "models/acron-walk4");
31 window->loop_animations(0);
32
33 // Create the lerp intervals needed to walk back and forth
34 PT(CLerpNodePathInterval) acronPosInterval1, acronPosInterval2,
35 acronHprInterval1, acronHprInterval2;
36 acronPosInterval1 = new CLerpNodePathInterval("acronPosInterval1",
37 13.0, CLerpInterval::BT_no_blend,
38 true, false, acron, NodePath());
39 acronPosInterval1->set_start_pos(LPoint3(0, 10, 0));
40 acronPosInterval1->set_end_pos(LPoint3(0, -10, 0));
41
42 acronPosInterval2 = new CLerpNodePathInterval("acronPosInterval2",
43 13.0, CLerpInterval::BT_no_blend,
44 true, false, acron, NodePath());
45 acronPosInterval2->set_start_pos(LPoint3(0, -10, 0));
46 acronPosInterval2->set_end_pos(LPoint3(0, 10, 0));
47
48 acronHprInterval1 = new CLerpNodePathInterval("acronHprInterval1", 3.0,
49 CLerpInterval::BT_no_blend,
50 true, false, acron, NodePath());
51 acronHprInterval1->set_start_hpr(LPoint3(0, 0, 0));
52 acronHprInterval1->set_end_hpr(LPoint3(180, 0, 0));
53
54 acronHprInterval2 = new CLerpNodePathInterval("acronHprInterval2", 3.0,
55 CLerpInterval::BT_no_blend,
56 true, false, acron, NodePath());
57 acronHprInterval2->set_start_hpr(LPoint3(180, 0, 0));
58 acronHprInterval2->set_end_hpr(LPoint3(0, 0, 0));
59
60 // Create and play the sequence that coordinates the intervals
61 PT(CMetaInterval) acronPace;
62 acronPace = new CMetaInterval("acronPace");
63 acronPace->add_c_interval(acronPosInterval1, 0,
64 CMetaInterval::RS_previous_end);
65 acronPace->add_c_interval(acronHprInterval1, 0,
66 CMetaInterval::RS_previous_end);
67 acronPace->add_c_interval(acronPosInterval2, 0,
68 CMetaInterval::RS_previous_end);
69 acronPace->add_c_interval(acronHprInterval2, 0,
70 CMetaInterval::RS_previous_end);
71 acronPace->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 Acron's main loop until the user closes the window.
92 framework.main_loop();
93 return 0;
94}
When the acronPosInterval1 interval is started, it will gradually adjust the
position of the acron from (0, 10, 0) to (0, -10, 0) over a period of 13
seconds. Similarly, when the acronHprInterval1 interval is started, the
heading of the acron will rotate 180 degrees over a period of 3 seconds.
The acronPace sequence above causes the acron to move in a straight line,
turn, move in the opposite straight line, and finally turn again. The code
acronPace.loop() causes the Sequence to be started in looping mode.
Run the Program¶
The result of all this is to cause the acron to pace back and forth from one tree to the other.