Skip to content

Commit 768f17e

Browse files
committed
Merge #133
133: Make key axis deltas continuous instead of discrete r=vitvakatu a=alteous Fixes unwanted 'jagged' input behaviour. This most notably affects the first person camera controls. #### Behaviour before fix Frame | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- Event | - | P | - | - | - | P | P | R | - | - Move | N | Y | N | N | N | Y | Y | N | N | N #### Behaviour after fix Frame | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- Event | - | P | - | - | - | P | P | R | - | - Move | N | Y | Y | Y | Y | Y | Y | N | N | N
2 parents 17d64b1 + e830f83 commit 768f17e

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

src/controls/first_person.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,7 @@ impl FirstPerson {
339339
&mut self,
340340
input: &Input,
341341
) {
342-
let dtime = input.delta_time();
343-
let dlook = dtime * self.look_speed;
344-
342+
let dlook = input.delta_time() * self.look_speed;
345343
let mouse = input.mouse_delta_raw();
346344

347345
self.yaw += dlook * mouse.x;
@@ -360,7 +358,7 @@ impl FirstPerson {
360358
self.axes
361359
.vertical
362360
.map(|a| if let Some(diff) = input.timed(a) {
363-
self.position.y += self.move_speed * diff * dtime;
361+
self.position.y += self.move_speed * diff;
364362
});
365363

366364
self.axes

src/input/mod.rs

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -261,23 +261,37 @@ impl Input {
261261
hit.hit(self)
262262
}
263263

264-
/// Returns `Option<delta>` value for specified axis, where `delta` is either `i8` for
265-
/// [`axis::Key`](struct.Key.html) or `f32` for [`axis::Raw`](struct.Raw.html).
264+
/// Returns the change ('delta') in input state since the last call to
265+
/// [`Window::update`].
266266
///
267-
/// Delta value for [`axis::Key`](struct.Key.html) represents the amount of `positive` hits minus
268-
/// amount of `negative` hits.
267+
/// This value depends on the type of input device is given.
269268
///
270-
/// Delta value for [`axis::Raw`](struct.Raw.html) represents the sum of all
271-
/// [raw movements](struct.Input.html#method.axes_movements) along specific axis.
269+
/// [`axis::Key`]
270+
///
271+
/// * `None` when no updates to the axis are received.
272+
/// * `Some(1)` when only positive input to the axis is received.
273+
/// * `Some(-1)` when only negative input to the axis is received.
274+
/// * `Some(0)` when both positive and negative input to the axis is received.
275+
///
276+
/// [`axis::Raw`]
277+
///
278+
/// * `None` when no updates to the axis are received and
279+
/// * `Some(x)` where `x` is the sum of positive and negative inputs otherwise.
280+
///
281+
/// [`Window::update`]: window/struct.Window.html#method.update
282+
/// [`axis::Key`]: input/axis/struct.Key.html
283+
/// [`axis::Raw`]: input/axis/struct.Raw.html
272284
pub fn delta<D: Delta>(
273285
&self,
274286
delta: D,
275287
) -> <D as Delta>::Output {
276288
delta.delta(self)
277289
}
278290

279-
/// The shortcut for [delta](struct.Input.html#method.delta) *
280-
/// [delta_time](struct.Input.html#method.delta_time).
291+
/// Shortcut for [`delta`] `*` [`delta_time`].
292+
///
293+
/// [`delta`]: struct.Input.html#method.delta
294+
/// [`delta_time`]: struct.Input.html#method.delta_time
281295
pub fn timed<D: Delta>(
282296
&self,
283297
delta: D,
@@ -323,23 +337,38 @@ impl Hit for Button {
323337
input: &Input,
324338
) -> bool {
325339
match *self {
326-
Button::Key(button) => input.state.keys_pressed.contains(&button),
327-
Button::Mouse(button) => input.state.mouse_pressed.contains(&button),
340+
Button::Key(button) => button.hit(input),
341+
Button::Mouse(button) => button.hit(input),
328342
}
329343
}
330344
}
331345

346+
impl Hit for Key {
347+
fn hit(
348+
&self,
349+
input: &Input,
350+
) -> bool {
351+
input.state.keys_pressed.contains(self)
352+
}
353+
}
354+
355+
impl Hit for MouseButton {
356+
fn hit(
357+
&self,
358+
input: &Input,
359+
) -> bool {
360+
input.state.mouse_pressed.contains(self)
361+
}
362+
}
363+
332364
impl Hit for axis::Key {
333365
fn hit(
334366
&self,
335367
input: &Input,
336368
) -> bool {
337-
input
338-
.delta
339-
.keys_hit
340-
.iter()
341-
.filter(|&&k| k == self.pos || k == self.neg)
342-
.count() > 0
369+
let pos_hit = input.state.keys_pressed.contains(&self.pos);
370+
let neg_hit = input.state.keys_pressed.contains(&self.neg);
371+
pos_hit || neg_hit
343372
}
344373
}
345374

@@ -444,11 +473,11 @@ impl Delta for axis::Key {
444473
&self,
445474
input: &Input,
446475
) -> Self::Output {
447-
let (pos, neg) = self.hit_count(input);
448-
if pos + neg == 0 {
449-
None
450-
} else {
451-
Some(pos as i8 - neg as i8)
476+
match (self.pos.hit(input), self.neg.hit(input)) {
477+
(true, true) => Some(0),
478+
(true, false) => Some(1),
479+
(false, true) => Some(-1),
480+
(false, false) => None,
452481
}
453482
}
454483

@@ -457,7 +486,7 @@ impl Delta for axis::Key {
457486
input: &Input,
458487
) -> Option<TimerDuration> {
459488
self.delta(input)
460-
.map(|v| v as TimerDuration * input.delta_time())
489+
.map(|delta| delta as TimerDuration * input.delta_time())
461490
}
462491
}
463492

0 commit comments

Comments
 (0)