# Time Syncronization Issues

## Recommended Posts

Plasmarobo    100
So, I'm writing a generic class that takes a structure with information on a source value and how it should change over time and modifies that value via a pointer. I seem to have implemented it wrong, because whenever I pass it a structure and run it, the value comes out vastly unexpected. My game engine relies heavily on vectors (where lists may serve it better, but it's a work in progress) and I think that it might be part of the problem, but I'm not changing the vectors size during this execution. I'll throw up the code... Honestly, this was supposed to solve problems, but it just broke my engine.
class gEvent{
protected:
union{
double *d;
long *l;
} m_ptr;
union{
double d;
long l;
} m_result;
union{
double d;
long l;
} m_init;
char m_format;
//long l
//double d
float m_duration;
float m_start;
long m_handle;
public:
gEvent();
gEvent(const gEvent &rhs);
gEvent(double *d, float time, double val);
gEvent(long *l, float time, long val);
void operator=(const gEvent& rhs);
void SetDuration(float time);
void SetValue(double d);
void SetValue(long l);
void Start(float now);
bool Step(float now); //Steps the value of the pointer
void SetPointer(long *l);
void SetPointer(double *d);
};


The functions are named for their functions, aptly enough. Here's the code that runs through a deque of these structures.
bool CORE::Game::gEvent::Step(float now){
if(m_ptr.l == NULL || m_ptr.d == NULL)
return false; //Done, delete the instance
switch(m_format){
case 'l':
if((m_duration<(now-m_start))){
*m_ptr.l = m_result.l;
return false;
}
(*m_ptr.l) = (m_init.l)*(m_duration/(now-m_start));
return true;
break;
case 'd':
if((m_duration<(now-m_start))){
*m_ptr.d = m_result.d;
return false;
}
(*m_ptr.d) = (m_init.d)*(m_duration/(now-m_start));
return true;
break;
default:
return false;
break;
}
}


Is there a better way to generically time data changes? I've done some googling, but can't seem to hit the right keywords or something.

##### Share on other sites
Ashaman73    13715
Well, do you want to interpolate a value between 'init' and 'result' in time 'duration' ?

If this assumption is correct, your interpolation code should look like
(*m_ptr.d) = (m_init.d) + (m_result.d - m_init.d)*(m_duration/(now-m_start));

--
Ashaman

##### Share on other sites
Plasmarobo    100
I've actually fixed that, my code is a little different, but functionally
equivalent. The code seems to change the values, but resets to zero every other
frame. I am unsure why, but it looks like the start value is reseting to the now
value every other frame or something similar.

##### Share on other sites
Matt_D    247
so what your writing is an interpolator. your basically interpolating between two values over a period of time.

lots of implementations available, anyway, im going to guess here, but what are you using as your units of time? each frame will occur after a very very small amount of time has passed, so if your using seconds as a measure of time, then its quite possible that your interpolation will be the starting value (as the amount of interpolation is close enough to 0 to be 0).

make sure you get your units right, and it should be pretty straightforward to put together an interpolator.

also, do you really need to use unions? looks like your overcomplicating things dramatically. the code should be very very simple.

##### Share on other sites
Plasmarobo    100
I'm using unions because I would rather not type-cast a void pointer.
I guess I could use templates and visualization to do the same.
I guess I could do that. I think my units are in either miliseconds or
clock-cycles. However, the code should compensate for instantaneous
transformation (a duration of zero or less) and simply assign the pointer
to the result value before being deleted. The fraction is constantly
being set to zero. On the occasion when it actually displays anything, I
can see the sprite flick rapidly back and forth, between the zero value
and some small fraction of it's entire journey.