Skip to content

Commit

Permalink
Change the direction fields of some TnuaBuiltinDashState variants…
Browse files Browse the repository at this point in the history
… from `Vector3` to `Dir3`
  • Loading branch information
idanarye committed Oct 11, 2024
1 parent 0e6bede commit 468ad24
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ NOTE: Subcrates have their own changelogs: [bevy-tnua-physics-integration-layer]
`&mut Self` (this was always redundant, since they get called from queries
anyway rather than on freshly created objects, so they don't benefit from a
fluent API)
- `desired_forward` fields of `TnuaBuiltinWalk` and `TnuaBuiltinDash` was
- `desired_forward` fields of `TnuaBuiltinWalk` and `TnuaBuiltinDash` were
changed from `Vector3` to `Option<Dir3>`.
- The `direction` fields of some of `TnuaBuiltinDashState`'s variants were
changed from `Vector3` to `Dir3`.

## 0.19.0 - 2024-07-05
### Changed
Expand Down
32 changes: 18 additions & 14 deletions src/builtins/dash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::math::{AdjustPrecision, Float, Vector3};
use crate::math::{AdjustPrecision, AsF32, Float, Vector3};
use bevy::prelude::*;

use crate::util::rotation_arc_around_axis;
Expand Down Expand Up @@ -95,12 +95,12 @@ impl TnuaAction for TnuaBuiltinDash {
for _ in 0..3 {
return match state {
TnuaBuiltinDashState::PreDash => {
// Probably unneeded because of the `initiation_decision`, but still
if !self.displacement.is_finite() || self.displacement == Vector3::ZERO {
let Ok(direction) = Dir3::new(self.displacement.f32()) else {
// Probably unneeded because of the `initiation_decision`, but still
return TnuaActionLifecycleDirective::Finished;
}
};
*state = TnuaBuiltinDashState::During {
direction: self.displacement.normalize(),
direction,
destination: ctx.tracker.translation + self.displacement,
desired_forward: self.desired_forward,
consider_blocked_if_speed_is_less_than: Float::NEG_INFINITY,
Expand All @@ -113,25 +113,29 @@ impl TnuaAction for TnuaBuiltinDash {
desired_forward,
consider_blocked_if_speed_is_less_than,
} => {
let distance_to_destination =
direction.dot(*destination - ctx.tracker.translation);
let distance_to_destination = direction
.adjust_precision()
.dot(*destination - ctx.tracker.translation);
if distance_to_destination < 0.0 {
*state = TnuaBuiltinDashState::Braking {
direction: *direction,
};
continue;
}

let current_speed = direction.dot(ctx.tracker.velocity);
let current_speed = direction.adjust_precision().dot(ctx.tracker.velocity);
if current_speed < *consider_blocked_if_speed_is_less_than {
return TnuaActionLifecycleDirective::Finished;
}

motor.lin = Default::default();
motor.lin.acceleration = -ctx.tracker.gravity;
motor.lin.boost = (*direction * self.speed - ctx.tracker.velocity)
motor.lin.boost = (direction.adjust_precision() * self.speed
- ctx.tracker.velocity)
.clamp_length_max(ctx.frame_duration * self.acceleration);
let expected_speed = direction.dot(ctx.tracker.velocity + motor.lin.boost);
let expected_speed = direction
.adjust_precision()
.dot(ctx.tracker.velocity + motor.lin.boost);
*consider_blocked_if_speed_is_less_than = if current_speed < expected_speed {
0.5 * (current_speed + expected_speed)
} else {
Expand All @@ -158,11 +162,11 @@ impl TnuaAction for TnuaBuiltinDash {
TnuaActionLifecycleDirective::StillActive
}
TnuaBuiltinDashState::Braking { direction } => {
let remaining_speed = direction.dot(ctx.tracker.velocity);
let remaining_speed = direction.adjust_precision().dot(ctx.tracker.velocity);
if remaining_speed <= self.brake_to_speed {
TnuaActionLifecycleDirective::Finished
} else {
motor.lin.boost = -*direction
motor.lin.boost = -direction.adjust_precision()
* (remaining_speed - self.brake_to_speed).min(self.brake_acceleration);
TnuaActionLifecycleDirective::StillActive
}
Expand All @@ -179,12 +183,12 @@ pub enum TnuaBuiltinDashState {
#[default]
PreDash,
During {
direction: Vector3,
direction: Dir3,
destination: Vector3,
desired_forward: Option<Dir3>,
consider_blocked_if_speed_is_less_than: Float,
},
Braking {
direction: Vector3,
direction: Dir3,
},
}

0 comments on commit 468ad24

Please sign in to comment.