Hi all,
I am trying out Panda3d/C++ and at the moment working through the Solar System sample, converting Python code to C++ tutorial by tutorial. All works well except that the planets revolving around Sun don’t do so at a fixed angular velocity (only observation) and that the animation jumps a few frames every few seconds. The Python code runs flawlessly. I even tried limiting the framerate but no resolve. I even tried C++ code from drunken-octo-robot’s repository and I see similar issue. I couldn’t find anything to that effect searching the forums and online so I decided to post my query here.
Thanks
solarsystem.h
#ifndef SOLARSYSTEM_H
#define SOLARSYSTEM_H
#include "pandaFramework.h"
#include "pandaSystem.h"
#include "texture.h"
#include "texturePool.h"
#include "cLerpNodePathInterval.h"
#include "lvecBase3.h"
#include "cIntervalManager.h"
#include "cMetaInterval.h"
#include "asyncTaskManager.h"
#include "clockObject.h"
#include <iostream>
class solarsystem
{
private:
PandaFramework framework;
WindowFramework* window;
float sizeScale = 0.6f;
float orbitScale = 10.0f;
float yearScale = 60.0f;
float dayScale = yearScale / 356.0f * 5.0f;
NodePath sky, sun, mercury, venus, earth, mars, moon;
NodePath orbit_root_mercury, orbit_root_venus, orbit_root_earth, orbit_root_mars, orbit_root_moon;
CLerpNodePathInterval *day_period_sun, *day_period_mercury, *day_period_venus, *day_period_earth, *day_period_mars, *day_period_moon;
CLerpNodePathInterval *orbit_period_mercury, *orbit_period_venus, *orbit_period_earth, *orbit_period_mars, *orbit_period_moon;
Texture* sky_tex, *sun_tex, *mercury_tex, *venus_tex, *earth_tex, *mars_tex, *moon_tex;
public:
solarsystem(int argc, char* args[]);
double timer;
void loadPlanets();
void rotatePlanets();
static AsyncTask::DoneStatus stepIntervalManager(GenericAsyncTask* taskPtr,void* dataPtr);
void run();
};
#endif // SOLARSYSTEM_H
solarsystem.cpp
#include "solarsystem.h"
using namespace std;
solarsystem::solarsystem(int argc, char* args[])
{
framework.open_framework(argc,args);
framework.set_window_title("Hello world");
window = framework.open_window();
TextNode * text = new TextNode("title");
text->set_text("Panda3D: Tutorial 1 - Solar System");
NodePath textNodePath = window->get_aspect_2d().attach_new_node(text);
textNodePath.set_pos(0,0.8,-0.95);
textNodePath.set_scale(0.07);
NodePath camera = window->get_camera_group();
camera.set_pos(0,0,45);
camera.set_hpr(0,-90,0);
timer = AsyncTaskManager::get_global_ptr()->get_clock()->get_frame_time();
}
void solarsystem::loadPlanets()
{
// /opt/panda3d/samples/Solar-System/models
orbit_root_mercury = window->get_render().attach_new_node("orbit_root_mercury");
orbit_root_venus = window->get_render().attach_new_node("orbit_root_venus");
orbit_root_earth = window->get_render().attach_new_node("orbit_root_earth");
orbit_root_moon = orbit_root_earth.attach_new_node("orbit_root_moon");
orbit_root_mars = window->get_render().attach_new_node("orbit_root_mars");
sky = window->load_model(framework.get_models(),"/opt/panda3d/samples/Solar-System/models/solar_sky_sphere");
sky.reparent_to(window->get_render());
sky.set_scale(40);
sky_tex = TexturePool::load_texture("/opt/panda3d/samples/Solar-System/models/stars_1k_tex.jpg");
sky.set_texture(sky_tex,1);
sun = window->load_model(framework.get_models(),"/opt/panda3d/samples/Solar-System/models/planet_sphere");
sun.reparent_to(window->get_render());
sun.set_scale(2 * sizeScale);
sun_tex = TexturePool::load_texture("/opt/panda3d/samples/Solar-System/models/sun_1k_tex.jpg");
sun.set_texture(sun_tex,1);
mercury = window->load_model(framework.get_models(),"/opt/panda3d/samples/Solar-System/models/planet_sphere");
mercury_tex = TexturePool::load_texture("/opt/panda3d/samples/Solar-System/models/mercury_1k_tex.jpg");
mercury.set_texture(mercury_tex,1);
mercury.reparent_to(orbit_root_mercury);
mercury.set_pos(0.38 * orbitScale,0,0);
mercury.set_scale(0.385 * sizeScale);
venus = window->load_model(framework.get_models(),"/opt/panda3d/samples/Solar-System/models/planet_sphere");
venus_tex = TexturePool::load_texture("/opt/panda3d/samples/Solar-System/models/venus_1k_tex.jpg");
venus.set_texture(venus_tex,1);
venus.reparent_to(orbit_root_venus);
venus.set_pos(0.72 * orbitScale,0,0);
venus.set_scale(0.923 * sizeScale);
earth = window->load_model(framework.get_models(),"/opt/panda3d/samples/Solar-System/models/planet_sphere");
earth_tex = TexturePool::load_texture("/opt/panda3d/samples/Solar-System/models/earth_1k_tex.jpg");
earth.set_texture(earth_tex,1);
earth.reparent_to(orbit_root_earth);
earth.set_pos(orbitScale,0,0);
earth.set_scale(sizeScale);
mars = window->load_model(framework.get_models(),"/opt/panda3d/samples/Solar-System/models/planet_sphere");
mars_tex = TexturePool::load_texture("/opt/panda3d/samples/Solar-System/models/mars_1k_tex.jpg");
mars.set_texture(mars_tex,1);
mars.reparent_to(orbit_root_mars);
mars.set_pos(1.52 * orbitScale,0,0);
mars.set_scale(0.515 * sizeScale);
orbit_root_moon.set_pos(orbitScale,0,0);
moon = window->load_model(framework.get_models(),"/opt/panda3d/samples/Solar-System/models/planet_sphere");
moon_tex = TexturePool::load_texture("/opt/panda3d/samples/Solar-System/models/moon_1k_tex.jpg");
moon.set_texture(moon_tex,1);
moon.reparent_to(orbit_root_moon);
moon.set_pos(0.1*orbitScale,0,0);
moon.set_scale(0.1 * sizeScale);
}
void solarsystem::rotatePlanets()
{
LPoint3f rotVecStart(0,0,0);
LPoint3f rotVecEnd(360,0,0.0);
NodePath render = window->get_render();
day_period_sun = new CLerpNodePathInterval("day_period_sun",20,CLerpInterval::BT_no_blend,false, true,sun,render);
day_period_sun->set_start_hpr(rotVecStart);
day_period_sun->set_end_hpr(rotVecEnd);
orbit_period_mercury = new CLerpNodePathInterval("orbit_period_mercury",0.241*yearScale,CLerpInterval::BT_no_blend,true, false,orbit_root_mercury,render);
orbit_period_mercury->set_start_hpr(rotVecStart);
orbit_period_mercury->set_end_hpr(rotVecEnd);
orbit_period_venus = new CLerpNodePathInterval("orbit_period_venus",0.615*yearScale,CLerpInterval::BT_no_blend,true, false,orbit_root_venus,render);
orbit_period_venus->set_start_hpr(rotVecStart);
orbit_period_venus->set_end_hpr(rotVecEnd);
orbit_period_earth = new CLerpNodePathInterval("orbit_period_earth",yearScale,CLerpInterval::BT_no_blend,false, false,orbit_root_earth,render);
orbit_period_earth->set_start_hpr(rotVecStart);
orbit_period_earth->set_end_hpr(rotVecEnd);
orbit_period_mars = new CLerpNodePathInterval("orbit_period_mars",1.881*yearScale,CLerpInterval::BT_no_blend,false, false,orbit_root_mars,render);
orbit_period_mars->set_start_hpr(rotVecStart);
orbit_period_mars->set_end_hpr(rotVecEnd);
orbit_period_moon = new CLerpNodePathInterval("orbit_period_moon",0.0749*yearScale,CLerpInterval::BT_no_blend,false, false,orbit_root_moon,render);
orbit_period_moon->set_start_hpr(rotVecStart);
orbit_period_moon->set_end_hpr(rotVecEnd);
day_period_sun->loop();
orbit_period_mercury->loop();
orbit_period_venus->loop();
orbit_period_earth->loop();
orbit_period_mars->loop();
orbit_period_moon->loop();
AsyncTaskManager::get_global_ptr()->add(
new GenericAsyncTask("intervalManagerTask", stepIntervalManager, this));
}
AsyncTask::DoneStatus solarsystem::stepIntervalManager(GenericAsyncTask* taskPtr,
void* dataPtr)
{
double now = AsyncTaskManager::get_global_ptr()->get_clock()->get_frame_time();
if(now - ((solarsystem*)dataPtr)->timer > 1.0/30.0 )
{
//cout << now << endl;
CIntervalManager::get_global_ptr()->step();
((solarsystem*)dataPtr)->timer = now;
}
return AsyncTask::DS_cont;
}
void solarsystem::run()
{
framework.main_loop();
framework.close_framework();
}
int main(int argc, char* args[])
{
solarsystem s(argc,args);
s.loadPlanets();
s.rotatePlanets();
s.run();
}