伴随着额外的要求路径动画 [英] Animate along path with additional requirements

查看:384
本文介绍了伴随着额外的要求路径动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关游戏我开发我想动画沿着路径的对象。有三侧到这一点;因此我不确定什么方法是最好的解决方案。

For a game I am developing I want to animate an object along a path. There are three sides to this; therefore I am unsure what approach is the best solution.

路径动画

目前的路径是由一系列的点,这意味着我使用的路径只有直线。不过我打算以后更改为一个贝塞尔曲线。目前的点是在5和8个像素之间的距离。我还打算降低在曲线上的控制点的数目。
当他说的动画我想 PathTransition ,但我不设置在此,由于基于各约束可能会有更好的解决方案。

Currently the path is made up as a series of points which means I am using Path with only straight lines. However I intend to change this later on to a Bézier curve. Currently the points are at a distance between 5 and 8 pixels. I also intend to reduce the number of control points on the curve. When saying animation I am thinking PathTransition, but I am not set on this, as based on the various constraints there might be better solutions.

这是沿路径动画的对象是一个的ImageView ,也可以像这样简单的一个图形 (如圆形)。

The object that is animated along the path is an ImageView but could also be something as simple as a Shape (like Circle).

持续时间

的游戏是由一个计时器来驱动。该定时器发送事件。多少游戏内时间已经过去了一个剔依赖于周期的时间间隔:一个游戏中打勾即可重新present 5分钟或一小时内传递。一个勾发生的每100毫秒。

The game is driven by a timer. The timer sends tick events. How much ingame time has passed for an tick depends on the tick interval: An ingame tick can represent the passing of 5 minutes or one hour. A tick happens every 100 ms.

根据刻度间隔有可能确定动画的最初时间:

Based on the tick interval it is possible to determine the initial duration of the animation:

int intervalInMinutes = 5;
int ingameAnimationDurationInMinutes = 60 * 24;
int nbTicks = ingameAnimationDurationInMinutes / 5; // = 288
int durationInMs = 100 * nbTicks; // 28800 ms = 28.8s

但是,可以在任何时间刻度间隔改变(例如5分钟 - > 30分钟)。这意味着剩余动画应该由因子6被加快。
在这一点上,我看到下面的选项:

However it is possible that the tick interval changes at any time (e.g. 5 minutes -> 30 minutes). This means that the remaining animation should be sped up by factor 6. At this point I see the following options:


  • 更新动画的持续时间。对于 PathTransition 这似乎很容易,但其他方法(如关键帧动画)可能会在这里有一个问题。

  • 停止正在运行的动画和从当前位置重新初始化动画。我认为这可能是困难的,因为目前的位置可以不与在曲线的控制点中的一个相匹配,这将意味着在曲线(或者至少第一片段)需要被重新计算。

  • 创建动画迭代:沿着曲线只为一个刻度动画。这种方法同样的问题也适用于上述的一种:当动画完成动画对象的位置必须是在一个控制点。如果周期的时间间隔改变控制之间的段点改变。

  • Update the duration of the animation. For a PathTransition this seems easy, but other approaches (like KeyFrame animation) might have an issue here.
  • Stop the running animation and reinitialize the animation from the current position. I think this could be difficult as the current location may not match with one of the control points in the curve, which would mean that the curve (or at least the first segment) needs to be recalculated.
  • Create the animation iteratively: animate along the curve only for one tick. For this approach the same issues apply as for the one above: When the animation is finished the position of the animated object must be at a control point. If the tick interval changes the segments between the control points change as well.

外部更新

需要在$ C $的c它的模型,因此其它部分进行更新,可向改变后的位置作出反应的动画对象的位置。我在这里可以设想各种情况:

The position of the animated object needs to be updated in it's model so other parts of the code can react to the changed position. I can envision various scenarios here:

  • On each tick query the position of the object that is animated and update the position in it's model
  • Animation updates the position of the object. For this approach however I would probably need an KeyFrame animation or even a KeyFrame animation together with an Interpolator.
  • Do not use one of the built in animations, but update the position on each tick (as there are only 10 ticks/s this will not result in a smooth animation)

什么是满足这些要求,而不使该解决方案太复杂的一个好方法?这是非常可能的,还有一些我不知道的,我已经忽略的方面,甚至其他的选择。

What is a good approach to address these requirements without making the solution too complicated? It is well possible that there are aspects that I have overlooked or even other options that I am unaware of.

推荐答案

如果您切换现在就贝塞尔曲线,我觉得你的问题会立即解决。贝塞尔曲线有一个参数空间,通常是[0,1],它的有些的转换速度(曲线的参数通常被命名吨的时间在数学的一个原因)。因此,如果你有你的曲线,无论是作为一个实际的多项式或作为您去应用Casteljau评估一些控制点,所有你需要知道的关于曲线上的进展是参数空​​间中的进展。

If you switch right now to Bezier curves, I think your problem is instantly solved. Bezier curves have a parameter space, usually [0,1], which somewhat translates to speed (the argument of a curve is usually named t for time in mathematics for a reason). Therefore, if you have your curve, either as an actual polynom or as some control points that you evaluate by de Casteljau, all you need to know about your progression on the curve is the progression within the parameter space.

因此​​,一个刻度是参数空间内一定的距离,像1/1000若1000蜱是它需要多少蜱完成曲线。你只需添加1/1000到参数的当前值和评估你的一个曲线。

Therefore, a tick would be a certain distance within the parameter space, like 1/1000 if 1000 ticks is how many ticks it takes to finish the curve. You'd simply add 1/1000 to the current value of the parameter and evaluate your curve for that one.

如果这不是你不够平滑,那么你可以做到以下几点:看作下一个刻度位置,你想和线性的方式您当前的位置做一个动画来表示。如果你的蜱细分曲线足够细,没有人会能够看到直线段(这应该足够了一场比赛,不会为一台切割机做到这一点)。

If that's not smooth enough for you, then you could do the following: Think of as the next ticks position as where you want to be and do an animation to that from your current position in linear manner. If your ticks subdivide the curve fine enough, nobody will be able to see the linear segments (this should suffice for a game, wouldn't do it for a cutting machine).

这篇关于伴随着额外的要求路径动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆