简体   繁体   English

C ++ DirectX运动

[英]C++ DirectX Movement

So I have been playing around with DirectX11 lately and I'm still pretty new at it. 所以最近我一直在玩DirectX11,但我仍然很新。 I'm trying to move something right now with the translation and this is what I've got. 我现在正在尝试通过翻译移动某些内容,这就是我所拥有的。 I've been reading Frank D Luna's book on DirectX11 and he provides a gameTimer class but I really am not sure how to use delta time. 我一直在阅读Frank D Luna在DirectX11上的书,他提供了gameTimer类,但是我真的不确定如何使用时间差。 This is the small snippet of code I was working with. 这是我正在使用的一小段代码。 Obviously this won't work because whenever I'm not pressing the key the time is still increasing and it's total time. 显然,这是行不通的,因为每当我不按该键时,时间仍在增加,这是总时间。

// Button down event.
if (GetAsyncKeyState('W') & 0x8000)
{
    XMMATRIX carTranslate;
    // Every quarter second incremete it
    static float t_base = 0.0f;
    if( (mTimer.TotalTime() - t_base) >= 0.25f )
        t_base += 0.25f;

    carPos.x = mTimer.TotalTime();
    carPos.y = 1.0f;
    carPos.z = 0.0f;
    carTranslate = XMMatrixTranslation(carPos.x, carPos.y, carPos.z);

    XMStoreFloat4x4(&mCarWorld, XMMatrixMultiply(carScale, carTranslate));
}

Usually we constantly render frames (redrawing screen) in a while loop (so called "main loop"). 通常,我们会在while循环(即所谓的“主循环”)中不断渲染帧(重画屏幕)。 To "move" an object we just draw it in another position than it was in previous frame. 要“移动”对象,我们只需将其绘制到与前一帧不同的位置即可。

To move objects consistently, you need to know a time between frames. 为了一致地移动对象,您需要知道帧之间的时间。 We call it "delta time" ( dt ). 我们称其为“增量时间”( dt )。 So, between frames, time increases by dt . 因此,在帧之间,时间增加了dt Given velocity (speed) of object ( v ), we can calculate displacement as dx = dt * v . 给定物体( v )的速度(速度),我们可以将位移计算为dx = dt * v Then, to get current position, we just add dx to previous position: x += dx . 然后,要获得当前位置,我们只需将dx添加到先前位置: x += dx

Note, that is a bad idea to calculate delta just inside your update or rendering code. 请注意,在更新或呈现代码内部计算增量不是一个好主意。 Avoiding spreading out this functionality, we usually localize this calculations in timer/clock class. 为避免扩展此功能,我们通常将此计算本地化为计时器/时钟类。

Here is a simplified example: 这是一个简化的示例:

// somewhere in timer class
// `Time` and `Duration` are some time units
class Timer {
    Time m_previousTime;
    Duration m_delta;
public:
    Duration getDelta() const { return m_delta; }

    void tick() {
        m_delta = currentTime() - m_previousTime; // just subtract
        m_previousTime = currentTime; // `current` becomes `previous` for next frame
    }
};

// main loop
while(rendering) {
    Timer.tick();
    Frame(m_Timer.getDelta());
}

void Frame(Duration dt) {
    if(keyPressed) {
        object.position += dt * object.velocity;
    }
}

You can now even make your object to move with acceleration (throttle, gravity, etc.): 您现在甚至可以使对象以加速(油门,重力等)移动:

    object.velocity += dt * object.acceleration;
    object.position += dt * object.velocity;

Hope you got the idea! 希望你有主意! Happy coding! 编码愉快!

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

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