aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-runtime
diff options
context:
space:
mode:
Diffstat (limited to 'crates/fparkan-runtime')
-rw-r--r--crates/fparkan-runtime/src/lib.rs16
-rw-r--r--crates/fparkan-runtime/src/player_motion.rs51
2 files changed, 53 insertions, 14 deletions
diff --git a/crates/fparkan-runtime/src/lib.rs b/crates/fparkan-runtime/src/lib.rs
index 23e18cae..f162b074 100644
--- a/crates/fparkan-runtime/src/lib.rs
+++ b/crates/fparkan-runtime/src/lib.rs
@@ -18011,7 +18011,19 @@ 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);
- assert!(((x1 - x0).hypot(y1 - y0) - 0.8).abs() < 1e-4);
+ 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],
+ );
+ assert!((runtime_speed - 14.0).abs() < 0.01);
+ assert!(((x1 - x0).hypot(y1 - y0) - runtime_speed * 0.1).abs() < 1e-4);
let ground = |xy| {
loaded_terrain(&engine)
.unwrap()
@@ -18034,7 +18046,7 @@ mod tests {
pose(&frame_with_player_motion(&mut engine, 400, cockpit, slow_frame, false).unwrap())
.position
.map(f32::from_bits);
- assert!(((x2 - x1).hypot(y2 - y1) - 1.6).abs() < 1e-4);
+ assert!(((x2 - x1).hypot(y2 - y1) - runtime_speed * 0.2).abs() < 1e-4);
set_transform(&mut engine.world, cockpit.handle, walked).unwrap();
let mut split = walked;
for clock_ms in [500, 600] {
diff --git a/crates/fparkan-runtime/src/player_motion.rs b/crates/fparkan-runtime/src/player_motion.rs
index b21efd5b..0b5ef029 100644
--- a/crates/fparkan-runtime/src/player_motion.rs
+++ b/crates/fparkan-runtime/src/player_motion.rs
@@ -28,7 +28,9 @@ const STEP_DOWN: f32 = 2.0;
const FLOOR_CLEARANCE: f32 = 0.02;
const WATER_SURFACE: u32 = 0x0002_0000;
// shortcut: sample the temporary floor probe at most 0.16 units apart until native collision is wired.
-const WALK_COLLISION_INTERVAL: f32 = 0.02;
+const WALK_COLLISION_DISTANCE: f32 = 0.16;
+const WALK_SPEED_FALLBACK: f32 = 8.0;
+const FLIGHT_SPEED_FALLBACK: f32 = 40.0;
/// Player controls sampled once for a simulation step.
#[derive(Clone, Copy, Debug, Default)]
@@ -396,6 +398,21 @@ fn plan_player_steps(
if !input.delta_seconds.is_finite() || input.delta_seconds < 0.0 {
return Err(EngineError::Movement("invalid player step".into()));
}
+ let speeds = if flying {
+ [FLIGHT_SPEED_FALLBACK; 2]
+ } else if mission.local_hero == Some(cockpit.handle) {
+ let forward = mission
+ .control_device_tables
+ .iter()
+ .find(|table| table.handle == cockpit.handle)
+ .and_then(|table| table.owner_profile.as_ref())
+ .map(|profile| f32::from_bits(profile.current_words[7]))
+ .filter(|speed| speed.is_finite() && *speed >= 0.0)
+ .unwrap_or(WALK_SPEED_FALLBACK);
+ [WALK_SPEED_FALLBACK, forward]
+ } else {
+ [WALK_SPEED_FALLBACK; 2]
+ };
// 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] {
@@ -411,20 +428,21 @@ fn plan_player_steps(
}
}
let Some(resolve) = resolve else {
- let mut step = plan_motion(&mission.terrain, pose, pitch, flying, input)?;
+ let mut step = plan_motion(&mission.terrain, pose, pitch, flying, speeds, input)?;
step.max_floor_clearance = max_floor_clearance;
return Ok(Some(step));
};
let count = if !flying && input.movement != [0.0; 2] {
// shortcut: bound movement after a long stall while the scene uses the render clock.
input.delta_seconds = input.delta_seconds.min(0.5);
- (input.delta_seconds / WALK_COLLISION_INTERVAL).ceil() as u32
+ let [across, along] = body_velocity(input.movement, speeds);
+ (input.delta_seconds * across.hypot(along) / WALK_COLLISION_DISTANCE).ceil() as u32
} else {
1
}
.max(1);
input.delta_seconds /= count as f32;
- let mut step = plan_motion(&mission.terrain, pose, pitch, flying, input)?;
+ let mut step = plan_motion(&mission.terrain, pose, pitch, flying, speeds, input)?;
let mut previous = pose;
let mut previous_surface = WalkingSurface::Ground;
input.mouse_x = 0.0;
@@ -436,6 +454,7 @@ fn plan_player_steps(
previous,
f32::from_bits(step.pitch_bits),
flying,
+ speeds,
input,
)?;
}
@@ -457,11 +476,21 @@ fn plan_player_steps(
Ok(Some(step))
}
+fn body_velocity(movement: [f32; 2], [side_speed, forward_speed]: [f32; 2]) -> [f32; 2] {
+ let [right, forward] = movement.map(|axis| axis.clamp(-1.0, 1.0));
+ let length = right.hypot(forward).max(1.0);
+ [
+ right * side_speed / length,
+ forward * forward_speed / length,
+ ]
+}
+
fn plan_motion(
terrain: &TerrainWorld,
mut pose: TransformState,
pitch: f32,
flying: bool,
+ speeds: [f32; 2],
input: PlayerMotion,
) -> Result<PlayerMotionStep, EngineError> {
let from = pose.position.map(f32::from_bits);
@@ -483,8 +512,7 @@ fn plan_motion(
{
return Err(EngineError::Movement("invalid player step".into()));
}
- // shortcut: use constant walk speed and mouse gain until native Control
- // acceleration and mouse sensitivity are measured.
+ // shortcut: use constant mouse gain until native sensitivity is measured.
let yaw = if input.mouse_x != 0.0 {
let yaw = (yaw - input.mouse_x.clamp(-1_000.0, 1_000.0) * 0.003)
.rem_euclid(std::f32::consts::TAU);
@@ -493,14 +521,13 @@ fn plan_motion(
} else {
yaw
};
- let [right, forward] = input.movement.map(|axis| axis.clamp(-1.0, 1.0));
- // shortcut: constant flight rates until thrust, clearance and inertia are measured.
- let speed = if flying { 40.0 } else { 8.0 };
- let distance = speed * input.delta_seconds / right.hypot(forward).max(1.0);
+ // shortcut: apply the requested velocity immediately until native acceleration is known.
+ let [across, along] =
+ body_velocity(input.movement, speeds).map(|speed| speed * input.delta_seconds);
let (sin, cos) = yaw.sin_cos();
let xy = [
- from[0] + (cos * right - sin * forward) * distance,
- from[1] + (sin * right + cos * forward) * distance,
+ from[0] + cos * across - sin * along,
+ from[1] + sin * across + cos * along,
];
let climb = if flying {
input.climb.clamp(-1.0, 1.0) * (16.0 * input.delta_seconds)