1、taskMgr.add() 在panda3d的任务管理器在每个镜头中增加一个回调函数spinCameraTask() 。
回调函数用来控制相机。
AsyncTask::DoneStatus spinCameraTask(GenericAsyncTask *task, void *data) {
// Calculate the new position and orientation (inefficient - change me!)
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);
// Tell the task manager to continue this task the next frame.
return AsyncTask::DS_cont;
}
2、一旦 spinCameraTask() 返回常数AsyncTask.DS_cont,任务管理器将在每个镜头时调用该回调函数。
3、taskMgr->add() 的对象是一个AsyncTask对象,我们使用GenericAsyncTask作为任务中的一个全局函数或静态方法。
taskMgr->add(new GenericAsyncTask("Spins the camera", &spinCameraTask, nullptr));
我们也可以增加一个额外的void*参数,用于我们想要的任何数据类型的指针,它将作为一个task函数的参数传递。一个GenericAsyncTask函数必须看起来像下面这样的
AsyncTask::DoneStatus your_task(GenericAsyncTask *task, void *data) {
// Do your stuff here.
// Tell the task manager to continue this task the next frame.
// You can also pass DS_done if this task should not be run again.
return AsyncTask::DS_cont;
}
4、可使用AsyncTask的子类,且覆盖 do_task 方法,做想做的事情。
5、写AsyncTask的子类,并覆盖do_task方法,实现你的要求。
6、在我们的代码中,spinCameraTask()过程基于已经过的时间计算摄像机的期望位置。
相机每秒转动6度。
下面计算相机期待方位。
double angledegrees = time * 6.0;
double angleradians = angledegrees * (3.14 / 180.0);
第一行计算角度 ,第二行计算弧度。
set_pos() 设置相机位置。
camera.set_pos(20*sin(angleradians),-20.0*cos(angleradians),3);
记住,Y是水平的,Z是水平的。
通过改变X,Y改变位置,Z固定在高于地面 的3单位
set_hpr() 设置方向
camera.set_hpr(angledegrees, 0, 0);