使立方体放入曲线动态切换其目标位置



使用,lua,我在立方体(放松/"加速",然后放松/"减速"之后)从一个数字插入到另一个数字;SimpleSpline的数字在0-1之间(动画进行的时间,最简单的put)和SimplesplineBettee的执行时间相同,但将其保持在两个给定的最小/最大值之间。

function SimpleSpline( v )
    local vSquared = v*v
    return (3 * vSquared - 2 * vSquared * v)
end
function SimpleSplineBetween( mins, maxs, v )
    local fraction = SimpleSpline( v )
    return (maxs * fraction + mins * (1 - fraction))
end

一切都很好。但是,我遇到了一些问题。我希望这是更多 dynamic 。例如,假设我的"分钟"是0.5,而我的"最大"是1,那么我的时间有一个变量,可以通过V;我们会说是0.5,因此我们当前的插值值为0.75。现在,我们还假设突然之间," Maxs"被猛拉到0.25,所以现在,我们有一个新的目标可以达到。

我当前的处理情况如上所述的方法是将我们的"时间"变量重置为当前价值;在上述情况下,0.75等。但是,这会在动画中产生A 非常明显的"停止"或" Freeze",因为它已完全重置。

我的问题是,如果不停止,我该如何使这种动态?我希望它能顺利过渡到一个目标号码。

local Start_New_Spline, Calculate_Point_on_Spline, Recalculate_Old_Spline
do
   local current_spline_params
   local function Start_New_Spline(froms, tos)
      current_spline_params = {d=0, froms=froms, h=tos-froms, last_v=0}
   end
   local function Calculate_Point_on_Spline(v)  --  v = 0...1
      v = v < 0 and 0 or v > 1 and 1 or v
      local d     = current_spline_params.d
      local h     = current_spline_params.h
      local froms = current_spline_params.froms
      current_spline_params.last_v = v
      return (((d-2*h)*v+3*h-2*d)*v+d)*v+froms
   end
   local function Recalculate_Old_Spline(new_tos)
      local d     = current_spline_params.d
      local v     = current_spline_params.last_v
      local h     = current_spline_params.h
      local froms = current_spline_params.froms
      froms = (((d-2*h)*v+3*h-2*d)*v+d)*v+froms
      d = ((3*d-6*h)*v+6*h-4*d)*v+d
      current_spline_params = {d=d, froms=froms, h=new_tos-froms, last_v=0}
   end
end

根据您的价值观的用法示例:

Start_New_Spline(0.5, 1)      -- "mins" is 0.5, "maxs" is 1
local inside_spline = true
while inside_spline do
   local goal_has_changed = false
   for time = 0, 1, 0.015625 do  -- time = 0...1
      -- It's time to draw next frame
      goal_has_changed = set to true when goal is changed
      if goal_has_changed then
         -- time == 0.5 ->  s == 0.75, suddenly "maxs" is jerked up to 0.25
         Recalculate_Old_Spline(0.25)  -- 0.25 is the new goal
         -- after recalculation, "time" must be started again from zero
         break  -- exiting this loop
      end
      local s = Calculate_Point_on_Spline(time)  -- s = mins...maxs
      Draw_something_at_position(s)
      wait()
   end
   if not goal_has_changed then
      inside_spline = false
   end
end

相关内容

  • 没有找到相关文章

最新更新