[osg-users] AnimationPath + AnimationPathCallback
Georg Gast
schorsch_76 at gmx.de
Thu Apr 23 10:44:15 PDT 2015
Hi,
right now i try to move to different objects in my scene. I attached a own AnimamationPathCallback to 2 MatrixTransform Nodes in my scene.
My goal is, that both nodes move independently but each should notify me, when it animation is finished.
Here is my callback
Code:
// create the update callback
struct Notify : osg::AnimationPathCallback
{
Notify(osg::AnimationPath* p, bool& animation_done)
: osg::AnimationPathCallback(p) , m_animation_done(animation_done) {}
virtual void operator()(osg::Node* node,
osg::NodeVisitor* nv)
{
osg::AnimationPathCallback::operator()(node,nv);
if (m_animation_done)
return;
osg::MatrixTransform* p_mt =
dynamic_cast<osg::MatrixTransform*>(node);
if (p_mt)
{
auto p_down = dynamic_cast<Notify*>(p_mt->getUpdateCallback());
if (p_down == this)
{
auto p_path = getAnimationPath();
double current_time = getAnimationTime();
double max_time = p_path->getLastTime();
if (current_time >= max_time)
{
m_animation_done = true;
}
}
}
}
bool& m_animation_done;
};
This callback works when only one is attached to a node in the scene. When i attach two of them, with different animation path, just the one of them gets updated. In fact, i bought both OpenSceneGraph Books, but i could not find a hint why this should not happen.
This way i start the animations ....
Code:
void update_controller::start_animation(const std::vector<int>& animation, bool& animation_done)
{
animation_done = false;
osg::ref_ptr<osg::AnimationPath> p_path =
new osg::AnimationPath;
// only one shot
p_path->setLoopMode(osg::AnimationPath::NO_LOOPING);
// define the control points
assert(animation.size() >= 2);
static const osg::Vec3 delta_z( 0, 0, 1 );
size_t p = 0; float time = 0.0f;
osg::AnimationPath::ControlPoint cp(m_all_pos[animation[p]]);
p_path->insert(time, cp); time+= 0.1f;
cp = osg::AnimationPath::ControlPoint(m_all_pos[animation[p]] - delta_z);
p_path->insert(time, cp); time+= 0.1f;
p++;
assert(p == 1);
while (p < animation.size()-1)
{
cp = osg::AnimationPath::ControlPoint(m_all_pos[animation[p]] - delta_z);
p_path->insert(time, cp); time+= 0.1f;
p++;
}
cp = osg::AnimationPath::ControlPoint(m_all_pos[animation[p]]);
p_path->insert(time, cp); time+= 0.1f;
osg::ref_ptr<Notify> p_animation_cb =
new Notify(p_path.get(), animation_done );
// set this callback to the desired pins matrix
assert(m_id_player_pin != -1);
assert(m_pins[m_id_player_pin] != nullptr);
m_pins[m_id_player_pin]->setUpdateCallback(p_animation_cb.get());
}
I guess, it is just a small thing. This is my first OSG project and in fact it is my first problem, which i cant solve by reading the books .... :(
Thank you!
Cheers,
Georg
P.S.: I dont know why the forum shows my "4 space tabs" as ??? I replaced my "\t" by four spaces in my editor .... :?
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=63519#63519
More information about the osg-users
mailing list