[osg-users] Get Animation current time

Diego Mancilla dmancillac at gmail.com
Mon Jan 7 11:30:25 PST 2019


Hello Robert,

Thank you very much for you complete response.

 I'm trying to do what you suggest and have my on AnimationPath/AnimationPathCallback classes that handle my data. But I stumble into a problem.

 When I subclass AnimationPath and AnimationPathCallback I have no problems. But when I wrote my own classes, from scratch my application crashes.

 In order to achieve what I want, I pass a pointer to a custom data container class to my AnimationPath class (I called it MobilesAnimationPath) from my main application. I tried to keep most part of the architecture of AnimationPath, but I eliminate the TimeControlPointMap container. So, now my class generates the control points from the data pointed from custom data pointer class. 


So, for instance, my custom Path class looks like this:


Code:
#include "MobileObject.h"
#include <osg/AnimationPath>
#include <osg/Object>

using namespace osg;

class MobilesAnimationPath: public virtual osg::Object
{
public:
	MobilesAnimationPath();
	MobilesAnimationPath(const MobilesAnimationPath &, const osg::CopyOp & copyop = osg::CopyOp::SHALLOW_COPY);
	MobilesAnimationPath(MobileObject & mobile);

	META_Object(osg, MobilesAnimationPath); //Visual Studio complains about this line, but compiles... : "function definition for META_Object not found"

	enum LoopMode
	{
		SWING,
		LOOP,
		NO_LOOPING
	};

	void setLoopMode(LoopMode loopMode);
	LoopMode getLoopMode() const;

	virtual bool getInterpolatedControlPoint(double time, osg::AnimationPath::ControlPoint & cp) const;

	double getFirstTime() const;
	double getLastTime() const;
	double getPeriod() const;

protected:
	~MobilesAnimationPath(){};

private:
	MobileObject * _mobile;
	LoopMode _loopMode;

};



And the implementation.


Code:
#include "MobilesAnimationPath.h"

MobilesAnimationPath::MobilesAnimationPath()
	:_loopMode(NO_LOOPING)
	,_mobile(nullptr)
{
}


MobilesAnimationPath::MobilesAnimationPath(const MobilesAnimationPath& ap, const osg::CopyOp& copyop)
	:osg::Object(ap, copyop)
	,_loopMode(ap._loopMode)
	,_mobile(ap._mobile) 
{
}

MobilesAnimationPath::MobilesAnimationPath(MobileObject & mobile)
	:_loopMode(NO_LOOPING)
{
	_mobile = &mobile;
}

void MobilesAnimationPath::setLoopMode(LoopMode loopMode)
{
	_loopMode = loopMode;
}

MobilesAnimationPath::LoopMode MobilesAnimationPath::getLoopMode() const
{
	return _loopMode;
}

double MobilesAnimationPath::getFirstTime() const
{
	if (_mobile != nullptr)
	{
		if (_mobile->dataLoaded())
		{
			return _mobile->getFirstTime();
		}
	}
	return 0.0;
}

double MobilesAnimationPath::getLastTime() const
{
	if (_mobile != nullptr)
	{
		if (_mobile->dataLoaded())
		{
			return _mobile->getLastTime();
		}
	}
	return 0.0;
}

double MobilesAnimationPath::getPeriod() const
{
	if (_mobile != nullptr)
	{
		if (_mobile->dataLoaded())
		{
			return _mobile->getFirstTime() - _mobile->getLastTime();
		}
	}
	return 0.0;
}

bool MobilesAnimationPath::getInterpolatedControlPoint(double time, osg::AnimationPath::ControlPoint & cp) const
{
	if (_mobile == nullptr) return false;
	if (!_mobile->dataLoaded()) return false;
        
        // at this point _mobile points something thats not nullptr but returns garbage...

	switch (_loopMode)
	{
	case(SWING):
	{
		double modulated_time = (time - getFirstTime()) / (getPeriod()*2.0);
		double fraction_part = modulated_time - floor(modulated_time);
		if (fraction_part > 0.5) fraction_part = 1.0 - fraction_part;

		time = getFirstTime() + (fraction_part*2.0) * getPeriod();
		break;
	}
	case(LOOP):
	{
		double modulated_time = (time - getFirstTime()) / getPeriod();
		double fraction_part = modulated_time - floor(modulated_time);
		time = getFirstTime() + fraction_part * getPeriod();
		break;
	}
	case(NO_LOOPING):
		// no need to modulate the time.
		break;
	}

	//... here I fill the control point through the data on _mobile.

	return true;

	
}



but at the first call to "getInterpolatedControlPoint" the application crashes. Moreover, the _mobile pointer is "corrupted", i.e., when I try to access its data it returns garbage (but it not nullptr). Also the "time" value passed is random (sometimes huge, sometimes negative, etc...)

As I pointed on the code snippet, MSVS complains about my META_Object call, but the code compiles.

Am I missing something?

Cheers,

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







More information about the osg-users mailing list