[QUOTE=tmason;1261023]Thanks for your responses; I really appreciate it.
I assume from the above code that you don’t dampen the camera?[/QUOTE]
This is a really basic camera class that I made for basic use. I’ve done animations between positions before, but I’m trying to keep my code as simple and easy to understand as possible right now since I’m only using a fixed camera in my main project (right now). I don’t get paid to build programs (yet) and I have a day-job so I try to keep things quick and simple. (Anyone hiring graphics programmers?)
Come to think of it, a better way to do it might be this, so you could animate parameters and then do update when it is requested:
/** Updates the projection transform after far has been changed. */
- ( void )setFar:( float )far {
_far = far;
_projectionCached = NO; // << and similar for all set methods
}
/**
* Rebuilds the projection matrix from fovY, aspectRatio and near and far.
*/
- ( void )makeProjectionTransform {
_projectionTransform = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(_fovyDegrees),_aspectRatio,_near,_far);
_projectionCached = YES;
}
/** Getter method. */
- ( GLKMatrix4 )projectionTransform {
if( _projectionCached == NO ) {
[ self makeProjectionTransform ];
}
return _projectionTransform;
}
I’ve had problems with my camera class getting way too complicated when I try to handle the animations within the class. I did a camera class in C++ using a generic animation class I wrote using templates and function pointers (handling multiple types, vecs with multiple interpolation types), but it was excessive for what I need (and significantly overcomplicated obviously).
To give you an idea, here’s some of my old code. With my interpolator template (I also did non-linear interpolators) anything that properly overrides * for scalars and + for two similar components can be interpolated (i.e. floats and vecs). Note how my code style has significantly simplified. No more systems hungarian for me!
// Allowed me to create arbitrary interpolators
template<typename tn_interpolated>
tn_interpolated
linearInterpolate(tn_interpolated start_, tn_interpolated end_, float fT_) {
// Clamp to values... return here if needed.
if(fT_ < 0.0f) return start_;
if(fT_ > 1.0f) return end_;
// ----------------------MAGIC HAPPENS HERE --------------------------
// Use * and + for vec3 and floats to allow interpolation
return start_*(1.0f-fT_) + end_*(fT_);
}
// Generic animation code which could handle vec's, floats, and matrices (by me... :( )
/**
* \brief
* Animates transition between two interpolatable types.
*/
template <typename tn_interpolated>
class west_InterpolatorAnimation {
float m_fElapsed; // Elapsed seconds of animation
float m_fDuration; // Total seconds of animation
tn_interpolated m_start; // Starting value
tn_interpolated m_end; // Ending value
tn_interpolated(*m_interpolator)(float, const tn_interpolated&, const tn_interpolated&);
// Interpolator function
public:
/**
* Creates an interpolated animation.
*/
west_InterpolatorAnimation(float fSecondsDuration_,
const tn_interpolated &c_rStart_,
const tn_interpolated &c_rEnd_,
tn_interpolated(*interpolator_)(float, const tn_interpolated&, const tn_interpolated&)
):
m_fElapsed(0),
m_fDuration(fSecondsDuration_),
m_start(c_rStart_),
m_end(c_rEnd_),
m_interpolator(interpolator_)
{
}
/**
* Updates this animation for a duration of elapsed seconds.
*/
void update(float fElapsedSeconds_) {
m_fElapsed += fElapsedSeconds_;
}
/**
* Returns duration of the animation, in seconds.
*/
float duration() const {
return m_fDuration;
}
float elapsed() const {
return m_fElapsed;
}
/**
*
* -------------------- THE MAGIC HAPPENS HERE ------------------
*
*
* Returns the current position of the animation. Starting position
* if animation has not started, and ending position if isFinished().
*/
tn_interpolated currentValue() const {
return m_interpolator(m_fElapsed / m_fDuration, m_start, m_end);
}
tn_interpolated start() const {
return m_start;
}
// Starting value for the animation, when t = 0.0f
tn_interpolated end() const {
return m_end;
}
// Ending value for the animation when t = 1.0f
bool isRunning() const {
return m_fElapsed < m_fDuration;
}
// Animation is between start and end times.
bool isFinished() const {
return m_fElapsed >= m_fDuration;
}
// Animation is at or past its end time.
};
Sample interpolation:
vec3 from( 1, 5, 0 );
vec3 to( 20, 50, 40 );
west_InterpolatorAnimation animatedParameter( 5.0, from, to, linearInterpolation<vec3> );
// .. also works for floats!