[英]Smooth moving from A to B
I have a android game where I have to from time to time move camera (visible screen part) from point A to point B. 我有一个Android游戏,必须不时将相机(可见屏幕部分)从A点移到B点。
Obviously I could do it like that: 显然,我可以这样做:
camera.setCenter(B.getX(), B.getY();
But it does not looks good, it simply jump immediately, and what I want to achieve is smooth movement from A to B. I can access onUpdate method which is loop updating certain game objects (so I can execute certain things in certain milliseconds) 但是它看起来并不好,它只是立即跳转,我想要实现的是从A到B的平滑移动。我可以访问onUpdate方法,该方法可以循环更新某些游戏对象(因此我可以在几毫秒内执行某些事情)
I really can not figure out how to create such algorithm, to allow smooth movement between two points (Classifying I have no clue how to calculate what values should I add to the 我真的不知道如何创建这样的算法,以允许两点之间的平滑移动(分类我不知道如何计算应该添加到什么值
camera.setCenter(camera.getX() + xValue, camera.getY() + yValue)
Because those values have to be calculated depends on what is distance between those two points. 因为必须计算这些值取决于这两点之间的距离。
If your game updates at 60 frames per second, and you want a transition to take two seconds, then it should take 120 frames to get there. 如果您的游戏以每秒60帧的速度更新,并且您希望转换花费2秒钟,那么到达该位置需要120帧。
Thus, the amount it should move in any given update is total/120
. 因此,它在任何给定更新中应移动的
total/120
为total/120
。
You can abstract this into a method that queues a movement, calculates the FPS and the amount to move each update, and calls .setCenter
on its current position plus the update amount. 您可以将其抽象为一种方法,该方法对移动进行排队,计算FPS和移动每次更新的数量,并在其当前位置加上更新数量时调用
.setCenter
。 This is typically called interpolation . 这通常称为插值 。 To determine how far you need to move, its no more than the absolute value of
current - destination
(it doesn't matter which is in front of the other, so we just take the absolute value to ignore the sign. To be technical, distance is a scalar whereas position is a vector ). 要确定您需要移动多远,它的最大值不超过
current - destination
的绝对值(在另一个的前面无所谓),因此我们只需要使用绝对值来忽略该符号即可。 distance是一个标量,而position是一个向量 )。
So, to sum up in semi-code (I don't know the actual contracts for these functions): 因此,以半代码总结(我不知道这些功能的实际合同):
//given these variables already have value
double fps, currentX, destinationX, currentY, destinationY, animationLength; //animation length in seconds
...
double xValue = Math.abs(currentX - destinationX);
double yValue = Math.abs(currentY - destinationY);
...
//on each update:
double amountToMoveX = xValue / (animationLength * fps);
double amountToMoveY = yValue / (animationLength * fps);
camera.setCenter(camera.getX() + amountToMoveX, camera.getY() + amountToMoveY)
Then you simply need to keep track of when to stop, and you should be good to go. 然后,您只需要跟踪何时停止,就可以了。
Whether or not you're using an engine that already has a means of doing this, I don't know. 我不知道您是否使用的引擎已经可以做到这一点。 But in terms of a pure algorithm, this seems the way to go.
但是就纯算法而言,这似乎是可行的方法。
See the discussion from this question 查看有关此问题的讨论
From the answer: 从答案:
length=square_root((bx - ax)^2+(by - ay)^2)
长度= square_root((bx-ax)^ 2 +(by-ay)^ 2)
velocityX = (bx - ax) / length * speed
velocityX =(bx-ax)/长度*速度
velocityY = (by - ay) / length * speed
速度Y =(乘以-ay)/长度*速度
Where xValue is velocityX and similarly for Y 其中xValue是velocityX,Y相似
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.