简体   繁体   English

libGDX 中对象的速度

[英]Speed of objects in libGDX

What is the correct way to move objects in-game with the given speed in libGDX?在 libGDX 中以给定速度在游戏中移动对象的正确方法是什么? Eg.例如。 I have a circle at the bottom of the screen and I want to move it to the top with speed 10 pixels per second.我在屏幕底部有一个圆圈,我想以每秒 10 像素的速度将它移动到顶部。 All phones have different speeds so delta time of render function on every phone is different so how I can do it?所有手机都有不同的速度,因此每部手机上渲染功能的增量时间都不同,所以我该怎么做?

I'm not sure what you exactly mean by this:我不确定你的意思是什么:

All phones have different speeds so delta time of render function on every phone is different...所有手机都有不同的速度,因此每部手机上渲染功能的增量时间都不同...

But I think your understanding of delta value in rendering process is incorrect.但我认为您对渲染过程中delta值的理解是不正确的。 As you probably know already, the render method is called multiple times per second, and after every finished call to render method the screen is updated.您可能已经知道, render方法每秒被调用多次,并且在每次完成对render方法的调用后,屏幕都会更新。 How many times the render method is called can be found by checking Gdx.graphics.getFramesPerSecond() .通过检查Gdx.graphics.getFramesPerSecond()可以找到调用渲染方法的次数。 So what is the purpose of delta value, exactly?那么delta值的目的究竟是什么? delta is simply the time span between the current frame (this render call) and the last frame (render call just before the current one) in seconds. delta只是当前帧(此渲染调用)和最后一帧(当前帧之前的渲染调用)之间的时间跨度,以秒为单位。

From physics we know that distance = velocity * time .从物理学我们知道distance = velocity * time

So, to move object by the distance of 10 units per 1 second (unit can be pixels, meters, etc... -- this actually depends on your camera and world rendering logic), we have to compute correct traveled distance for current frame.因此,要以每 1 秒 10 个单位的距离移动对象(单位可以是像素、米等……这实际上取决于您的相机和世界渲染逻辑),我们必须计算当前帧的正确行进距离. We know the velocity and the elapsed time ( delta ).我们知道速度和经过的时间 ( delta )。 We can compute next position like this我们可以像这样计算下一个位置

public void render(float delta) {
    float velocity = 10.0f; // actually 10.0units / 1s
    position = position + velocity * delta; // position can be circle.y to travel up
}

You can set the delta time with the libGDX method:您可以使用libGDX方法设置增量时间:

world.step(Gdx.graphics.getDeltaTime());

At the end of the render() method.render()方法的末尾。 This allows to libGDX manage the FPS .这允许libGDX管理FPS

To adapt the objects speed as you like you can user the Pixel Per Meter Factor: http://seanballais.github.io/blog/box2d-and-the-pixel-per-meter-ratio/要根据需要调整对象速度,您可以使用每米像素系数: http : //seanballais.github.io/blog/box2d-and-the-pixel-per-meter-ratio/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM