简体   繁体   English

非常慢且不稳定的相机 Controller in Rust

[英]Very Slow and Choppy Camera Controller in Rust

I am currently working on simple Rendering in Rust using the OpenGL Wrapper Glium.我目前正在使用 OpenGL Wrapper Glium 在 Rust 中进行简单渲染。 I have a simple 3D model and a Camera ready set up and started working on a fitting camera controller.我有一个简单的 3D model 和一个准备好的相机,并开始使用合适的相机 controller。 I'm using device_query to check for keyboard input and if it's a Specific Keyboard input I just change the x and y coordinates.我正在使用 device_query 检查键盘输入,如果它是特定键盘输入,我只需更改 x 和 y 坐标。 This works, however it works really bad, inconstantly and choppy.这行得通,但它的工作原理非常糟糕,反复无常和波涛汹涌。

My Event loop:我的事件循环:

event_loop.run(move |event, _, control_flow| {
        let next_frame_time =
            std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667);
####### This is where I change the coordinates ########        
        x = keyboardinput::keyboard_input_x(x);
        y = keyboardinput::keyboard_input_y(y);
        *control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time);

        match event {
            glutin::event::Event::WindowEvent { event, .. } => match event {
                glutin::event::WindowEvent::CloseRequested => {
                    *control_flow = glutin::event_loop::ControlFlow::Exit;
                    return;
                }
                _ => return,
            },
            glutin::event::Event::NewEvents(cause) => match cause {
                glutin::event::StartCause::ResumeTimeReached { .. } => (),
                glutin::event::StartCause::Init => (),
                _ => return,
            },
            _ => return,
        }

This is my Keyboard input rs file:这是我的键盘输入 rs 文件:

use device_query::{DeviceQuery, DeviceState, Keycode};

pub fn keyboard_input_x(mut x: f32) -> f32 {
    let device_state = DeviceState::new();
    let keys: Vec<Keycode> = device_state.get_keys();
    for key in keys.iter() {
        println!("Pressed key: {:?}", key);
        if key == &Keycode::W {
            x -= 0.03;
            return x;
        }
        if key == &Keycode::S {
            x += 0.03;
            return x;
        }
    }
    return x;
}

pub fn keyboard_input_y(mut y: f32) -> f32 {
    let device_state = DeviceState::new();
    let keys: Vec<Keycode> = device_state.get_keys();
    for key in keys.iter() {
        println!("Pressed key: {:?}", key);
        if key == &Keycode::Space {
            y += 0.03;
            return y;
        }
        if key == &Keycode::LControl {
            y -= 0.03;
            return y;
        }
    }
    return y;
}

What can I do the make the camera as smooth as possible?我该怎么做才能使相机尽可能平滑?

Instead of directly updating your x / y position in the input handler, create two variables holding your x velocity and y velocity ( xv / yv ), and then in your input handler do something like this:不要在输入处理程序中直接更新您的x / y position,而是创建两个变量来保存您的 x 速度和 y 速度( xv / yv ),然后在您的输入处理程序中执行以下操作:

bool space_pressed = false;
for key in keys.iter() {
    if key == &Keycode::Space {
        space_pressed = true;
    }
}

if space_pressed {
    yv = 1.0; // Move up.
} else {
    yv = 0.0; // Don't move.
}

And in your main loop where you compute each frame, count the amount of time since the last frame in seconds (often called delta-t or dt ), and integrate your x velocity and y velocity to a new position:在计算每一帧的主循环中,以秒为单位计算自上一帧以来的时间量(通常称为 delta-t 或dt ),并将 x 速度和 y 速度整合到新的 position 中:

// Update camera position.
x += xv * dt;
y += yv * dt;

Now when you hold space you will move up at a constant velocity, rather than unpredictably and choppy.现在,当您保持空间时,您将以恒定的速度向上移动,而不是不可预测和波涛汹涌。

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

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