Discussion:
[osg-users] Speeding u/down animation smoothly
Diego Mancilla
2018-12-05 23:01:18 UTC
Permalink
Hello,

I'm trying to sppeding up/down an animation using AnimationPathCallback. The idea is to pause/speed/up/down the animation using key strokes, for this I have a GuiEventHandler derived class and a NodeVisitor derived class. So far I've playing round with the base clases AnimationPath and AnimationPathCallback methods get/setStartTime, get/setTimeOffset, get/setLastTime and get/settimeMultiplier with no success.

I found http://forum.openscenegraph.org/viewtopic.php?t=10014 but my implementation still jumps around when speeding up/down:


Code:
void AnimationVisitor::apply(osg::Transform& transform)
{
osg::AnimationPathCallback* apc = dynamic_cast<osg::AnimationPathCallback*>(transform.getUpdateCallback());

if (apc)
{
osg::AnimationPath * path = apc->getAnimationPath();
int key = getKey();
double lambda;
switch (key)
{
case osgGA::GUIEventAdapter::KEY_Space:
{
bool is_paused = apc->getPause();
apc->setPause(!is_paused);
std::cout << "Animation time: " << apc->getAnimationTime() << std::endl;
std::cout << "\tPause: " << !is_paused << std::endl;
std::cout << "---------------------------" << std::endl;
break;
}
case osgGA::GUIEventAdapter::KEY_Right:
{
double last = path->getLastTime();
double first = path->getFirstTime();
lambda = apc->getTimeMultiplier();
double simTime = last - first;
double currOffset = apc->getTimeOffset();
double offset = simTime - (simTime - currOffset) / 1.1;
apc->setTimeMultiplier(1.1*lambda);
apc->setTimeOffset(offset);
std::cout << "Animation time: " << apc->getAnimationTime() << std::endl;
std::cout << "\tSpeed up: : " << lambda * 1.1 << std::endl;
std::cout << "---------------------------" << std::endl;
break;
}
case osgGA::GUIEventAdapter::KEY_Left:
{
double last = path->getLastTime();
double first = path->getFirstTime();
lambda = apc->getTimeMultiplier();
double simTime = last - first;
double currOffset = apc->getTimeOffset();
double offset = simTime - (simTime - currOffset) * 0.9;
apc->setTimeMultiplier(0.9*lambda);
apc->setTimeOffset(offset);
std::cout << "Animation time: " << apc->getAnimationTime() << std::endl;
std::cout << "\tSpeed up: : " << lambda * 0.9 << std::endl;
std::cout << "---------------------------" << std::endl;
break;
}
default:
break;
}

}
traverse(transform);
}




Can anyone give some pointers about how to achieve a smooth speeding transition? I look up in the source code of both classes involved (AnimationPath and AnimationPathCallback) but I havent been able to figure out a solution. What can I do? or What am I missing?

Thank you!

Cheers

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75287#75287
Julien Valentin
2018-12-05 23:16:20 UTC
Permalink
Code:
float getAnimationTime{return ((_latestTime-_firstTime)-_timeOffset)*_timeMultiplier;}



so you'll have to do math!!
;)
Post by Diego Mancilla
Hello,
I'm trying to sppeding up/down an animation using AnimationPathCallback. The idea is to pause/speed/up/down the animation using key strokes, for this I have a GuiEventHandler derived class and a NodeVisitor derived class. So far I've playing round with the base clases AnimationPath and AnimationPathCallback methods get/setStartTime, get/setTimeOffset, get/setLastTime and get/settimeMultiplier with no success.
void AnimationVisitor::apply(osg::Transform& transform)
{
osg::AnimationPathCallback* apc = dynamic_cast<osg::AnimationPathCallback*>(transform.getUpdateCallback());
if (apc)
{
osg::AnimationPath * path = apc->getAnimationPath();
int key = getKey();
double lambda;
switch (key)
{
{
bool is_paused = apc->getPause();
apc->setPause(!is_paused);
std::cout << "Animation time: " << apc->getAnimationTime() << std::endl;
std::cout << "\tPause: " << !is_paused << std::endl;
std::cout << "---------------------------" << std::endl;
break;
}
{
double last = path->getLastTime();
double first = path->getFirstTime();
lambda = apc->getTimeMultiplier();
double simTime = last - first;
double currOffset = apc->getTimeOffset();
double offset = simTime - (simTime - currOffset) / 1.1;
apc->setTimeMultiplier(1.1*lambda);
apc->setTimeOffset(offset);
std::cout << "Animation time: " << apc->getAnimationTime() << std::endl;
std::cout << "\tSpeed up: : " << lambda * 1.1 << std::endl;
std::cout << "---------------------------" << std::endl;
break;
}
{
double last = path->getLastTime();
double first = path->getFirstTime();
lambda = apc->getTimeMultiplier();
double simTime = last - first;
double currOffset = apc->getTimeOffset();
double offset = simTime - (simTime - currOffset) * 0.9;
apc->setTimeMultiplier(0.9*lambda);
apc->setTimeOffset(offset);
std::cout << "Animation time: " << apc->getAnimationTime() << std::endl;
std::cout << "\tSpeed up: : " << lambda * 0.9 << std::endl;
std::cout << "---------------------------" << std::endl;
break;
}
break;
}
}
traverse(transform);
}
Can anyone give some pointers about how to achieve a smooth speeding transition? I look up in the source code of both classes involved (AnimationPath and AnimationPathCallback) but I havent been able to figure out a solution. What can I do? or What am I missing?
Thank you!
Cheers
------------------------
Twirling twirling twirling toward freedom

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75288#75288
Julien Valentin
2018-12-05 23:26:05 UTC
Permalink
set _firstframe to 0 to think in animation time base (not in simulation time)
let timemultiplier to 1
and set latesttime to what you want(in animation time base)

then setup
Post by Julien Valentin
float getAnimationTime{return ((_latestTime-_firstTime)-_timeOffset)*_timeMultiplier;}
so you'll have to do math!!
;)
Post by Diego Mancilla
Hello,
I'm trying to sppeding up/down an animation using AnimationPathCallback. The idea is to pause/speed/up/down the animation using key strokes, for this I have a GuiEventHandler derived class and a NodeVisitor derived class. So far I've playing round with the base clases AnimationPath and AnimationPathCallback methods get/setStartTime, get/setTimeOffset, get/setLastTime and get/settimeMultiplier with no success.
void AnimationVisitor::apply(osg::Transform& transform)
{
osg::AnimationPathCallback* apc = dynamic_cast<osg::AnimationPathCallback*>(transform.getUpdateCallback());
if (apc)
{
osg::AnimationPath * path = apc->getAnimationPath();
int key = getKey();
double lambda;
switch (key)
{
{
bool is_paused = apc->getPause();
apc->setPause(!is_paused);
std::cout << "Animation time: " << apc->getAnimationTime() << std::endl;
std::cout << "\tPause: " << !is_paused << std::endl;
std::cout << "---------------------------" << std::endl;
break;
}
{
double last = path->getLastTime();
double first = path->getFirstTime();
lambda = apc->getTimeMultiplier();
double simTime = last - first;
double currOffset = apc->getTimeOffset();
double offset = simTime - (simTime - currOffset) / 1.1;
apc->setTimeMultiplier(1.1*lambda);
apc->setTimeOffset(offset);
std::cout << "Animation time: " << apc->getAnimationTime() << std::endl;
std::cout << "\tSpeed up: : " << lambda * 1.1 << std::endl;
std::cout << "---------------------------" << std::endl;
break;
}
{
double last = path->getLastTime();
double first = path->getFirstTime();
lambda = apc->getTimeMultiplier();
double simTime = last - first;
double currOffset = apc->getTimeOffset();
double offset = simTime - (simTime - currOffset) * 0.9;
apc->setTimeMultiplier(0.9*lambda);
apc->setTimeOffset(offset);
std::cout << "Animation time: " << apc->getAnimationTime() << std::endl;
std::cout << "\tSpeed up: : " << lambda * 0.9 << std::endl;
std::cout << "---------------------------" << std::endl;
break;
}
break;
}
}
traverse(transform);
}
Can anyone give some pointers about how to achieve a smooth speeding transition? I look up in the source code of both classes involved (AnimationPath and AnimationPathCallback) but I havent been able to figure out a solution. What can I do? or What am I missing?
Thank you!
Cheers
------------------------
Twirling twirling twirling toward freedom

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75289#75289
Diego Mancilla
2018-12-06 13:21:27 UTC
Permalink
Hi,

Thank you for your answer mp3butcher.

Regarding the math issue: actually the arithmethic is correct in my code, the problem, as I found, was that the _latestTime and _fisrtTime in the AnimationPathCallback won't match the ones provided in AnimationPath, so I subclassed AnimationPathCallback providing access to mentioned members and extract them from the callback (instead of the path) and it finally worked.

Regarding your second post, to be honest I didnt understand.

I'm just a newbie on OSG and the lack of proper documentation is becoming quite a problem (for me at least).



Cheers,

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75290#75290
Julien Valentin
2018-12-06 14:40:12 UTC
Permalink
This post might be inappropriate. Click to display it.
Julien Valentin
2018-12-06 14:42:27 UTC
Permalink
oups I haven't see the latest auto update
So yes you have to subclass to avoid var auto updates
Post by Julien Valentin
Interpreting the sources,( I haven't tested as it's kinda obvious)
the animationtime is (latest - first - offset) * timemul
posing timemult=1 offset =0 and first=0 give use animationtime =latest
so have complete control with only one parameter
NB: first must always reset at each update to prevent its auto update in apc
Post by Diego Mancilla
Hi,
Thank you for your answer mp3butcher.
Regarding the math issue: actually the arithmethic is correct in my code, the problem, as I found, was that the _latestTime and _fisrtTime in the AnimationPathCallback won't match the ones provided in AnimationPath, so I subclassed AnimationPathCallback providing access to mentioned members and extract them from the callback (instead of the path) and it finally worked.
Regarding your second post, to be honest I didnt understand.
I'm just a newbie on OSG and the lack of proper documentation is becoming quite a problem (for me at least).
Cheers,
------------------------
Twirling twirling twirling toward freedom

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75293#75293

Loading...