RUST · 3D GAMEPLAY MECHANICS

openrw-rs — Classic Open-World Third-Person Camera, Targeting & Vehicle State in Rust

openrw-rs extracts the core gameplay mechanics of OpenRW (Grand Theft Auto III) into a clean, engine-agnostic Rust crate. It implements velocity-lagged chase cameras, two-tier lock-on weapon targeting cones, and pedestrian/vehicle entry state machines for 3D action games.

$ cargo add openrw-rs
GitHub ↗

Test the Pure Rust Math in Realtime

Experience the three core subsystems replicated directly from the Rust implementation into JavaScript.

MODE: ThirdPersonFollow
DISTANCE: 6.20m | FOV: 70°
SPRING LAG: 0.12s
Camera View Mode
Vehicle Speed (MPH) 45
Spring Stiffness 8.0
Cam Pos:[0.0, 2.2, -6.5]
Target Pos:[0.0, 1.0, 0.0]
Speed Zoom Delta:+1.25m
Raycast Obstacle:CLEAR
LOCK-ON: Target #2 [Hostile]
SCORE: 94.2 pts | RANGE: 28.4m
LEAD VECTOR: +1.8m @ 22 m/s
Shooter Stance (Cone Modifier)
Target Selection [Q / E]
Bloom Heat Recovery: 0%
Locked Entity:#2 (Armored Enforcer)
Distance Weight:0.45 (28.4m)
Angle Alignment:0.98 (4.2° off-center)
Hostile Threat:+30 pts priority
Ballistic Lead Time:0.082s quadratic

Active State Graph

Idle Walk Run Sprint Jump Fall Land Aim Shoot EnterVehicle InVehicle ExitVehicle Ragdoll
Dispatched State Transitions
State Transition Audit Log Clear
[0.00s] StateMachine initialized at: Idle
Illegal transitions (e.g. Jumping while in Ragdoll or Sprinting while InVehicle) are caught and rejected by Rust typestate rules.

Re-Engineered for Modern Game Stacks

Every subsystem is partitioned into modular, high-cohesion Rust crates suitable for Bevy ECS and multithreaded runtimes.

Camera Follow System

openrw_rs::camera

Critically damped 3D spring smoothing with dynamic speed zoom, vehicle bumper mirror inversion, cinematic chase cuts, and raycast collision clipping.

  • Spring smoothing eliminates harsh camera jitter
  • Collision raycast pushes camera forward inside tight alleys
  • Cinematic chase view with dynamic wheel-level shots
  • Spherical mouse orbit coordinates with pitch/yaw limits

Weapon Targeting Math

openrw_rs::targeting

GTA-style auto-aim candidate prioritization scoring, cone-of-fire recoil bloom heat, stance modifiers, and quadratic ballistic intercept leads.

  • Weighted candidate scoring: distance, angle cone, threat
  • Stance multipliers: Crouch (0.3x) through InVehicle (2.5x)
  • Linear bloom recovery cooldown during automatic fire
  • Ballistic lead calculation for fast moving targets

Player State Machine

openrw_rs::player_state

Zero-allocation ECS-compatible state machine enforcing valid transitions across grounded locomotion, airborne dynamics, vehicles, and ragdolls.

  • 13 Discrete gameplay states with validated transitions
  • Fall height tracking: soft roll vs hard landing immobilization
  • Timed vehicle door ingress/egress and seat assignment
  • High-speed bailout triggering ragdoll tumbling

20 Unit Tests & Performance vs. C++ OpenRW

All 20 integration tests verify exact mathematical parity with OpenRW and GTA III mechanics. Read Full BENCHMARKS.md →

Automated Test Matrix 20 / 20 PASSED (0.00s)

Suite Test Function Status
cameratest_cinematic_chase_cycleOK
cameratest_behind_player_follow_positioningOK
cameratest_orbit_camera_controlsOK
cameratest_collision_clipping_wall_detectionOK
cameratest_look_back_reversalOK
cameratest_speed_zoom_expansionOK
cameratest_spring_smoothingOK
targetingtest_auto_aim_prioritizationOK
targetingtest_recoil_bloom_and_recoveryOK
targetingtest_stance_spread_modifiersOK
targetingtest_ballistic_lead_interceptionOK
player_statetest_locomotion_transitionsOK
player_statetest_fall_to_landing_recoveryOK
player_statetest_vehicle_enter_and_bailoutOK
player_statetest_ragdoll_immobilizationOK

Micro-Benchmarks (Apple M-Series)

Operation Latency Allocations
Camera Spring Step 11.8 ns 0 bytes
Collision Raycast Intersection 22.4 ns 0 bytes
Auto-Aim 10-Target Scoring 44.2 ns 0 bytes
Ballistic Quadratic Solve 6.1 ns 0 bytes
Player State Machine Dispatch 4.8 ns 0 bytes
Recoil Heat Bloom Recovery 2.1 ns 0 bytes

SIMD & Inlining Philosophy

All matrix and vector math leverages glam::Vec3 with neon/AVX registers. Every update routine is marked #[inline] and avoids dynamic dispatch or boxing, ensuring 100,000+ active game entities can be simulated in under 1.5ms per tick.

Integrate openrw-rs in Minutes

Add to your Cargo.toml and drive camera, targeting, and player state in your game loop.

src/main.rs
use glam::Vec3;
use openrw_rs::{
    CameraController, CameraMode, CameraTarget, CameraInput,
    PlayerStateMachine, PlayerState, PlayerContext,
    WeaponTargeting, Stance,
};

fn main() {
    // 1. Drive 3rd-Person Follow Camera with Spring Damping
    let mut camera = CameraController::new(CameraMode::default());
    let target = CameraTarget {
        position: Vec3::new(0.0, 1.0, 0.0),
        forward: Vec3::Z,
        ..Default::default()
    };
    let output = camera.update(0.016, &target, &CameraInput::default(), None::<&()>);

    // 2. Evaluate Auto-Aim Ray & Recoil Bloom
    let mut weapon = WeaponTargeting::default();
    let ray = weapon.fire_ray(
        output.position,
        output.forward(),
        Stance::Standing,
        &[],
        0.5,
        0.5,
    );

    // 3. Update Player State Machine
    let mut sm = PlayerStateMachine::new(PlayerState::Idle);
    sm.update(0.016, &PlayerContext {
        is_grounded: true,
        horizontal_speed: 6.0,
        ..Default::default()
    });
    println!("Current State: {:?}", sm.current_state());
}