DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

SmoothMove.h

08.11.2007
| 2191 views |
  • submit to reddit
        // description of your code here
It uses Ogre, but only for the Vector3 class.

#ifndef SMOOTHMOVE_H
#define SMOOTHMOVE_H
#include <Ogre/Ogre.h>
#include <cmath>

using namespace Ogre;

// Class to interpolate the points in a path defined by 2 points (start and end)
// There are several methods to do the interpolation (linear, spherical, halfsinus, etc)
// You can add methods just by definig another method in this class. The only requirements
// are that they take a float parameter, return a float, and that for x=0 they return 0, and
// for x=1 they return 1 

class SmoothMove
{
	private:
		Vector3 mStart;
		Vector3 mEnd;
		float mTotalTime;
		
		float mCurrentTime;
		Vector3 mCurrent;
		float (*mCurrentMethod)(float);
		float CalculatePos(float start, float end,float currentTime, float totalTime);
		static bool isEqual(float x, float y);

	public:
		SmoothMove(const Vector3 &start, const Vector3 &end,const float time,float (*smoothMethod)(float));
		Vector3 IterateFrame(const float time);
		Vector3 CalculateCurrentPos();
		void SetNewTarget(const Vector3 &end,const float time);

		static float LinearMethod(float x) { return x; }
		static float AscQuadraticMethod(float x){ return x*x; }	
		static float DescQuadraticMethod(float x){ return 1-(x-1)*(x-1); }
		static float HalfSinusMethod(float x){ return sin(x*M_PI/2); }
		static float SphericalMethod(float x){ return sqrt(1-(x-1)*(x-1)); }
		static float SmoothSinus(float x){ return sin(M_PI*(x-0.5))*3/2; }
};

#endif