diff options
| author | Valentin Popov <valentin@popov.link> | 2026-11-26 19:37:09 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-11-26 19:37:09 +0300 |
| commit | 1f366ac0d39a3b1f0b19721ed78936bad1e504dd (patch) | |
| tree | 431e3cebf1f3c96af086e928b3acdae53df48d1c /crates/fparkan-runtime/src | |
| parent | f3d1b4d2aff8e47003626129641c36e44c7fb040 (diff) | |
| download | fparkan-1f366ac0d39a3b1f0b19721ed78936bad1e504dd.tar.xz fparkan-1f366ac0d39a3b1f0b19721ed78936bad1e504dd.zip | |
fix(movement): carry hero walking velocity between ticks
Take both horizontal limits and acceleration from the active Control
profile. Apply the measured slope braking after the input update and
clear velocity on a blocking wall, teleport or boarding.
Keep the terrain limit during geometric sweeps so momentum cannot skip
a narrow cliff. In the stock Firing Line window the hero reached the
slope, stopped at its foot, backed away, and coasted on key release.
Diffstat (limited to 'crates/fparkan-runtime/src')
| -rw-r--r-- | crates/fparkan-runtime/src/boarding.rs | 2 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/hero_walk.rs | 164 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/lib.rs | 51 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/manual_flight.rs | 2 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/player_motion.rs | 114 | ||||
| -rw-r--r-- | crates/fparkan-runtime/src/walking_slope.rs | 45 |
6 files changed, 270 insertions, 108 deletions
diff --git a/crates/fparkan-runtime/src/boarding.rs b/crates/fparkan-runtime/src/boarding.rs index cb15f420..afb2e50d 100644 --- a/crates/fparkan-runtime/src/boarding.rs +++ b/crates/fparkan-runtime/src/boarding.rs @@ -142,6 +142,7 @@ pub fn board_friendly_warbot( flight_velocity: [0.0; 3], }); mission.player_pitch_bits = 0; + mission.hero_walk_velocity = [0.0; 2]; } Ok(Some(cockpit)) } @@ -222,6 +223,7 @@ pub fn disembark_warbot(engine: &mut Engine) -> Result<Option<StartingCockpit>, } mission.boarding = None; mission.player_pitch_bits = 0; + mission.hero_walk_velocity = [0.0; 2]; Ok(Some(hero)) } diff --git a/crates/fparkan-runtime/src/hero_walk.rs b/crates/fparkan-runtime/src/hero_walk.rs new file mode 100644 index 00000000..22601196 --- /dev/null +++ b/crates/fparkan-runtime/src/hero_walk.rs @@ -0,0 +1,164 @@ +//! Translational velocity and slope braking for the walking mode of Control. + +use crate::manual_flight::ramp_axis; + +#[derive(Clone, Copy)] +pub(super) struct WalkingDrive { + pub(super) velocity: [f32; 2], + pub(super) limits: [f32; 2], + acceleration: [f32; 2], + slope: WalkingSlope, +} + +impl WalkingDrive { + pub(super) fn from_profile(words: &[u32; 27], velocity: [f32; 2]) -> Option<Self> { + let slope = WalkingSlope::from_profile(words)?; + let acceleration = [0, 1].map(|word| f32::from_bits(words[word])); + let limits = [6, 7].map(|word| f32::from_bits(words[word])); + acceleration + .into_iter() + .chain(limits) + .all(|rate| rate.is_finite() && rate >= 0.0) + .then(|| Self { + velocity: std::array::from_fn(|axis| { + velocity[axis].clamp(-limits[axis], limits[axis]) + }), + limits, + acceleration, + slope, + }) + } + + pub(super) fn advance(self, axes: [f32; 2], seconds: f32) -> ([f32; 2], [f32; 2]) { + let [(vx, across), (vy, along)] = std::array::from_fn(|axis| { + ramp_axis( + self.velocity[axis], + axes[axis].clamp(-1.0, 1.0) * self.limits[axis], + self.acceleration[axis], + seconds, + ) + }); + ([vx, vy], [across, along]) + } + + pub(super) fn brake( + self, + velocity: [f32; 2], + normal: [f32; 3], + yaw: f32, + seconds: f32, + ) -> Option<([f32; 2], [f32; 2])> { + let [across, along] = velocity; + let (sin, cos) = yaw.sin_cos(); + let world_velocity = [cos * across - sin * along, sin * across + cos * along]; + let factor = self.slope.speed_factor(normal, world_velocity); + if factor == 1.0 { + return None; + } + let velocity = std::array::from_fn(|axis| { + ramp_axis( + velocity[axis], + velocity[axis] * factor, + self.acceleration[axis] * 1.5, + seconds, + ) + .0 + }); + // The scene uses short geometric sweeps. Bridge the surviving speeds + // at the ends of that sweep while the native contact solver is open. + let distance = if factor == 0.0 { + // The surface cannot carry the hero uphill at this inclination. + // Keep braking the stored velocity while the ground blocks travel. + [0.0; 2] + } else { + std::array::from_fn(|axis| (self.velocity[axis] + velocity[axis]) * (0.5 * seconds)) + }; + Some((velocity, distance)) + } +} + +#[derive(Clone, Copy)] +struct WalkingSlope { + minimum_up: f32, +} + +impl WalkingSlope { + fn from_profile(words: &[u32; 27]) -> Option<Self> { + let angle = f32::from_bits(words[23]); + let minimum_up = angle.cos(); + (words[21] == 2 && angle > 0.0 && angle <= std::f32::consts::FRAC_PI_2 && minimum_up < 1.0) + .then_some(Self { minimum_up }) + } + + fn speed_factor(self, normal: [f32; 3], direction: [f32; 2]) -> f32 { + // Control+0x153c0 applies the limit on an ascent and along the contour. + let along_normal = direction[0] * normal[0] + direction[1] * normal[1]; + if along_normal > 0.0 { + return 1.0; + } + (2.0 * (normal[2] - self.minimum_up) / (1.0 - self.minimum_up)).clamp(0.0, 1.0) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn hero_profile_limits_steep_ascent_and_contour() { + let mut words = [0; 27]; + words[21] = 2; + words[23] = 0.6_f32.to_bits(); + let slope = WalkingSlope::from_profile(&words).unwrap(); + let up_normal = |angle: f32| [0.0, -angle.sin(), angle.cos()]; + + assert_eq!(slope.speed_factor(up_normal(0.3), [0.0, 1.0]), 1.0); + let uphill = slope.speed_factor(up_normal(0.5), [0.0, 1.0]); + assert!((14.0 * uphill - 8.375_571).abs() < 0.000_01); + assert_eq!(slope.speed_factor(up_normal(0.7), [0.0, 1.0]), 0.0); + assert_eq!(slope.speed_factor(up_normal(0.7), [1.0, 0.0]), 0.0); + assert_eq!(slope.speed_factor(up_normal(0.7), [0.0, -1.0]), 1.0); + } + + #[test] + fn profile_walk_keeps_speed_across_contact_and_input_release() { + let mut words = [0; 27]; + words[0] = 180.0_f32.to_bits(); + words[1] = 140.0_f32.to_bits(); + words[6] = 1.0_f32.to_bits(); + words[7] = 14.0_f32.to_bits(); + words[21] = 2; + words[23] = 0.6_f32.to_bits(); + let mut drive = WalkingDrive::from_profile(&words, [0.0; 2]).unwrap(); + let (sin, cos) = 0.7_f32.sin_cos(); + let normal = [0.0, -sin, cos]; + for (axes, seconds, steep, expected) in [ + ([1.0, 1.0], 0.02, false, [1.0, 2.8]), + ([1.0, 1.0], 0.02, false, [1.0, 5.6]), + ([0.0, 0.0], 0.02, false, [0.0, 2.8]), + ([0.0, 0.0], 0.02, false, [0.0, 0.0]), + ([0.0, 1.0], 0.10, false, [0.0, 14.0]), + ([0.0, 1.0], 0.02, true, [0.0, 9.8]), + ([0.0, 1.0], 0.02, true, [0.0, 8.4]), + ([0.0, 0.0], 0.02, true, [0.0, 1.4]), + ([0.0, 0.0], 0.02, true, [0.0, 0.0]), + ([0.0, 1.0], 0.02, true, [0.0, 0.0]), + ([0.0, -1.0], 0.02, true, [0.0, -2.8]), + ] { + let (mut velocity, _) = drive.advance(axes, seconds); + if steep { + if let Some((braked, _)) = drive.brake(velocity, normal, 0.0, seconds) { + velocity = braked; + } + } + assert!( + velocity + .into_iter() + .zip(expected) + .all(|(v, expected)| (v - expected).abs() < 0.000_01), + "{velocity:?} expected {expected:?}" + ); + drive.velocity = velocity; + } + } +} diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs index 3afb3717..c21a1c89 100644 --- a/crates/fparkan-runtime/src/lib.rs +++ b/crates/fparkan-runtime/src/lib.rs @@ -97,6 +97,7 @@ use behavior_clock::next_crt_rand; mod mesh_collision; mod mining; pub use laboratory::LaboratoryReport; +mod hero_walk; mod main_teleport; mod manual_chassis; mod manual_flight; @@ -106,7 +107,6 @@ mod patrol_points; mod player_gait; mod player_motion; mod unit_speed; -mod walking_slope; pub use patrol_points::PatrolPointReport; pub use unit_speed::{UnitSpeedKind, UnitSpeedReport}; mod factory_release; @@ -1475,6 +1475,7 @@ struct LoadedMissionState { unit_patrol: Option<unit_patrol::UnitPatrol>, player_pitch_bits: u32, player_gait: player_gait::PlayerGait, + hero_walk_velocity: [f32; 2], boarding: Option<boarding::Boarding>, local_hero: Option<ObjectHandle>, script_host_mode: MissionMode, @@ -2807,6 +2808,7 @@ node_provider_distribution_suspended: false, unit_patrol, player_pitch_bits: 0, player_gait: Default::default(), + hero_walk_velocity: [0.0; 2], boarding: None, local_hero, script_host_mode: request.mode, @@ -3034,7 +3036,12 @@ pub fn frame_with_player_motion( if let Some(mission) = &mut engine.loaded { mission.player_pitch_bits = motion.pitch_bits; frame.player_pitch_bits = motion.pitch_bits; - player_motion::commit_flight(mission, cockpit, motion, blocked); + player_motion::commit_velocity( + mission, + cockpit, + motion, + blocked || drowned || landing.is_some(), + ); if !blocked && !drowned && landing.is_none() @@ -18020,19 +18027,20 @@ mod tests { pose(&frame_with_player_motion(&mut engine, 200, cockpit, proposed, false).unwrap()); let [x0, y0, z0] = original.position.map(f32::from_bits); let [x1, y1, z1] = walked.position.map(f32::from_bits); - let runtime_speed = f32::from_bits( - loaded_mission_control_device_tables(&engine) - .unwrap() - .iter() - .find(|table| table.handle == cockpit.handle) - .unwrap() - .owner_profile - .as_ref() - .unwrap() - .current_words[7], - ); + let profile = &loaded_mission_control_device_tables(&engine) + .unwrap() + .iter() + .find(|table| table.handle == cockpit.handle) + .unwrap() + .owner_profile + .as_ref() + .unwrap() + .current_words; + let runtime_speed = f32::from_bits(profile[7]); + let acceleration = f32::from_bits(profile[1]); assert!((runtime_speed - 14.0).abs() < 0.01); - assert!(((x1 - x0).hypot(y1 - y0) - runtime_speed * 0.1).abs() < 1e-4); + let coast_distance = 0.5 * runtime_speed * runtime_speed / acceleration; + assert!(((x1 - x0).hypot(y1 - y0) - (runtime_speed * 0.1 - coast_distance)).abs() < 1e-4); let ground = |xy| { loaded_terrain(&engine) .unwrap() @@ -18068,8 +18076,21 @@ mod tests { } let [split_x, split_y, _] = split.position.map(f32::from_bits); assert!((split_x - x2).hypot(split_y - y2) < 1e-4); - let idle = frame(&mut engine, 700).unwrap(); + let release = PlayerMotion { + delta_seconds: 0.2, + ..PlayerMotion::default() + }; + let release_step = plan_player_motion(&engine, cockpit, release) + .unwrap() + .unwrap(); + let idle = + frame_with_player_motion(&mut engine, 800, cockpit, release_step, false).unwrap(); assert_eq!(idle.player_pitch_bits, blocked.player_pitch_bits); + let [x3, y3, _] = pose(&idle).position.map(f32::from_bits); + assert!(((x3 - split_x).hypot(y3 - split_y) - coast_distance).abs() < 1e-4); + assert!(plan_player_motion(&engine, cockpit, release) + .unwrap() + .is_none()); } #[test] diff --git a/crates/fparkan-runtime/src/manual_flight.rs b/crates/fparkan-runtime/src/manual_flight.rs index db6bee42..cac50b41 100644 --- a/crates/fparkan-runtime/src/manual_flight.rs +++ b/crates/fparkan-runtime/src/manual_flight.rs @@ -88,7 +88,7 @@ impl FlightDrive { } } -fn ramp_axis(velocity: f32, target: f32, acceleration: f32, seconds: f32) -> (f32, f32) { +pub(super) fn ramp_axis(velocity: f32, target: f32, acceleration: f32, seconds: f32) -> (f32, f32) { if acceleration == 0.0 || velocity == target { return (velocity, velocity * seconds); } diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs index 18164ec0..8ac5eb5a 100644 --- a/crates/fparkan-runtime/src/player_motion.rs +++ b/crates/fparkan-runtime/src/player_motion.rs @@ -1,6 +1,6 @@ use super::{transform_state, Engine, EngineError, StartingCockpit, SurfaceQuery, TransformState}; use crate::manual_flight::{FlightDrive, FlightProfile}; -use crate::{walking_slope::WalkingSlope, TerrainWorld}; +use crate::{hero_walk::WalkingDrive, TerrainWorld}; use fparkan_resource::CachedResourceRepository; use fparkan_terrain::{FullSurfaceMask, TerrainSupport}; @@ -65,6 +65,7 @@ pub struct PlayerMotionStep { pub(super) pose: TransformState, pub(super) pitch_bits: u32, flight_velocity: Option<[f32; 3]>, + hero_walk_velocity: Option<[f32; 2]>, max_floor_clearance: f32, previous_terrain_height: Option<f32>, terrain_height: Option<f32>, @@ -176,13 +177,10 @@ impl PlayerMotionStep { } } - fn slope_speed_factor(self, limit: Option<WalkingSlope>) -> f32 { - if let (WalkingSurface::Ground, Some(limit), Some(normal), Some([from, to])) = - (self.surface, limit, self.terrain_normal, self.sweep) - { - return limit.speed_factor(normal, [to[0] - from[0], to[1] - from[1]]); - } - 1.0 + fn terrain_slope(self) -> Option<[f32; 3]> { + (self.surface == WalkingSurface::Ground && self.sweep.is_some()) + .then_some(self.terrain_normal) + .flatten() } pub(super) fn touches_water(self) -> bool { @@ -222,6 +220,7 @@ mod tests { }, pitch_bits: 0, flight_velocity: None, + hero_walk_velocity: None, max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height: None, terrain_height: Some(11.5), @@ -235,12 +234,8 @@ mod tests { ); assert_eq!(step.terrain_exit_floor(), Some(11.5)); let lowered = step.with_building_floor(11.5, Some(12.0)); - let mut words = [0; 27]; - words[21] = 2; - words[23] = 0.6_f32.to_bits(); - let slope = super::WalkingSlope::from_profile(&words); - assert_eq!(step.slope_speed_factor(slope), 0.0); - assert_eq!(lowered.slope_speed_factor(slope), 1.0); + assert_eq!(step.terrain_slope(), Some([0.0, -0.8, 0.6])); + assert_eq!(lowered.terrain_slope(), None); assert_eq!(lowered.pose.position.map(f32::from_bits), [4.0, 6.0, 11.52]); assert_eq!(lowered.sweep.unwrap()[1], [4.0, 6.0, 12.02]); assert_eq!( @@ -264,6 +259,7 @@ mod tests { pose: TransformState::default(), pitch_bits: 0, flight_velocity: None, + hero_walk_velocity: None, max_floor_clearance: 1.25, previous_terrain_height: Some(10.0), terrain_height: Some(10.0), @@ -287,6 +283,7 @@ mod tests { pose: TransformState::default(), pitch_bits: 0, flight_velocity: None, + hero_walk_velocity: None, max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height: None, terrain_height: None, @@ -438,17 +435,6 @@ fn plan_player_steps( }); let flying = flight.is_some(); let chassis_ground = boarded.and_then(|boarding| boarding.ground); - if input.movement == [0.0; 2] - && flight.is_none_or(|drive| input.climb == 0.0 && drive.velocity == [0.0; 3]) - && input.mouse_x == 0.0 - && input.mouse_y == 0.0 - { - return Ok(None); - } - let pitch = f32::from_bits(mission.player_pitch_bits); - if !input.delta_seconds.is_finite() || input.delta_seconds < 0.0 { - return Err(EngineError::Movement("invalid player step".into())); - } let hero_profile = if mission.local_hero == Some(cockpit.handle) { mission .control_device_tables @@ -459,7 +445,21 @@ fn plan_player_steps( } else { None }; - let slope = hero_profile.and_then(WalkingSlope::from_profile); + let mut walking = hero_profile + .and_then(|profile| WalkingDrive::from_profile(profile, mission.hero_walk_velocity)); + let walking_motion = + input.movement != [0.0; 2] || (walking.is_some() && mission.hero_walk_velocity != [0.0; 2]); + if !walking_motion + && flight.is_none_or(|drive| input.climb == 0.0 && drive.velocity == [0.0; 3]) + && input.mouse_x == 0.0 + && input.mouse_y == 0.0 + { + return Ok(None); + } + let pitch = f32::from_bits(mission.player_pitch_bits); + if !input.delta_seconds.is_finite() || input.delta_seconds < 0.0 { + return Err(EngineError::Movement("invalid player step".into())); + } let speeds = if boarded.is_some() && !flying { crate::manual_chassis::speed_limits(mission, cockpit.handle) .unwrap_or([WALK_SPEED_FALLBACK; 2]) @@ -472,15 +472,18 @@ fn plan_player_steps( }; // shortcut: use the outdoor spawn clearance until the native walking collider is wired. let mut max_floor_clearance = FLOOR_CLEARANCE; - if !flying && input.movement != [0.0; 2] { + if !flying && walking_motion { if let Some(draft) = mission.object_drafts.get(cockpit.object_index) { max_floor_clearance = spawn_floor_clearance(&mission.terrain, draft.position)?; } } - let count = if resolve.is_some() && !flying && input.movement != [0.0; 2] { + let count = if resolve.is_some() && !flying && walking_motion { // shortcut: bound movement after a long stall while the scene uses the render clock. input.delta_seconds = input.delta_seconds.min(0.5); - let [across, along] = body_velocity(input.movement, speeds); + let [across, along] = walking.map_or_else( + || body_velocity(input.movement, speeds), + |drive| drive.limits, + ); (input.delta_seconds * across.hypot(along) / WALK_COLLISION_DISTANCE).ceil() as u32 } else { 1 @@ -491,25 +494,34 @@ fn plan_player_steps( let mut previous_surface = WalkingSurface::Ground; let mut pitch = pitch; for index in 0..count { - let mut propose = |input| { - let mut step = plan_motion(&mission.terrain, previous, pitch, flight, speeds, input)?; + let mut propose = |walk: Option<([f32; 2], [f32; 2])>| { + let ground_displacement = walk.map_or_else( + || body_velocity(input.movement, speeds).map(|speed| speed * input.delta_seconds), + |(_, distance)| distance, + ); + let mut step = plan_motion( + &mission.terrain, + previous, + pitch, + flight, + ground_displacement, + input, + )?; step.max_floor_clearance = max_floor_clearance; + step.hero_walk_velocity = walk.map(|(velocity, _)| velocity); match resolve.as_mut() { Some(resolve) => resolve(step), None => Ok((step, false)), } }; - let (mut step, mut blocked) = propose(input)?; - if !blocked { - let factor = step.slope_speed_factor(slope); - if factor < 1.0 { - // The existing hero step applies the speed immediately. Use the - // native speed envelope and resample the shortened step's support. - (step, blocked) = propose(PlayerMotion { - delta_seconds: input.delta_seconds * factor, - ..input - })?; - blocked |= factor == 0.0; + let advance = walking.map(|drive| drive.advance(input.movement, input.delta_seconds)); + let (mut step, mut blocked) = propose(advance)?; + if let (false, Some(drive), Some(normal), Some((velocity, _))) = + (blocked, walking, step.terrain_slope(), advance) + { + let yaw = f32::from_bits(step.pose.orientation[2]); + if let Some(braked) = drive.brake(velocity, normal, yaw, input.delta_seconds) { + (step, blocked) = propose(Some(braked))?; } } if blocked { @@ -517,6 +529,7 @@ fn plan_player_steps( step.sweep = None; step.surface = previous_surface; step.flight_velocity = step.flight_velocity.map(|_| [0.0; 3]); + step.hero_walk_velocity = step.hero_walk_velocity.map(|_| [0.0; 2]); return Ok(Some(step)); } crate::manual_chassis::support_step(chassis_ground, &mission.terrain, previous, &mut step)?; @@ -528,16 +541,24 @@ fn plan_player_steps( pitch = f32::from_bits(step.pitch_bits); previous = step.pose; previous_surface = step.surface; + if let (Some(drive), Some(velocity)) = (walking.as_mut(), step.hero_walk_velocity) { + drive.velocity = velocity; + } } unreachable!("a movement sample always has at least one step") } -pub(super) fn commit_flight( +pub(super) fn commit_velocity( mission: &mut crate::LoadedMissionState, cockpit: StartingCockpit, step: PlayerMotionStep, blocked: bool, ) { + if let Some(velocity) = step.hero_walk_velocity { + if mission.local_hero == Some(cockpit.handle) { + mission.hero_walk_velocity = if blocked { [0.0; 2] } else { velocity }; + } + } if let (Some(boarding), Some(velocity)) = (&mut mission.boarding, step.flight_velocity) { if boarding.cockpit.handle == cockpit.handle { boarding.flight_velocity = if blocked { [0.0; 3] } else { velocity }; @@ -559,7 +580,7 @@ fn plan_motion( mut pose: TransformState, pitch: f32, flight: Option<FlightDrive>, - speeds: [f32; 2], + ground_displacement: [f32; 2], input: PlayerMotion, ) -> Result<PlayerMotionStep, EngineError> { let from = pose.position.map(f32::from_bits); @@ -614,9 +635,7 @@ fn plan_motion( ]); } } else { - // shortcut: apply the requested ground velocity immediately. - let [across, along] = - body_velocity(input.movement, speeds).map(|speed| speed * input.delta_seconds); + let [across, along] = ground_displacement; let (sin, cos) = yaw.sin_cos(); let xy = [ from[0] + cos * across - sin * along, @@ -658,6 +677,7 @@ fn plan_motion( pose, pitch_bits: pitch.to_bits(), flight_velocity, + hero_walk_velocity: None, max_floor_clearance: FLOOR_CLEARANCE, previous_terrain_height, terrain_height, diff --git a/crates/fparkan-runtime/src/walking_slope.rs b/crates/fparkan-runtime/src/walking_slope.rs deleted file mode 100644 index a6604c51..00000000 --- a/crates/fparkan-runtime/src/walking_slope.rs +++ /dev/null @@ -1,45 +0,0 @@ -//! Ground speed envelope for the walking mode of Control. - -#[derive(Clone, Copy)] -pub(super) struct WalkingSlope { - minimum_up: f32, -} - -impl WalkingSlope { - pub(super) fn from_profile(words: &[u32; 27]) -> Option<Self> { - let angle = f32::from_bits(words[23]); - let minimum_up = angle.cos(); - (words[21] == 2 && angle > 0.0 && angle <= std::f32::consts::FRAC_PI_2 && minimum_up < 1.0) - .then_some(Self { minimum_up }) - } - - pub(super) fn speed_factor(self, normal: [f32; 3], direction: [f32; 2]) -> f32 { - // Control+0x153c0 applies the limit on an ascent and along the contour. - let along_normal = direction[0] * normal[0] + direction[1] * normal[1]; - if along_normal > 0.0 { - return 1.0; - } - (2.0 * (normal[2] - self.minimum_up) / (1.0 - self.minimum_up)).clamp(0.0, 1.0) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn hero_profile_limits_steep_ascent_and_contour() { - let mut words = [0; 27]; - words[21] = 2; - words[23] = 0.6_f32.to_bits(); - let slope = WalkingSlope::from_profile(&words).unwrap(); - let up_normal = |angle: f32| [0.0, -angle.sin(), angle.cos()]; - - assert_eq!(slope.speed_factor(up_normal(0.3), [0.0, 1.0]), 1.0); - let uphill = slope.speed_factor(up_normal(0.5), [0.0, 1.0]); - assert!((14.0 * uphill - 8.375_571).abs() < 0.000_01); - assert_eq!(slope.speed_factor(up_normal(0.7), [0.0, 1.0]), 0.0); - assert_eq!(slope.speed_factor(up_normal(0.7), [1.0, 0.0]), 0.0); - assert_eq!(slope.speed_factor(up_normal(0.7), [0.0, -1.0]), 1.0); - } -} |
