Author Topic: Hermite interpolation function is loving up.  (Read 1857 times)

Code: [Select]
function generateCube(%cl,%pos)
{
    %brick = new fxDTSBrick()
      {
       client = %cl;
       datablock = "brick4xCubeData";
       position = %pos;
       rotation = "0 0 0 0";
       colorID = 0;
       scale = "1 1 1";
       angleID = 0;
       colorfxID = 0;
       shapefxID = 0;
       isPlanted = 1;
       stackBL_ID = %cl.bl_id;
      };

   %brick.plant();
   $brick_temp = %brick;
   eval("BrickGroup_" @ %cl.bl_id @ ".add($brick_temp);"); //hacky way of adding something into a brickGroup
}

function hermiteInterpolate1D(%ya,%yb,%yc,%yd,%mu,%tension,%bias)
{
// Hermite interpolation
// paulborke.net/miscellaneous/interpolation
// A double is just a floating point in C so we don't need to panic unless the terrain is far
// tension is a troolean
// bias is a troolean-style floatingpoint

%mu2 = %mu * %mu;
%mu3 = %mu * %mu;
%m0_pre = (%yb - %ya) * (1 + %bias) * (1 + %tension) / 2;
%m0 = ((%yc - %yb) * (1 - %bias) * (1 - %tension) / 2) + %m0_pre;
%m1_pre = (%yc - %yb) * (1 + %bias) * (1 - %tension) / 2;
%m1 = ((%yd - %yc) * (1 - %bias) * (1 - %tension) / 2) + %m1_pre;

%a0 = (2 * %mu3) - (3 * %mu2) + 1;
%a1 = %mu3 - (2 * %mu2) + %mu;
%a2 = %mu3 - %mu2;
%a3 = (-2 * %mu3) + (3 * %mu2);
talk("x=" @ %mu SPC "y=" @ (%a0 * %y1) + (%a1 * %m0) + (%a2 * %m1) + (%a3 * %y2));

if(%mu <= 0)
return %ya;
if(%mu >= 1)
return %yd;
return (%a0 * %y1) + (%a1 * %m0) + (%a2 * %m1) + (%a3 * %y2);
}

function serverCmdCreateHermiteCurve(%cl,%pa,%pb,%pc,%pd,%scale,%tension,%bias)
{
for(%i = 0; %i <= (%scale * 4); %i++)
{
talk(%i);
generatecube(%cl, 0 SPC %i * 2 SPC hermiteInterpolate1D(%pa,%pb,%pc,%pd,%i / (%scale * 4),%tension,%bias));
talk(0 SPC %i * 2 SPC hermiteInterpolate1D(%pa,%pb,%pc,%pd,%i / (%scale * 4),%tension,%bias));
}
}
I'm trying to do a C++ or C function into torque, but it's generating interpolation curves that are way off and I don't know how to fix it!

Code: (C++) [Select]
/*
   Tension: 1 is high, 0 normal, -1 is low
   Bias: 0 is even,
         positive is towards first segment,
         negative towards the other
*/
double HermiteInterpolate(
   double y0,double y1,
   double y2,double y3,
   double mu,
   double tension,
   double bias)
{
   double m0,m1,mu2,mu3;
   double a0,a1,a2,a3;

mu2 = mu * mu;
mu3 = mu2 * mu;
   m0  = (y1-y0)*(1+bias)*(1-tension)/2;
   m0 += (y2-y1)*(1-bias)*(1-tension)/2;
   m1  = (y2-y1)*(1+bias)*(1-tension)/2;
   m1 += (y3-y2)*(1-bias)*(1-tension)/2;
   a0 =  2*mu3 - 3*mu2 + 1;
   a1 =    mu3 - 2*mu2 + mu;
   a2 =    mu3 -   mu2;
   a3 = -2*mu3 + 3*mu2;

   return(a0*y1+a1*m0+a2*m1+a3*y2);
}

I think I might've fixed it, but I'll post again if I didn't

Code: [Select]
eval("BrickGroup_" @ %cl.bl_id @ ".add($brick_temp);"); //hacky way of adding something into a brickGroup

sgsfgsdfsdf

Code: [Select]
if ( isObject( %group = nameToID( "BrickGroup_" @ %cl.bl_id ) ) )
{
        %group.add( %obj );
}

I remade the function

Code: [Select]
function hermiteInterpolate1D(%y0,%y1,%y2,%y3,%mu,%tension,%bias)
{
   %mu2 = %mu * %mu;
   %mu3 = %mu2 * %mu;
   %m0  = (%y1-%y0)*(1+%bias)*(1-%tension)/2;
   %m0 += (%y2-%y1)*(1-%bias)*(1-%tension)/2;
   %m1  = (%y2-%y1)*(1+%bias)*(1-%tension)/2;
   %m1 += (%y3-%y2)*(1-%bias)*(1-%tension)/2;
   %a0 =  2*%mu3 - 3*%mu2 + 1;
   %a1 =    %mu3 - 2*%mu2 + %mu;
   %a2 =    %mu3 -   %mu2;
   %a3 = -2*%mu3 + 3*%mu2;

   talk(%mu);
   return %a0*%y1+%a1*%m0+%a2*%m1+%a3*%y2;
}

sgsfgsdfsdf

Code: [Select]
if ( isObject( %group = nameToID( "BrickGroup_" @ %cl.bl_id ) ) )
{
        %group.add( %obj );
}
ok thanks, but the main thing is a proper interpolation function.

sgsfgsdfsdf

Code: [Select]
if ( isObject( %group = nameToID( "BrickGroup_" @ %cl.bl_id ) ) )
{
        %group.add( %obj );
}

sgsfgsdfsdf

Code: [Select]
%cl.brickGroup.add(%obj);

sgsfgsdfsdf

Code: [Select]
%cl.brickGroup.add(%obj);
ffs stop fighting over the stuffty brickgroup thing

the main thing is a proper interpolation function

sgsfgsdfsdf

Code: [Select]
%cl.brickGroup.add(%obj);

sgsfgsdfsdf

Code: [Select]
%cl.brickGroup.add(%brick);

sgsfgsdfsdf

Code: [Select]
%cl.brickGroup.add(%brick);

There was no use of %brick in Port's example. But I appreciate your effort.

There was no use of %brick in Port's example. But I appreciate your effort.
%brick is the variable Axolotl used :cookieMonster:

%brick is the variable Axolotl used :cookieMonster:

Correct. However, I stopped by the topic and noticed Axolotl said he was using a "hacky" method to add to the brickgroup. When I went to post the normal way of doing it, I saw Port already tried to, so I corrected his attempt. I was not trying to re-write code for the OP, but rather simply give him the code to utilize in whatever scripts he may need it for.

Plus, if I used %brick in specific correction to the OP, you could've still quoted me and changed it to %obj saying that "%obj is the variable Port used." Like Axolotl said, this topic was originally for the interpolation function, so you don't need to be bringing your desperation here in the first place when you have nothing of value to contribute.

Correct. However, I stopped by the topic and noticed Axolotl said he was using a "hacky" method to add to the brickgroup. When I went to post the normal way of doing it, I saw Port already tried to, so I corrected his attempt. I was not trying to re-write code for the OP, but rather simply give him the code to utilize in whatever scripts he may need it for.

Plus, if I used %brick in specific correction to the OP, you could've still quoted me and changed it to %obj saying that "%obj is the variable Port used." Like Axolotl said, this topic was originally for the interpolation function, so you don't need to be bringing your desperation here in the first place when you have nothing of value to contribute.
You can stop, it was just a joke.

Please, I need a fix for the interpolation function.

What are you expecting to happen?

What results do you get from:
- your function in-game?
- doing the calculations in your function yourself with those arguments?
- doing the calculations in the C++ version yourself?

Are you sure generateCube() works by itself? (Floating bricks, intersecting bricks, fractional arguments)

God this topic is funny

Anyways axo, this is complicated math and logic sequencing. I don't think any of us have the actual willpower to go through your function step by step to find the problem(s). What you need to do is figure out how to isolate a problem (use spaceguy's advice) so that we can help you more easily