rl_ball_sym 5.0.0

rl_ball_sym is a Rust implementation of Rocket League's ball physics
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
//! Tools for simulation a Rocket League ball.

use super::{
    game::{Constraints, Game},
    geometry::Sphere,
};
use glam::Vec3A;

#[cfg(feature = "heatseeker")]
use super::geometry::Angle;

#[cfg(feature = "heatseeker")]
mod heatseeker {
    use std::f32::consts::PI;

    pub const INITIAL_TARGET_SPEED: f32 = 2900.;
    pub const TARGET_SPEED_INCREMENT: f32 = 85.;
    pub const TARGET_Y: f32 = 5120.;
    pub const TARGET_Z: f32 = 320.;
    pub const HORIZONTAL_BLEND: f32 = 1.45;
    pub const VERTICAL_BLEND: f32 = 0.78;
    pub const SPEED_BLEND: f32 = 0.3;
    pub const MAX_TURN_PITCH: f32 = 7000. * PI / (1u16 << 15) as f32;
    pub const MAX_SPEED: f32 = 4600.;
    pub const WALL_BOUNCE_CHANGE_Y_THRESH: f32 = 300.;
    pub const WALL_BOUNCE_CHANGE_NORMAL_Y: f32 = 0.5;
    pub const WALL_BOUNCE_FORCE_SCALE: f32 = 1. / 3.;
    pub const WALL_BOUNCE_UP_FRAC: f32 = 0.3;
}

/// Represents the game's ball
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ball {
    /// Game time of the ball
    pub time: f32,
    /// Position of the ball
    pub location: Vec3A,
    /// Linear velocity of the ball
    pub velocity: Vec3A,
    /// Rotational velocity of the ball
    pub angular_velocity: Vec3A,
    /// Size of the ball
    radius: f32,
    /// 1 / Moment of inertia of the ball in the form of a diagonal matrix
    pub(crate) inv_inertia: f32,
    #[cfg(feature = "heatseeker")]
    pub(crate) y_target_dir: f32,
}

impl Default for Ball {
    #[inline]
    fn default() -> Self {
        Self::DEFAULT_STANDARD
    }
}

/// Collection of Balls representing future predictions based on field geometry
pub type Predictions = Vec<Ball>;

impl Ball {
    const DRAG: f32 = 0.03;

    const V_MAX: f32 = 6000.;
    const W_MAX: f32 = 6.;

    const M: f32 = 30.;
    pub(crate) const INV_M: f32 = 1. / Self::M;

    const STANDARD_RADIUS: f32 = 91.25;
    const HOOPS_RADIUS: f32 = 96.3831;
    const DROPSHOT_RADIUS: f32 = 100.2565;

    const SIMULATION_DT: f32 = 1. / 120.;
    const STANDARD_NUM_SLICES: usize = 720;

    const DEFAULT: Self = Self {
        time: 0.,
        location: Vec3A::Z,
        velocity: Vec3A::ZERO,
        angular_velocity: Vec3A::ZERO,
        radius: 1.,
        inv_inertia: 1. / (0.4 * Self::M),
        #[cfg(feature = "heatseeker")]
        y_target_dir: 0.,
    };

    /// Calculates the default ball height based on the collision radius (arbitrary)
    const fn default_height(radius: f32) -> Vec3A {
        Vec3A::new(0., 0., 1.1 * radius)
    }

    /// Calculates the inverse inertia of the ball based on the collision radius
    const fn get_inv_inertia(radius: f32) -> f32 {
        1. / (0.4 * Self::M * radius * radius)
    }

    /// A ball with default values for the standard game mode
    pub const DEFAULT_STANDARD: Self = Self {
        radius: Self::STANDARD_RADIUS,
        inv_inertia: Self::get_inv_inertia(Self::STANDARD_RADIUS),
        location: Self::default_height(Self::STANDARD_RADIUS),
        ..Self::DEFAULT
    };

    /// A ball with default values for the heatseeker game mode
    #[cfg(feature = "heatseeker")]
    pub const DEFAULT_HEATSEEKER: Self = Self {
        radius: Self::STANDARD_RADIUS,
        inv_inertia: Self::get_inv_inertia(Self::STANDARD_RADIUS),
        location: Vec3A::new(-1000., -2220., 92.75),
        velocity: Vec3A::new(0., -65., 650.),
        ..Self::DEFAULT
    };

    /// A ball with default values for the hoops game mode
    pub const DEFAULT_HOOPS: Self = Self {
        radius: Self::HOOPS_RADIUS,
        inv_inertia: Self::get_inv_inertia(Self::HOOPS_RADIUS),
        location: Self::default_height(Self::HOOPS_RADIUS),
        ..Self::DEFAULT
    };

    /// A ball with default values for the dropshot game mode
    pub const DEFAULT_DROPSHOT: Self = Self {
        radius: Self::DROPSHOT_RADIUS,
        inv_inertia: Self::get_inv_inertia(Self::DROPSHOT_RADIUS),
        location: Self::default_height(Self::DROPSHOT_RADIUS),
        ..Self::DEFAULT
    };

    /// Set a custom radius for the ball
    pub const fn set_radius(&mut self, radius: f32) {
        debug_assert!(radius > 0.);
        self.radius = radius;
        self.inv_inertia = Self::get_inv_inertia(radius);
    }

    #[inline]
    #[must_use]
    /// Get the radius of the ball
    pub const fn radius(&self) -> f32 {
        self.radius
    }

    /// Updates the ball with everything that changes from game tick to game tick
    pub const fn update(
        &mut self,
        time: f32,
        location: Vec3A,
        velocity: Vec3A,
        angular_velocity: Vec3A,
    ) {
        self.time = time;
        self.location = location;
        self.velocity = velocity;
        self.angular_velocity = angular_velocity;
    }

    #[inline]
    const fn hitbox(&self) -> Sphere {
        Sphere::new(self.location, self.radius)
    }

    #[inline]
    pub(crate) fn get_velocity_in_local_point(&self, rel_pos: Vec3A) -> Vec3A {
        self.velocity + self.angular_velocity.cross(rel_pos)
    }

    fn limit_velocities(&mut self) {
        self.angular_velocity = self.angular_velocity.clamp_length_max(Self::W_MAX);
        self.velocity = self.velocity.clamp_length_max(Self::V_MAX);
    }

    /// Simulate the ball for one game tick,
    /// returning whether the ball has made contact with the world
    fn internal_step(&mut self, game: &Game, dt: f32) -> Option<Vec3A> {
        self.velocity *= (1. - Self::DRAG).powf(dt);
        let external_force_impulse = game.gravity * dt;

        let contacts = game.triangle_collisions.collide(self.hitbox());

        let world_contact_normal = if contacts.is_empty() {
            None
        } else {
            let mut constraints = Constraints::new(Self::INV_M, external_force_impulse);
            constraints.add_contacts(contacts, self, dt);

            let (delta_velocity, push_velocity, normal) = constraints.solve(self);

            self.velocity += delta_velocity.linear;
            self.angular_velocity += delta_velocity.angular;
            self.location += push_velocity * dt;

            Some(normal)
        };

        self.velocity += external_force_impulse;
        self.location += self.velocity * dt;

        world_contact_normal
    }

    /// Set the target direction for the ball in Heatseeker mode
    ///
    /// `blue_goal` - Whether the ball should target the blue goal
    #[cfg(feature = "heatseeker")]
    pub const fn set_heatseeker_target(&mut self, blue_goal: bool) {
        self.y_target_dir = if blue_goal { -1. } else { 1. };
    }

    /// Reset the target direction for the ball in Heatseeker mode
    ///
    /// This will make the ball behave normally and not target any goal
    #[cfg(feature = "heatseeker")]
    pub const fn reset_heatseeker_target(&mut self) {
        self.y_target_dir = 0.;
    }

    /// Get the target position for the ball in Heatseeker mode
    #[inline]
    #[must_use]
    #[cfg(feature = "heatseeker")]
    pub const fn get_heatseeker_target(&self) -> Vec3A {
        Vec3A::new(
            0.,
            heatseeker::TARGET_Y * self.y_target_dir,
            heatseeker::TARGET_Z,
        )
    }

    /// Simulate the ball for one game tick in Heatseeker mode
    ///
    /// `dt` - The delta time (game tick length)
    #[cfg(feature = "heatseeker")]
    pub fn step_heatseeker(&mut self, game: &Game, dt: f32) {
        self.time += dt;

        if self.velocity.length_squared() != 0. || self.angular_velocity.length_squared() != 0. {
            if self.y_target_dir != 0. {
                let vel = self.velocity.length();
                let vel_norm = self.velocity / vel;

                let vel_angle = Angle::from_vec(vel_norm);
                let angle_to_goal =
                    Angle::from_vec((self.get_heatseeker_target() - self.location).normalize());
                let delta_angle = angle_to_goal - vel_angle;

                // Determine speed ratio
                let speed_ratio = vel / heatseeker::MAX_SPEED;

                // Interpolate delta
                let base_interp_factor = speed_ratio * dt;
                let mut new_angle = vel_angle;
                new_angle.yaw +=
                    delta_angle.yaw * base_interp_factor * heatseeker::HORIZONTAL_BLEND;
                new_angle.pitch +=
                    delta_angle.pitch * base_interp_factor * heatseeker::VERTICAL_BLEND;
                new_angle.normalize_fix();

                // Limit pitch
                new_angle.pitch = new_angle
                    .pitch
                    .clamp(-heatseeker::MAX_TURN_PITCH, heatseeker::MAX_TURN_PITCH);

                new_angle = new_angle.round();

                // Determine new interpolated speed
                let current_state = ((vel - heatseeker::INITIAL_TARGET_SPEED)
                    / heatseeker::TARGET_SPEED_INCREMENT)
                    .floor()
                    .clamp(0., 20.);
                let target_speed = current_state * heatseeker::TARGET_SPEED_INCREMENT
                    + heatseeker::INITIAL_TARGET_SPEED;
                let new_speed = vel + ((target_speed - vel) * heatseeker::SPEED_BLEND);

                // Update velocity
                self.velocity = new_angle.get_forward_vec() * new_speed;
            }

            if let Some(normal) = self.internal_step(game, dt) {
                if self.y_target_dir != 0. {
                    let rel_normal_y = normal.y * self.y_target_dir;
                    let rel_y = self.location.y * self.y_target_dir;

                    if rel_y >= 5120. - heatseeker::WALL_BOUNCE_CHANGE_Y_THRESH
                        && rel_normal_y <= -heatseeker::WALL_BOUNCE_CHANGE_NORMAL_Y
                    {
                        self.y_target_dir *= -1.;

                        let dir_to_goal =
                            (self.get_heatseeker_target() - self.location).normalize();

                        let bounce_dir = dir_to_goal * (1. - heatseeker::WALL_BOUNCE_UP_FRAC)
                            + Vec3A::Z * heatseeker::WALL_BOUNCE_UP_FRAC;
                        let bounce_impulse = bounce_dir
                            * self.velocity.length()
                            * heatseeker::WALL_BOUNCE_FORCE_SCALE;
                        self.velocity += bounce_impulse;
                    }
                }
            }

            self.limit_velocities();
        }
    }

    /// Simulate the ball for one game tick
    ///
    /// `dt` - The delta time (game tick length)
    pub fn step(&mut self, game: &Game, dt: f32) {
        self.time += dt;

        if self.velocity.length_squared() != 0. || self.angular_velocity.length_squared() != 0. {
            self.internal_step(game, dt);
            self.limit_velocities();
        }
    }

    /// Simulate the ball for at least the given amount of time
    #[inline]
    #[must_use]
    pub fn get_ball_prediction_struct_for_time(self, game: &Game, time: f32) -> Predictions {
        debug_assert!(time >= 0.);
        // Ignoring these warnings is ok because:
        // We are rounding up to the nearest integer so no truncation will occur
        // We are making sure that the minimum possible value is 0 so no sign loss will occur
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        self.get_ball_prediction_struct_for_slices(
            game,
            (time / Self::SIMULATION_DT).round() as usize,
        )
    }

    /// Simulate the ball for the standard amount of time (6 seconds)
    #[inline]
    #[must_use]
    pub fn get_ball_prediction_struct(self, game: &Game) -> Predictions {
        self.get_ball_prediction_struct_for_slices(game, Self::STANDARD_NUM_SLICES)
    }

    /// Simulate the ball for a given amount of ticks
    #[inline]
    #[must_use]
    pub fn get_ball_prediction_struct_for_slices(
        mut self,
        game: &Game,
        num_slices: usize,
    ) -> Predictions {
        (0..num_slices)
            .map(|_| {
                self.step(game, Self::SIMULATION_DT);
                self
            })
            .collect()
    }

    /// Simulate the ball in Heatseeker mode for at least the given amount of time
    #[inline]
    #[must_use]
    #[cfg(feature = "heatseeker")]
    pub fn get_heatseeker_prediction_struct_for_time(self, game: &Game, time: f32) -> Predictions {
        debug_assert!(time >= 0.);
        // Ignoring these warnings is ok because:
        // We are rounding up to the nearest integer so no truncation will occur
        // We are making sure that the minimum possible value is 0 so no sign loss will occur
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        self.get_heatseeker_prediction_struct_for_slices(
            game,
            (time / Self::SIMULATION_DT).round() as usize,
        )
    }

    /// Simulate the ball in Heatseeker mode for the standard amount of time (6 seconds)
    #[inline]
    #[must_use]
    #[cfg(feature = "heatseeker")]
    pub fn get_heatseeker_prediction_struct(self, game: &Game) -> Predictions {
        self.get_heatseeker_prediction_struct_for_slices(game, Self::STANDARD_NUM_SLICES)
    }

    /// Simulate the ball in Heatseeker mode for a given amount of ticks
    #[inline]
    #[must_use]
    #[cfg(feature = "heatseeker")]
    pub fn get_heatseeker_prediction_struct_for_slices(
        mut self,
        game: &Game,
        num_slices: usize,
    ) -> Predictions {
        (0..num_slices)
            .map(|_| {
                self.step_heatseeker(game, Self::SIMULATION_DT);
                self
            })
            .collect()
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::load_standard;

    #[test]
    fn check_standard_num_slices() {
        let (game, ball) = load_standard();

        let prediction = ball.get_ball_prediction_struct(&game);

        assert_eq!(prediction.len(), Ball::STANDARD_NUM_SLICES);
    }

    #[test]
    fn check_custom_num_slices() {
        const REQUESTED_SLICES: usize = 200;

        let (game, ball) = load_standard();

        let prediction = ball.get_ball_prediction_struct_for_slices(&game, REQUESTED_SLICES);

        assert_eq!(prediction.len(), REQUESTED_SLICES);
    }

    #[test]
    fn check_num_slices_for_time() {
        const REQUESTED_TIME: f32 = 8.0;

        let (game, ball) = load_standard();

        let prediction = ball.get_ball_prediction_struct_for_time(&game, REQUESTED_TIME);

        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let predicted_slices = (REQUESTED_TIME / Ball::SIMULATION_DT).ceil().max(0.) as usize;

        assert_eq!(prediction.len(), predicted_slices);
    }
}