在penner宽松中防止超出界限值

时间:2012-11-10 21:35:19

标签: math graphics2d easing-functions

我正在开发一个项目,该项目使用以下缓动功能在屏幕上移动对象:

function easeinoutquart(t,b,c,d) as float
    't=time, b=startvalue, c=change in value, d=duration
    d=d/2
    t=t/d
    if (t < 1) then 
        return c/2*t*t*t*t + b
    end if
    t=t-2
    return -c/2 * (t*t*t*t - 2) + b
end function

我一直遇到价值迅速超出界限的情况,导致物体以惊人的速度移动到屏幕外。第一种情况似乎是由函数的结果截断为整数值引起的,这是我修正的。触发完全相同行为的下一件事似乎是在对象停止并且输入新的缓动距离后未重置时间值。一旦我在更改目标值(change = destination-start)后添加了重置,这似乎完全解决了问题。

现在我已经为下载和交换图像的循环添加了更多代码。似乎循环中增加的执行时间有时会导致值超出界限,从而产生从屏幕上冲出的相同模糊图像。

简要描述一下代码:

loop
   check for user input (up down left right select) 
      if so set new dest position for all images, reset timer for all images
      if a new image is selected on screen, load related content, reset related content timer

      is there an image in the queue to download? if so get initiate async download
      are any images downloaded? If so swap out temp image with final image

      call easing function for primary images (vertical movement on y axis)
      call easing function for related content (horizontal movement on x axis)
      draw all graphics and swap display buffer
end loop

在添加图像下载/交换(实际交换只是更改指针)代码之前,所有内容都适用于预加载的图像和临时图像。现在我首先加载临时图像并进行交换,我认为循环是在超过1/30秒的时间执行,这可能会导致计时器值变得奇怪。

基本上,我想知道是否存在某种“管理者”钳位,我可以设置值,这将使缓和功能不会产生快速失控的疯狂值。例如,在一次迭代中移动的总距离不应超过250个像素,实际上应始终只是目标值的几个像素。

1 个答案:

答案 0 :(得分:0)

虽然我确信我没有很好地提出这个问题,但我终于找到了答案。我遇到的问题是由时间值大于持续时间的值引起的。

在上述功能中,您不会输入目标值,只输入当前值以及起点和终点之间的差值。我想出的解决方案是:如果调用缓动函数之间的执行时间超出了缓动完成所允许的最大允许持续时间值,那么只需将起始值设置为等于目标值。

time是自缓动函数启动以来的毫秒数:

change=destination-currentposition
currentposition=inoutquart(time,currentposition,change,duration)
if time > duration then startarray[i]=destarray[i]

因此,如果时间超过持续时间,我们会将输出值钳制到目标位置。