c++的3D游戏笔录-panda3d(4)
1、要创建自己的Actor类,至少完成以下工作:
加载Actor模型
加载动画
使用AnimControl或AnimControlCollection建造模型和动画
2、加载panda模型和行走动画,window->loop_animations(0)。构造所有加载后的模型和它们的动画,在节点路径render之下。
3、一些加载的动画在调用之后,直到下一个同样的方法再次调用将不会显示。
4、任何不属于render的节点路径下的加载的动画,例如render_2d,都不会显示,即使调用了window->loop_animations(0)。要显示这样的动画,必须应用一些其它步骤。
5、首先加载模型文件
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");
比如常见模型,然后,我们简单调用loop_animations(0),循环所有动画。
window->loop_animations(0); // bind models and animations
//set animations to loop
#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;
}
6、间隔
间隔是在指定的时间段内将属性从一个值更改为另一个值的任务。启动间隔可以有效地启动一个后台进程,该进程在指定的时间段内修改属性。
// Create the lerp intervals needed to walk back and forth
PT(CLerpNodePathInterval) pandaPosInterval1, pandaPosInterval2,
pandaHprInterval1, pandaHprInterval2;
pandaPosInterval1 = new CLerpNodePathInterval("pandaPosInterval1",
13.0, CLerpInterval::BT_no_blend,
true, false, pandaActor, NodePath());
pandaPosInterval1->set_start_pos(LPoint3(0, 10, 0));
pandaPosInterval1->set_end_pos(LPoint3(0, -10, 0));
pandaPosInterval2 = new CLerpNodePathInterval("pandaPosInterval2",
13.0, CLerpInterval::BT_no_blend,
true, false, pandaActor, NodePath());
pandaPosInterval2->set_start_pos(LPoint3(0, -10, 0));
pandaPosInterval2->set_end_pos(LPoint3(0, 10, 0));
pandaHprInterval1 = new CLerpNodePathInterval("pandaHprInterval1", 3.0,
CLerpInterval::BT_no_blend,
true, false, pandaActor, NodePath());
pandaHprInterval1->set_start_hpr(LPoint3(0, 0, 0));
pandaHprInterval1->set_end_hpr(LPoint3(180, 0, 0));
pandaHprInterval2 = new CLerpNodePathInterval("pandaHprInterval2", 3.0,
CLerpInterval::BT_no_blend,
true, false, pandaActor, NodePath());
pandaHprInterval2->set_start_hpr(LPoint3(180, 0, 0));
pandaHprInterval2->set_end_hpr(LPoint3(0, 0, 0));
(1)pandaPosInterval1间隔调整熊猫的位置从 (0, 10, 0) 到 (0, -10, 0),每13秒调整一次。
(2)同样,当pandaHprInterval1间隔启动后,panda的方向将旋转180度每3秒。
7、序列
序列有时称为MetaIntervals,是一种包含其他间隔的间隔类型。播放一个序列将导致每个包含的间隔按顺序执行,一个接一个。
// Create and play the sequence that coordinates the intervals
PT(CMetaInterval) pandaPace;
pandaPace = new CMetaInterval("pandaPace");
pandaPace->add_c_interval(pandaPosInterval1, 0,
CMetaInterval::RS_previous_end);
pandaPace->add_c_interval(pandaHprInterval1, 0,
CMetaInterval::RS_previous_end);
pandaPace->add_c_interval(pandaPosInterval2, 0,
CMetaInterval::RS_previous_end);
pandaPace->add_c_interval(pandaHprInterval2, 0,
CMetaInterval::RS_previous_end);
pandaPace->loop();
整个序列完成:
熊猫在直线上移动,转弯,在相反的直线上移动,最后再次转弯。
代码pandpace .loop()使Sequence以循环模式启动。
全部程序如下:
#include "pandaFramework.h"
#include "pandaSystem.h"
#include "genericAsyncTask.h"
#include "asyncTaskManager.h"
#include "cIntervalManager.h"
#include "cLerpNodePathInterval.h"
#include "cMetaInterval.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("game2");
// 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);
// Create the lerp intervals needed to walk back and forth
PT(CLerpNodePathInterval) pandaPosInterval1, pandaPosInterval2,
pandaHprInterval1, pandaHprInterval2;
pandaPosInterval1 = new CLerpNodePathInterval("pandaPosInterval1",
13.0, CLerpInterval::BT_no_blend,
true, false, pandaActor, NodePath());
pandaPosInterval1->set_start_pos(LPoint3(0, 10, 0));
pandaPosInterval1->set_end_pos(LPoint3(0, -10, 0));
pandaPosInterval2 = new CLerpNodePathInterval("pandaPosInterval2",
13.0, CLerpInterval::BT_no_blend,
true, false, pandaActor, NodePath());
pandaPosInterval2->set_start_pos(LPoint3(0, -10, 0));
pandaPosInterval2->set_end_pos(LPoint3(0, 10, 0));
pandaHprInterval1 = new CLerpNodePathInterval("pandaHprInterval1", 3.0,
CLerpInterval::BT_no_blend,
true, false, pandaActor, NodePath());
pandaHprInterval1->set_start_hpr(LPoint3(0, 0, 0));
pandaHprInterval1->set_end_hpr(LPoint3(180, 0, 0));
pandaHprInterval2 = new CLerpNodePathInterval("pandaHprInterval2", 3.0,
CLerpInterval::BT_no_blend,
true, false, pandaActor, NodePath());
pandaHprInterval2->set_start_hpr(LPoint3(180, 0, 0));
pandaHprInterval2->set_end_hpr(LPoint3(0, 0, 0));
// Create and play the sequence that coordinates the intervals
PT(CMetaInterval) pandaPace;
pandaPace = new CMetaInterval("pandaPace");
pandaPace->add_c_interval(pandaPosInterval1, 0,
CMetaInterval::RS_previous_end);
pandaPace->add_c_interval(pandaHprInterval1, 0,
CMetaInterval::RS_previous_end);
pandaPace->add_c_interval(pandaPosInterval2, 0,
CMetaInterval::RS_previous_end);
pandaPace->add_c_interval(pandaHprInterval2, 0,
CMetaInterval::RS_previous_end);
pandaPace->loop();
// Add our task.
taskMgr->add(new GenericAsyncTask("Spins the camera",
&SpinCameraTask, nullptr));
// This is a simpler way to do stuff every frame,
// if you're too lazy to create a task.
Thread* current_thread = Thread::get_current_thread();
while (framework.do_frame(current_thread)) {
// Step the interval manager
CIntervalManager::get_global_ptr()->step();
}
framework.close_framework();
return 0;
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfkfekb
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24