Libgdx - 弹丸减速停止,然后反转它 [英] Libgdx - Slowing Projectile to stop, then Reversing it
问题描述
当在Libgdx创造游戏上的工作,我没有使用物理引擎为我没有在比赛中活动的部件太多了。因此,有物体从屏幕底部的顶部落下,我使用的文档有什么,这样的事情:
When working with creating games in Libgdx, I have not been using the physics engine as I dont have too many moving parts in the game. So, to have objects fall from the top of the screen to the bottom, I used what the documentation had, something like this:
projectile.y -= 200 * Gdx.graphics.getDeltaTime();
这例子将使说弹丸下井以每秒200像素(我相信)。我所要做的是使它所以两秒钟之后,弹丸会从负200过渡每秒,正每秒200次。我已经尝试使用循环和视频下载,但是这只是冻结整个游戏,并与弹走另一条路解冻。任何想法?
That example will make said projectile go down at 200 pixels per second (I believe). What I am trying to do is make it so after two seconds, the projectile will transition from negative 200 per second, to positive 200 per second. I've tried using loops and Thread.sleep, but that will just freeze the entire game and unfreeze with the projectiles going the other way. Any ideas?
推荐答案
线性插值。
所有你需要做的是确定起点:X1 = -200
All you need to do is determine the start point: x1 = -200
确定终点:X2 = 200
Determine the end point: x2 = 200
确定秒,它需要到达终点的量:Tmax为2.0秒。
Determine the amount of seconds that it takes to reach the end point: tmax = 2.0 sec
确定需要添加到原始到达终点的差:V =(X2-X1)=(200 - (-200))= 400
Determine the difference that you need to add to the original to reach the end point: v = (x2-x1) = (200 - (-200)) = 400
使用线性插值功能:X1 + T * V = X2,其中TE [0 ... 1] //必须被规范为0..1间隔
Use the linear interpolation function: x1 + t*v = x2 where t e [0...1] //must be normalized to 0..1 interval
因此在t = 0,该值是在X1 + 0 = X1;并在t =(TN / TMAX)这是1],该值在X1 + V = X2。
Thus at t = 0, the value is at x1 + 0 = x1; and at t = (tn/tmax) [which is 1], the value is at x1 + v = x2.
因此,所有你需要的是从0到2定时器和公式如下:
So all you need is a timer from 0 to 2 and the following equation:
float interpolationTimer = 0.0f;
final float interpolationTimerMax = 2.0f;
public void render()
{
float delta = Gdx.graphics.getDeltaTime();
interpolationTimer += delta;
if(interpolationTimer > interpolationTimerMax )
{
interpolationTimer = interpolationTimerMax ;
}
velocity.y = -200 + (interpolationTimer/interpolationTimerMax) * (400); //x1 + t*v = x2
projectile.y -= velocity.y * delta;
}
这篇关于Libgdx - 弹丸减速停止,然后反转它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!