rocketsim 0.3.6

Simulate Rocket League games at maximum efficiency
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
use std::ops::{Deref, DerefMut};

use glam::{Mat3A, Vec3A};

use crate::{CarControls, PhysState, RaycastHitInfo, consts};

/// Core fields used to build or replace a car snapshot.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CarCore {
    pub phys: PhysState,
    /// Controls to simulate the car with.
    pub controls: CarControls,
    /// Whether the car jumped to get into the air.
    pub has_jumped: bool,
    /// Whether the car double jumped and is still in the air.
    pub has_double_jumped: bool,
    /// Whether the car has flipped in the air.
    pub has_flipped: bool,
    /// Relative torque direction of the flip. Forward flip has positive Y.
    pub flip_rel_torque: Vec3A,
    /// Integer tick count that drives `jump_time`.
    pub jump_ticks: u32,
    /// Seconds since flip activity last changed. Advances on ground.
    pub flip_time: f32,
    /// True during a flip, not an auto-flip or after a flip.
    pub is_flipping: bool,
    /// True during a jump.
    pub is_jumping: bool,
    /// True if jump hold was released during sustain.
    pub jump_hold_broken: bool,
    /// Time in air after jump ends. Zero if the car did not jump.
    pub air_time_since_jump: f32,
    /// Boost amount, normally from 0 to 100.
    pub boost: f32,
    /// True while auto-flip recovery plays.
    pub is_auto_flipping: bool,
    /// Seconds left in auto-flip recovery.
    pub auto_flip_timer: f32,
    /// Roll direction sign for auto-flip angular velocity.
    pub auto_flip_torque_scale: f32,
    /// Seconds until this car can bump the same victim again. Does not block demolition.
    pub bump_cooldown_timer: f32,
    /// Arena index of the last car that started the bump cooldown.
    pub bump_cooldown_victim: Option<usize>,
    /// Last arena tick when this car added a ball-hit impulse.
    pub last_extra_hit_tick: Option<u64>,
    /// True while demolished.
    pub is_demoed: bool,
    /// Seconds until respawn when demolished.
    pub demo_respawn_timer: f32,
}

impl CarCore {
    pub const DEFAULT: Self = Self {
        phys: PhysState {
            pos: Vec3A::new(0.0, 0.0, consts::car::spawn::REST_Z),
            rot_mat: Mat3A::IDENTITY,
            vel: Vec3A::ZERO,
            ang_vel: Vec3A::ZERO,
        },
        controls: CarControls::DEFAULT,
        has_jumped: false,
        has_double_jumped: false,
        has_flipped: false,
        flip_rel_torque: Vec3A::ZERO,
        jump_ticks: 0,
        flip_time: 0.0,
        is_flipping: false,
        is_jumping: false,
        jump_hold_broken: false,
        air_time_since_jump: 0.0,
        boost: consts::car::boost::SPAWN_AMOUNT,
        is_auto_flipping: false,
        auto_flip_timer: 0.0,
        auto_flip_torque_scale: 0.0,
        bump_cooldown_timer: 0.0,
        bump_cooldown_victim: None,
        last_extra_hit_tick: None,
        is_demoed: false,
        demo_respawn_timer: 0.0,
    };

    /// Clear jump and flip state for ground placement. Clamp boost to `0..100`.
    /// Calculate wheel contacts with the next tick or [`crate::Arena::refresh_car_contacts`].
    #[must_use]
    pub fn grounded(phys: PhysState, boost: f32) -> Self {
        Self {
            phys,
            boost: boost.clamp(0.0, consts::car::boost::MAX),
            has_jumped: false,
            has_double_jumped: false,
            has_flipped: false,
            is_jumping: false,
            is_flipping: false,
            ..Self::DEFAULT
        }
    }

    /// Create an airborne state with flip reset ready.
    #[must_use]
    pub fn aerial(phys: PhysState) -> Self {
        Self {
            phys,
            has_jumped: false,
            has_double_jumped: false,
            has_flipped: false,
            is_jumping: false,
            is_flipping: false,
            ..Self::DEFAULT
        }
    }

    /// Create an active jump state with the given elapsed tick count.
    #[must_use]
    pub fn jumping(phys: PhysState, ticks: u32) -> Self {
        Self {
            phys,
            has_jumped: true,
            is_jumping: true,
            jump_ticks: ticks,
            ..Self::DEFAULT
        }
    }

    /// Create an active flip state with the given relative torque.
    #[must_use]
    pub fn flipping(phys: PhysState, rel_torque: Vec3A) -> Self {
        Self {
            phys,
            has_jumped: true,
            has_flipped: true,
            is_flipping: true,
            flip_rel_torque: rel_torque,
            ..Self::DEFAULT
        }
    }

    #[must_use]
    pub fn with_controls(mut self, controls: CarControls) -> Self {
        self.controls = controls;
        self
    }

    #[must_use]
    pub fn with_boost(mut self, boost: f32) -> Self {
        self.boost = boost.clamp(0.0, consts::car::boost::MAX);
        self
    }

    /// Set linear velocity.
    #[must_use]
    pub fn with_vel(mut self, vel: Vec3A) -> Self {
        self.phys.vel = vel;
        self
    }

    /// Set position.
    #[must_use]
    pub fn with_pos(mut self, pos: Vec3A) -> Self {
        self.phys.pos = pos;
        self
    }

    /// Set rotation matrix.
    #[must_use]
    pub fn with_rot_mat(mut self, rot_mat: Mat3A) -> Self {
        self.phys.rot_mat = rot_mat;
        self
    }

    /// Set angular velocity.
    #[must_use]
    pub fn with_ang_vel(mut self, ang_vel: Vec3A) -> Self {
        self.phys.ang_vel = ang_vel;
        self
    }

    /// Check core invariants. This check is opt-in.
    pub fn debug_validate(&self) {
        debug_assert!(!self.is_jumping || self.has_jumped);
        debug_assert!(!self.is_flipping || self.has_flipped);
        debug_assert!((0.0..=consts::car::boost::MAX).contains(&self.boost));
    }
}

impl Default for CarCore {
    fn default() -> Self {
        Self::DEFAULT
    }
}

/// Tuning values used to seed or replace a car snapshot.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CarTuning {
    /// Smoothed handbrake value from 0 to 1.
    pub handbrake_val: f32,
    /// True above supersonic speed, with a grace period.
    pub is_supersonic: bool,
    /// Seconds since speed fell below start speed while still supersonic.
    pub supersonic_grace_timer: f32,
    /// Seconds since boost was last held.
    pub time_since_boosted: f32,
    /// Seconds spent boosting continuously.
    pub boosting_time: f32,
    /// True if the car boosted this tick.
    pub is_boosting: bool,
    /// Total time spent in the air.
    pub air_time: f32,
}

impl CarTuning {
    pub const DEFAULT: Self = Self {
        handbrake_val: 0.0,
        is_supersonic: false,
        supersonic_grace_timer: 0.0,
        time_since_boosted: 0.0,
        boosting_time: 0.0,
        is_boosting: false,
        air_time: 0.0,
    };

    /// Derive supersonic state from velocity.
    #[must_use]
    pub fn from_speed(vel: Vec3A) -> Self {
        Self {
            is_supersonic: vel.length_squared()
                >= consts::car::supersonic::START_SPEED * consts::car::supersonic::START_SPEED,
            ..Self::DEFAULT
        }
    }

    /// Copy tuning values from an existing snapshot without changing their bits.
    #[must_use]
    pub const fn preserve(state: &CarState) -> Self {
        state.tuning()
    }
}

impl Default for CarTuning {
    fn default() -> Self {
        Self::DEFAULT
    }
}

/// Collision, previous-control, and hidden per-tick data.
/// A full car snapshot retains these fields. Read contact data with the cache
/// getters.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub(crate) struct CarCache {
    pub is_on_ground: bool,
    pub wheels_with_contact: [Option<RaycastHitInfo>; 4],
    pub world_contact_normal: Option<Vec3A>,
    pub prev_controls: CarControls,
    /// Number of wheels with world contact on the previous tick.
    #[cfg_attr(feature = "serde", serde(default))]
    pub world_wheels_prev: u8,
    /// Tick on which another car demolished this car, or `u64::MAX`.
    #[cfg_attr(feature = "serde", serde(default = "default_demo_tick"))]
    pub demo_tick: u64,
    /// Arena index of the car that demolished this car on `demo_tick`.
    #[cfg_attr(feature = "serde", serde(default))]
    pub demoer_idx: Option<usize>,
}

#[cfg(feature = "serde")]
const fn default_demo_tick() -> u64 {
    u64::MAX
}

impl Default for CarCache {
    fn default() -> Self {
        Self {
            is_on_ground: true,
            wheels_with_contact: [None; 4],
            world_contact_normal: None,
            prev_controls: CarControls::DEFAULT,
            world_wheels_prev: 0,
            demo_tick: u64::MAX,
            demoer_idx: None,
        }
    }
}

/// Full mutable car snapshot.
///
/// This type has flat public fields for inspection and replay.
/// Use [`CarCore`] with [`crate::Arena::set_car_state`] for normal changes.
/// Use [`CarTuning`] to preserve replay seeds with the full-state setter.
/// Times are seconds. Ticks use `TICK_TIME` (`1/120` s).
/// `DEFAULT` spawns at rest with spawn boost.
///
/// Read flat fields and build safe replacement values:
///
/// ```
/// use rocketsim::{CarCore, CarState, CarTuning};
///
/// let snapshot = CarState::DEFAULT;
/// let position = snapshot.pos;
/// let core = snapshot.core();
/// let tuning = snapshot.tuning();
/// let replacement = CarState::from(CarCore::grounded(core.phys, core.boost)).with_tuning(tuning);
/// assert_eq!(position, replacement.pos);
/// ```
///
/// Cache fields are private. Read them through getters:
///
/// ```compile_fail
/// use rocketsim::CarState;
/// let state = CarState::DEFAULT;
/// let _ground = state.cache.is_on_ground;
/// ```
///
/// Cache fields cannot be set through a struct literal:
///
/// ```compile_fail
/// use rocketsim::CarState;
/// let _state = CarState { cache: todo!(), ..CarState::DEFAULT };
/// ```
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CarState {
    pub phys: PhysState,
    /// Controls to simulate the car with.
    pub controls: CarControls,
    /// Whether the car jumped to get into the air.
    pub has_jumped: bool,
    /// Whether the car double jumped and is still in the air.
    pub has_double_jumped: bool,
    /// Whether the car has flipped in the air.
    pub has_flipped: bool,
    /// Relative torque direction of the flip.
    pub flip_rel_torque: Vec3A,
    /// Tick count for jump time.
    pub jump_ticks: u32,
    /// Seconds since flip activity changed. Advances on ground.
    pub flip_time: f32,
    /// True during a flip, not an auto-flip or after a flip.
    pub is_flipping: bool,
    /// True during a jump.
    pub is_jumping: bool,
    /// True if jump hold was released during sustain.
    pub jump_hold_broken: bool,
    /// Total time spent in the air.
    pub air_time: f32,
    /// Time in air after the jump ends. Zero if no jump occurred.
    pub air_time_since_jump: f32,
    /// Boost amount, normally from 0 to 100.
    pub boost: f32,
    /// Smoothed handbrake value from 0 to 1.
    pub handbrake_val: f32,
    /// True above supersonic speed, with a grace period.
    pub is_supersonic: bool,
    /// Time in the supersonic grace period.
    pub supersonic_grace_timer: f32,
    /// Seconds since boost was last held.
    pub time_since_boosted: f32,
    /// Seconds spent boosting continuously.
    pub boosting_time: f32,
    /// True if the car boosted this tick.
    pub is_boosting: bool,
    /// True while auto-flip recovery plays.
    pub is_auto_flipping: bool,
    /// Seconds left in auto-flip recovery.
    pub auto_flip_timer: f32,
    /// Roll direction sign for auto-flip angular velocity.
    pub auto_flip_torque_scale: f32,
    /// Seconds until this car can bump the same victim again. Does not block demolition.
    pub bump_cooldown_timer: f32,
    /// Arena index of the last car that started the bump cooldown.
    pub bump_cooldown_victim: Option<usize>,
    /// Last arena tick when this car added a ball-hit impulse.
    pub last_extra_hit_tick: Option<u64>,
    /// True while demolished.
    pub is_demoed: bool,
    /// Seconds until respawn when demolished.
    pub demo_respawn_timer: f32,
    pub(crate) cache: CarCache,
}

impl Default for CarState {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl CarState {
    pub const DEFAULT: Self = Self {
        phys: CarCore::DEFAULT.phys,
        controls: CarCore::DEFAULT.controls,
        has_jumped: CarCore::DEFAULT.has_jumped,
        has_double_jumped: CarCore::DEFAULT.has_double_jumped,
        has_flipped: CarCore::DEFAULT.has_flipped,
        flip_rel_torque: CarCore::DEFAULT.flip_rel_torque,
        jump_ticks: CarCore::DEFAULT.jump_ticks,
        flip_time: CarCore::DEFAULT.flip_time,
        is_flipping: CarCore::DEFAULT.is_flipping,
        is_jumping: CarCore::DEFAULT.is_jumping,
        jump_hold_broken: CarCore::DEFAULT.jump_hold_broken,
        air_time: CarTuning::DEFAULT.air_time,
        air_time_since_jump: CarCore::DEFAULT.air_time_since_jump,
        boost: CarCore::DEFAULT.boost,
        handbrake_val: CarTuning::DEFAULT.handbrake_val,
        is_supersonic: CarTuning::DEFAULT.is_supersonic,
        supersonic_grace_timer: CarTuning::DEFAULT.supersonic_grace_timer,
        time_since_boosted: CarTuning::DEFAULT.time_since_boosted,
        boosting_time: CarTuning::DEFAULT.boosting_time,
        is_boosting: CarTuning::DEFAULT.is_boosting,
        is_auto_flipping: CarCore::DEFAULT.is_auto_flipping,
        auto_flip_timer: CarCore::DEFAULT.auto_flip_timer,
        auto_flip_torque_scale: CarCore::DEFAULT.auto_flip_torque_scale,
        bump_cooldown_timer: CarCore::DEFAULT.bump_cooldown_timer,
        bump_cooldown_victim: CarCore::DEFAULT.bump_cooldown_victim,
        last_extra_hit_tick: CarCore::DEFAULT.last_extra_hit_tick,
        is_demoed: CarCore::DEFAULT.is_demoed,
        demo_respawn_timer: CarCore::DEFAULT.demo_respawn_timer,
        cache: CarCache {
            is_on_ground: true,
            wheels_with_contact: [None; 4],
            world_contact_normal: None,
            prev_controls: CarControls::DEFAULT,
            world_wheels_prev: 0,
            demo_tick: u64::MAX,
            demoer_idx: None,
        },
    };

    /// Project the core fields without normalization.
    #[must_use]
    pub const fn core(&self) -> CarCore {
        CarCore {
            phys: self.phys,
            controls: self.controls,
            has_jumped: self.has_jumped,
            has_double_jumped: self.has_double_jumped,
            has_flipped: self.has_flipped,
            flip_rel_torque: self.flip_rel_torque,
            jump_ticks: self.jump_ticks,
            flip_time: self.flip_time,
            is_flipping: self.is_flipping,
            is_jumping: self.is_jumping,
            jump_hold_broken: self.jump_hold_broken,
            air_time_since_jump: self.air_time_since_jump,
            boost: self.boost,
            is_auto_flipping: self.is_auto_flipping,
            auto_flip_timer: self.auto_flip_timer,
            auto_flip_torque_scale: self.auto_flip_torque_scale,
            bump_cooldown_timer: self.bump_cooldown_timer,
            bump_cooldown_victim: self.bump_cooldown_victim,
            last_extra_hit_tick: self.last_extra_hit_tick,
            is_demoed: self.is_demoed,
            demo_respawn_timer: self.demo_respawn_timer,
        }
    }

    /// Project tuning fields without normalization.
    #[must_use]
    pub const fn tuning(&self) -> CarTuning {
        CarTuning {
            handbrake_val: self.handbrake_val,
            is_supersonic: self.is_supersonic,
            supersonic_grace_timer: self.supersonic_grace_timer,
            time_since_boosted: self.time_since_boosted,
            boosting_time: self.boosting_time,
            is_boosting: self.is_boosting,
            air_time: self.air_time,
        }
    }

    /// Apply a replay tuning seed exactly. This does not normalize values.
    #[must_use]
    pub fn with_tuning(mut self, tuning: CarTuning) -> Self {
        self.handbrake_val = tuning.handbrake_val;
        self.is_supersonic = tuning.is_supersonic;
        self.supersonic_grace_timer = tuning.supersonic_grace_timer;
        self.time_since_boosted = tuning.time_since_boosted;
        self.boosting_time = tuning.boosting_time;
        self.is_boosting = tuning.is_boosting;
        self.air_time = tuning.air_time;
        self
    }

    /// True on ground or airborne with a flip or double-jump available next tick.
    #[must_use]
    pub const fn has_flip_or_jump(&self) -> bool {
        self.cache.is_on_ground
            || (!self.has_flipped
                && !self.has_double_jumped
                && self.air_time_since_jump + consts::TICK_TIME
                    < consts::car::jump::DOUBLEJUMP_MAX_DELAY)
    }

    /// True when a wheel contact gave a fresh flip reset.
    #[must_use]
    pub const fn has_flip_reset(&self) -> bool {
        !self.cache.is_on_ground && self.has_flip_or_jump() && !self.has_jumped
    }

    /// True when airborne without having jumped.
    #[must_use]
    pub const fn got_flip_reset(&self) -> bool {
        !self.cache.is_on_ground && !self.has_jumped
    }

    /// Return the number of wheels in contact.
    #[must_use]
    pub fn num_wheels_in_contact(&self) -> usize {
        self.cache
            .wheels_with_contact
            .iter()
            .filter(|w| w.is_some())
            .count()
    }

    /// Return elapsed jump-hold time in seconds.
    #[must_use]
    pub const fn jump_time(&self) -> f32 {
        self.jump_ticks as f32 * consts::TICK_TIME
    }

    /// Grounded flag from simulation, a contact query, or imported observations.
    /// Imported ground flags can disagree with the wheel count. New states need a tick or
    /// [`crate::Arena::refresh_car_contacts`] to query simulated geometry.
    #[must_use]
    pub const fn is_on_ground(&self) -> bool {
        self.cache.is_on_ground
    }

    /// Return contact information for each wheel. The first two are front wheels.
    /// Imported contact flags use placeholder hit fields, not measured raycast geometry.
    #[must_use]
    pub const fn wheels_with_contact(&self) -> &[Option<RaycastHitInfo>; 4] {
        &self.cache.wheels_with_contact
    }

    /// Return the combined world-contact normal, if present.
    ///
    /// Each world object's contact normal sum is normalized. The sum of those
    /// object normals is normalized again. An unusable total keeps the last
    /// contact normal. A tick with no world contact returns `None`.
    #[must_use]
    pub const fn world_contact_normal(&self) -> Option<Vec3A> {
        self.cache.world_contact_normal
    }

    /// Return controls from the last simulation step.
    #[must_use]
    pub const fn prev_controls(&self) -> &CarControls {
        &self.cache.prev_controls
    }

    /// Import coarse contact observations without querying the world.
    ///
    /// Each true wheel flag gets placeholder hit fields. These values are not measured
    /// raycast geometry. Ground state is set independently of the wheel flags.
    /// Keeps the world-contact normal, previous controls, and all other state.
    /// `Arena::set_car_state(core)` clears these observations. The full-state setter and
    /// full-state editor preserve them.
    pub fn set_contact_flags(&mut self, is_on_ground: bool, wheel_contacts: [bool; 4]) {
        self.cache.is_on_ground = is_on_ground;
        self.cache.wheels_with_contact = wheel_contacts.map(|has_contact| {
            has_contact.then_some(RaycastHitInfo {
                hit_point: Vec3A::ZERO,
                hit_normal: Vec3A::Z,
                hit_fraction: 0.0,
                user_info: crate::UserInfoType::None,
            })
        });
    }

    /// Restore input history for full-state replay.
    /// Normal `set_car_state(core)` sets previous controls to current controls.
    /// Use this inside an `edit_car_state_full` closure when replay needs prior inputs.
    pub const fn set_prev_controls(&mut self, controls: CarControls) {
        self.cache.prev_controls = controls;
    }

    /// Check invariants for a state seed. This check is opt-in.
    ///
    /// Do not call this after every simulation tick. Live state can contain
    /// phase transitions, and `flip_time` can advance while grounded.
    pub fn debug_validate(&self) {
        self.core().debug_validate();
        debug_assert!((0.0..=1.0).contains(&self.handbrake_val));
        debug_assert!(self.is_supersonic || self.supersonic_grace_timer == 0.0);
        if self.cache.is_on_ground {
            debug_assert!(!self.has_double_jumped && !self.has_flipped && !self.is_flipping);
            debug_assert_eq!(self.air_time, 0.0);
            debug_assert_eq!(self.flip_time, 0.0);
        }
    }
}

impl From<CarCore> for CarState {
    fn from(mut core: CarCore) -> Self {
        core.boost = core.boost.clamp(0.0, consts::car::boost::MAX);
        if core.is_jumping {
            core.has_jumped = true;
        }

        if core.is_flipping {
            core.has_flipped = true;
        }

        let tuning = CarTuning::from_speed(core.phys.vel);
        Self {
            phys: core.phys,
            controls: core.controls,
            has_jumped: core.has_jumped,
            has_double_jumped: core.has_double_jumped,
            has_flipped: core.has_flipped,
            flip_rel_torque: core.flip_rel_torque,
            jump_ticks: core.jump_ticks,
            flip_time: core.flip_time,
            is_flipping: core.is_flipping,
            is_jumping: core.is_jumping,
            jump_hold_broken: core.jump_hold_broken,
            air_time: tuning.air_time,
            air_time_since_jump: core.air_time_since_jump,
            boost: core.boost,
            handbrake_val: tuning.handbrake_val,
            is_supersonic: tuning.is_supersonic,
            supersonic_grace_timer: tuning.supersonic_grace_timer,
            time_since_boosted: tuning.time_since_boosted,
            boosting_time: tuning.boosting_time,
            is_boosting: tuning.is_boosting,
            is_auto_flipping: core.is_auto_flipping,
            auto_flip_timer: core.auto_flip_timer,
            auto_flip_torque_scale: core.auto_flip_torque_scale,
            bump_cooldown_timer: core.bump_cooldown_timer,
            bump_cooldown_victim: core.bump_cooldown_victim,
            last_extra_hit_tick: core.last_extra_hit_tick,
            is_demoed: core.is_demoed,
            demo_respawn_timer: core.demo_respawn_timer,
            cache: CarCache {
                is_on_ground: false,
                wheels_with_contact: [None; 4],
                world_contact_normal: None,
                prev_controls: core.controls,
                world_wheels_prev: 0,
                demo_tick: u64::MAX,
                demoer_idx: None,
            },
        }
    }
}

impl Deref for CarCore {
    type Target = PhysState;
    fn deref(&self) -> &Self::Target {
        &self.phys
    }
}
impl DerefMut for CarCore {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.phys
    }
}

impl Deref for CarState {
    type Target = PhysState;
    fn deref(&self) -> &Self::Target {
        &self.phys
    }
}
impl DerefMut for CarState {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.phys
    }
}