Main Menu

Cubic interpolation

Started by jaymzjulian, July 09, 2020, 04:39:55 PM

Previous topic - Next topic

jaymzjulian

Everyone needs to interpolate, right?  I use this for pathing:


function cubic_interpolate(a,b,c,d,t)
  ia = -a / 2.0 + (3.0*b) / 2.0 - (3.0*c) / 2.0 + d / 2.0
  ib = a - (5.0*b) / 2.0 + 2.0*c - d / 2.0
  ic = -a / 2.0 + c / 2.0
  id = b
  return ia*t*t*t + ib*t*t + ic*t + id
endfunction


or a faster version:

function fast_cubic(a,b,c,d,t)
  return (-a / 2.0 + (3.0*b) / 2.0 - (3.0*c) / 2.0 + d / 2.0)*t*t*t + (a - (5.0*b) / 2.0 + 2.0*c - d / 2.0)*t*t + (-a / 2.0 + c / 2.0)*t + b
endfunction


fast point in a 2d dot array via inlining:
(doing 512 of these takes 360 ticks, vs 628 ticks if you call functions instead!)

      a = land[((sy-1) & (dot_horizon-1))+1, ((sx-1) & (dot_horizon-1))+1]
      b = land[((sy) & (dot_horizon-1))+1, ((sx-1) & (dot_horizon-1))+1]
      c = land[((sy+1) & (dot_horizon-1))+1, ((sx-1) & (dot_horizon-1))+1]
      d = land[((sy+2) & (dot_horizon-1))+1, ((sx-1) & (dot_horizon-1))+1]
      ya = (-a / 2.0 + (3.0*b) / 2.0 - (3.0*c) / 2.0 + d / 2.0)*yoffset*yoffset*yoffset + (a - (5.0*b) / 2.0 + 2.0*c - d / 2.0)*yoffset*yoffset + (-a / 2.0 + c / 2.0)*yoffset + b

      a = land[((sy-1) & (dot_horizon-1))+1, ((sx) & (dot_horizon-1))+1]
      b = land[((sy) & (dot_horizon-1))+1, ((sx) & (dot_horizon-1))+1]
      c = land[((sy+1) & (dot_horizon-1))+1, ((sx) & (dot_horizon-1))+1]
      d = land[((sy+2) & (dot_horizon-1))+1, ((sx) & (dot_horizon-1))+1]
      yb = (-a / 2.0 + (3.0*b) / 2.0 - (3.0*c) / 2.0 + d / 2.0)*yoffset*yoffset*yoffset + (a - (5.0*b) / 2.0 + 2.0*c - d / 2.0)*yoffset*yoffset + (-a / 2.0 + c / 2.0)*yoffset + b

      a = land[((sy-1) & (dot_horizon-1))+1, ((sx+1) & (dot_horizon-1))+1]
      b = land[((sy) & (dot_horizon-1))+1, ((sx+1) & (dot_horizon-1))+1]
      c = land[((sy+1) & (dot_horizon-1))+1, ((sx+1) & (dot_horizon-1))+1]
      d = land[((sy+2) & (dot_horizon-1))+1, ((sx+1) & (dot_horizon-1))+1]
      yc = (-a / 2.0 + (3.0*b) / 2.0 - (3.0*c) / 2.0 + d / 2.0)*yoffset*yoffset*yoffset + (a - (5.0*b) / 2.0 + 2.0*c - d / 2.0)*yoffset*yoffset + (-a / 2.0 + c / 2.0)*yoffset + b

      a = land[((sy-1) & (dot_horizon-1))+1, ((sx+2) & (dot_horizon-1))+1]
      b = land[((sy) & (dot_horizon-1))+1, ((sx+2) & (dot_horizon-1))+1]
      c = land[((sy+1) & (dot_horizon-1))+1, ((sx+2) & (dot_horizon-1))+1]
      d = land[((sy+2) & (dot_horizon-1))+1, ((sx+2) & (dot_horizon-1))+1]
      yd = (-a / 2.0 + (3.0*b) / 2.0 - (3.0*c) / 2.0 + d / 2.0)*yoffset*yoffset*yoffset + (a - (5.0*b) / 2.0 + 2.0*c - d / 2.0)*yoffset*yoffset + (-a / 2.0 + c / 2.0)*yoffset + b

      return (-ya / 2.0 + (3.0*yb) / 2.0 - (3.0*yc) / 2.0 + yd / 2.0)*xoffset*xoffset*xoffset + (ya - (5.0*yb) / 2.0 + 2.0*yc - yd / 2.0)*xoffset*xoffset + (-ya / 2.0 + yc / 2.0)*xoffset + yb

Vectrex32

Quote from: jaymzjulian on July 09, 2020, 04:39:55 PM
Everyone needs to interpolate, right?

If there are three guys, and the guy on the left needs to interpolate, and the gut on the right needs to interpolate, can we interpolate that the guy in the middle needs to interpolate?