Game Development Community

Is it possible to interpolate between two floats?

by Stefan Lundmark · in Torque Game Engine · 05/19/2006 (2:54 pm) · 8 replies

I want to interpolate between two floating point values, but have yet to grasp the syntax.
This is what I tried:

F32 start = 0.5;
   F32 end = 10.5;
   F32 result; 
   result.interpolate(start, end, 0.2);

I know I am missing alot in that codesnippit, but are there any examples on how to do this with a pair of floating point values? No documentation or posts about it, and the engine mostly deals with Point3F's and Matrix's.

Anyone can point me in the right direction?

#1
05/19/2006 (3:18 pm)
Uh, F32 is a primitive class like bool or int. it has no methods.

result = interpolate(start, end, amount);

amount = 0 gets you start,
amount = 1 gets you end.

you can also use amounts outside the range [0, 1],
but that's only if you kinda know why you'd want to do that.
#2
05/19/2006 (3:30 pm)
Thanks Orion!

That doesn't work though, tried it before. :p
Quote:
error C3861: 'interpolate': identifier not found, even with argument-dependent lookup

I am looking to interpolate each tick, like particleEmitters do with color, for example.

Anyone else?
#3
05/19/2006 (3:42 pm)
.. this is what source code is for, man!

try this-

result = TSTransform::interpolate(stuff);

or write it yourself,
it's really not that involved:

result = start + (end - start) * amount;
#4
05/19/2006 (3:43 pm)
Linear interpolation is a staple of just about any sort of programming involving anything with dynamic or simulated floating-point values, (eg, anything in a FPS or graphics, or.. well, anything).
it'd be good to get some intuition on how it works.
#5
05/20/2006 (2:02 am)
Quote:
.. this is what source code is for, man!

Very helpful. I spent a good hour researching this, but there is no information to be found.

I see this as a good thing when unanswered questions are brought up. There is no documentation about it, or threads - so I'll make one so the question might get answered.

Quote:
or write it yourself,
it's really not that involved:

result = start + (end - start) * amount;

I fail to see how your example above would interpolate over each tick, I thought that was what interpolate(); did which is why I want to use it instead of writing my own stuff (which frankly, I don't know how to do myself).

Thanks for your time though.
#6
05/20/2006 (10:10 am)
Hey stephen,
sorry i haven't been so helpful.

here's some example code written in torque-script
which might help you interpolate between two values each tick.

.. but it might not, too.

frankly i don't know what interpolate() method you're talking about.
maybe it's a TGE 1.4 thing; i'm in 1.3. But there's about fifty interpolate
functions in the 1.3 source, so if you can point to a specific one that would help too.

orion

// example parameters:
$timeBegin   = 10;
$timeFinish  = 50;
$valueBegin  = 255;
$valueFinish = 37;

// note - it doesn't matter what units the time params are in,
//        as long as they're used consistently.




function getInterpolatedValue(%timeCurrent)
{
   // get the current time as a value between 0 and 1,
   // where 0 = timeBegin and 1 = timeFinish.
   %timeCurrentNormalized = ($timeFinish - %timeCurrent) / ($timeFinish - $timeBegin);
   // note the above will cause an error if $timeFinish = $timeBegin.

   // optional: make sure it's in the range [0, 1]:
   if (%timeCurrentNormalized < 0)
      %timeCurrentNormalized = 0;
   else if (%timeCurrentNormalized > 1)
      %timeCurrentNormalized = 1;

   // now interpolate value.
   %result = $valueBegin + ($valueFinish - $valueBegin) * %timeCurrentNormalized;

   return %result;
}


// helper functions:


// set up the thing to interpolate from one value to another,
// over some chunk of time starting now.
function beginInterpolation(%valueBegin, %valueFinish, %duration)
{
   $timeBegin   = <get current time>;
   $timeFinish  = $timeBegin + %duration;
   $valueBegin  = %valueBegin;
   $valueFinish = %valueFinish;
}


// set up the thing to interpolate from the current value to a new value,
// over some chunk of time starting now.
function beginInterpolationTo(%value, %duration)
{
   $timeBegin   = <get current time>;
   $timeFinish  = $timeBegin + %duration;
   $valueFinish = %value;
}
#7
05/21/2006 (12:09 pm)
Hi Orion,

Sorry. Perhaps I misunderstood your first post above.
Anyway, I managed to solve this on my own by looking at startFade from the playerClass (or was it ShapeBase?) and this resource.

Quote from the resource:

Quote:
The idea is to change the mOrientation vector just a little bit on each tick, using the interpolate function.

Which lead me to believe there was a global interpolate function that changed a value to another on each tick, pretty much automatically. :) In any case, I'll take a look at startFade instead and go from there.

Thanks again for your help.
#8
05/21/2006 (12:21 pm)
Interpolate is a class specific method (not global function) that originates as part of Point3F, and is reimplemented for several subclasses (such as Color3F).