Skip to content

Commit

Permalink
Update to new RocketSim protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
VirxEC committed Oct 14, 2023
1 parent 6e42962 commit 97caeaa
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 144 deletions.
148 changes: 70 additions & 78 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
version = "0.3.5"
version = "0.4.0"
name = "rlviser"
edition = "2021"
publish = false
Expand All @@ -9,7 +9,7 @@ publish = false
[dependencies]
bevy_asset_loader = "0.17.0"
bevy_atmosphere = "0.7.0"
bevy_egui = "0.21.0"
bevy_egui = "0.22.0"
bevy_eventlistener = "0.3.0"
bevy_framepace = { version = "0.13.2", default-features = false }
bevy_mod_picking = { version = "0.15.0", default-features = false, features = ["backend_raycast"]}
Expand Down
2 changes: 1 addition & 1 deletion src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ fn retreive_material(name: &str, asset_server: &AssetServer, base_color: Color)
};

let props = format!("./assets/{pre_path}.props.txt");
let Ok(props_file) = fs::read_to_string(&props) else {
let Ok(props_file) = fs::read_to_string(props) else {
error!("Failed to read {path} ({name})");
return None;
};
Expand Down
84 changes: 41 additions & 43 deletions src/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use bevy::{
math::{Mat3A as RotMat, Vec3A as Vec3},
prelude::Quat,
};
use bevy::math::{Mat3A as RotMat, Vec3A as Vec3};

use crate::rocketsim::{
BallHitInfo, BallState, BoostPad, BoostPadState, CarConfig, CarControls, CarInfo, CarState, GameState, Team,
WheelPairConfig,
BallHitInfo, BallState, BoostPad, BoostPadState, CarConfig, CarControls, CarInfo, CarState, GameState, HeatseekerInfo,
Team, WheelPairConfig,
};

trait ToBytesExact<const N: usize>: FromBytesExact {
Expand Down Expand Up @@ -36,12 +33,25 @@ impl ToBytesExact<{ Self::NUM_BYTES }> for RotMat {
}
}

impl ToBytesExact<{ Self::NUM_BYTES }> for HeatseekerInfo {
fn to_bytes(&self) -> [u8; Self::NUM_BYTES] {
let mut bytes = [0; Self::NUM_BYTES];
bytes[..f32::NUM_BYTES].copy_from_slice(&self.y_target_dir.to_le_bytes());
bytes[f32::NUM_BYTES..f32::NUM_BYTES * 2].copy_from_slice(&self.cur_target_speed.to_le_bytes());
bytes[f32::NUM_BYTES * 2..].copy_from_slice(&self.time_since_hit.to_le_bytes());
bytes
}
}

impl ToBytesExact<{ Self::NUM_BYTES }> for BallState {
fn to_bytes(&self) -> [u8; Self::NUM_BYTES] {
let mut bytes = [0; Self::NUM_BYTES];
bytes[..Vec3::NUM_BYTES].copy_from_slice(&self.pos.to_bytes());
bytes[Vec3::NUM_BYTES..Vec3::NUM_BYTES * 2].copy_from_slice(&self.vel.to_bytes());
bytes[Vec3::NUM_BYTES * 2..].copy_from_slice(&self.ang_vel.to_bytes());
bytes[Vec3::NUM_BYTES + RotMat::NUM_BYTES..Vec3::NUM_BYTES * 2 + RotMat::NUM_BYTES]
.copy_from_slice(&self.vel.to_bytes());
bytes[Vec3::NUM_BYTES * 2 + RotMat::NUM_BYTES..Vec3::NUM_BYTES * 3 + RotMat::NUM_BYTES]
.copy_from_slice(&self.ang_vel.to_bytes());
bytes[Vec3::NUM_BYTES * 2 + RotMat::NUM_BYTES..].copy_from_slice(&self.hs_info.to_bytes());
bytes
}
}
Expand Down Expand Up @@ -261,10 +271,6 @@ impl ToBytes for GameState {
bytes.extend(&(self.pads.len() as u32).to_le_bytes());
bytes.extend(&(self.cars.len() as u32).to_le_bytes());
bytes.extend(self.ball.to_bytes());
bytes.extend(self.ball_rot.x.to_le_bytes());
bytes.extend(self.ball_rot.y.to_le_bytes());
bytes.extend(self.ball_rot.z.to_le_bytes());
bytes.extend(self.ball_rot.w.to_le_bytes());
bytes.extend(self.pads.iter().flat_map(ToBytesExact::<{ BoostPad::NUM_BYTES }>::to_bytes));
bytes.extend(self.cars.iter().flat_map(ToBytesExact::<{ CarInfo::NUM_BYTES }>::to_bytes));

Expand Down Expand Up @@ -330,15 +336,32 @@ impl FromBytesExact for RotMat {
}
}

impl FromBytesExact for HeatseekerInfo {
const NUM_BYTES: usize = f32::NUM_BYTES * 3;

#[inline]
fn from_bytes(bytes: &[u8]) -> Self {
Self {
y_target_dir: f32::from_bytes(&bytes[..f32::NUM_BYTES]),
cur_target_speed: f32::from_bytes(&bytes[f32::NUM_BYTES..f32::NUM_BYTES * 2]),
time_since_hit: f32::from_bytes(&bytes[f32::NUM_BYTES * 2..]),
}
}
}

impl FromBytesExact for BallState {
const NUM_BYTES: usize = Vec3::NUM_BYTES * 3;
const NUM_BYTES: usize = Vec3::NUM_BYTES * 3 + RotMat::NUM_BYTES + HeatseekerInfo::NUM_BYTES;

#[inline]
fn from_bytes(bytes: &[u8]) -> Self {
Self {
pos: Vec3::from_bytes(&bytes[..Vec3::NUM_BYTES]),
vel: Vec3::from_bytes(&bytes[Vec3::NUM_BYTES..Vec3::NUM_BYTES * 2]),
ang_vel: Vec3::from_bytes(&bytes[Vec3::NUM_BYTES * 2..]),
rot_mat: RotMat::from_bytes(&bytes[Vec3::NUM_BYTES..Vec3::NUM_BYTES + RotMat::NUM_BYTES]),
vel: Vec3::from_bytes(&bytes[Vec3::NUM_BYTES + RotMat::NUM_BYTES..Vec3::NUM_BYTES * 2 + RotMat::NUM_BYTES]),
ang_vel: Vec3::from_bytes(
&bytes[Vec3::NUM_BYTES * 2 + RotMat::NUM_BYTES..Vec3::NUM_BYTES * 3 + RotMat::NUM_BYTES],
),
hs_info: HeatseekerInfo::from_bytes(&bytes[Vec3::NUM_BYTES * 3 + RotMat::NUM_BYTES..]),
}
}
}
Expand Down Expand Up @@ -587,7 +610,6 @@ impl FromBytes for GameState {
fn get_num_bytes(bytes: &[u8]) -> usize {
Self::MIN_NUM_BYTES
+ BallState::NUM_BYTES
+ f32::NUM_BYTES * 4
+ Self::read_num_pads(bytes) * BoostPad::NUM_BYTES
+ Self::read_num_cars(bytes) * CarInfo::NUM_BYTES
}
Expand Down Expand Up @@ -618,36 +640,12 @@ impl FromBytes for GameState {
tick_count: Self::read_tick_count(bytes),
tick_rate: Self::read_tick_rate(bytes),
ball: BallState::from_bytes(&bytes[Self::MIN_NUM_BYTES..Self::MIN_NUM_BYTES + BallState::NUM_BYTES]),
ball_rot: Quat::from_xyzw(
f32::from_bytes(
&bytes[Self::MIN_NUM_BYTES + BallState::NUM_BYTES
..Self::MIN_NUM_BYTES + BallState::NUM_BYTES + f32::NUM_BYTES],
),
f32::from_bytes(
&bytes[Self::MIN_NUM_BYTES + BallState::NUM_BYTES + f32::NUM_BYTES
..Self::MIN_NUM_BYTES + BallState::NUM_BYTES + f32::NUM_BYTES * 2],
),
f32::from_bytes(
&bytes[Self::MIN_NUM_BYTES + BallState::NUM_BYTES + f32::NUM_BYTES * 2
..Self::MIN_NUM_BYTES + BallState::NUM_BYTES + f32::NUM_BYTES * 3],
),
f32::from_bytes(
&bytes[Self::MIN_NUM_BYTES + BallState::NUM_BYTES + f32::NUM_BYTES * 3
..Self::MIN_NUM_BYTES + BallState::NUM_BYTES + f32::NUM_BYTES * 4],
),
),
pads: bytes[Self::MIN_NUM_BYTES + BallState::NUM_BYTES + f32::NUM_BYTES * 4
..Self::MIN_NUM_BYTES
+ BallState::NUM_BYTES
+ f32::NUM_BYTES * 4
+ Self::read_num_pads(bytes) * BoostPad::NUM_BYTES]
pads: bytes[Self::MIN_NUM_BYTES + BallState::NUM_BYTES
..Self::MIN_NUM_BYTES + BallState::NUM_BYTES + Self::read_num_pads(bytes) * BoostPad::NUM_BYTES]
.chunks_exact(BoostPad::NUM_BYTES)
.map(BoostPad::from_bytes)
.collect(),
cars: bytes[Self::MIN_NUM_BYTES
+ BallState::NUM_BYTES
+ f32::NUM_BYTES * 4
+ Self::read_num_pads(bytes) * BoostPad::NUM_BYTES..]
cars: bytes[Self::MIN_NUM_BYTES + BallState::NUM_BYTES + Self::read_num_pads(bytes) * BoostPad::NUM_BYTES..]
.chunks_exact(CarInfo::NUM_BYTES)
.map(CarInfo::from_bytes)
.collect(),
Expand Down
2 changes: 1 addition & 1 deletion src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Default for Options {
}

impl Options {
const FILE_NAME: &str = "settings.txt";
const FILE_NAME: &'static str = "settings.txt";

#[inline]
fn default_read_file() -> Self {
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![warn(clippy::all)]

mod assets;
mod bytes;
mod camera;
Expand Down
27 changes: 25 additions & 2 deletions src/rocketsim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,44 @@ pub struct BallHitInfo {
pub tick_count_when_extra_impulse_applied: u64,
}

#[derive(Clone, Copy, Debug)]
pub struct HeatseekerInfo {
/// Which net the ball should seek towards;
/// When 0, no net
pub y_target_dir: f32,
pub cur_target_speed: f32,
pub time_since_hit: f32,
}

impl Default for HeatseekerInfo {
#[inline]
fn default() -> Self {
Self {
y_target_dir: 0.,
cur_target_speed: 2900.,
time_since_hit: 0.,
}
}
}

#[derive(Clone, Copy, Debug)]
pub struct BallState {
pub pos: Vec3,
pub rot_mat: RotMat,
pub vel: Vec3,
pub ang_vel: Vec3,
pub hs_info: HeatseekerInfo,
}

impl Default for BallState {
#[inline]
fn default() -> Self {
Self {
pos: Vec3::new(0., 0., 92.),
pos: Vec3::new(0., 0., 93.15),
rot_mat: RotMat::IDENTITY,
vel: Vec3::ZERO,
ang_vel: Vec3::ZERO,
hs_info: HeatseekerInfo::default(),
}
}
}
Expand Down Expand Up @@ -129,7 +153,6 @@ pub struct GameState {
pub tick_count: u64,
pub tick_rate: f32,
pub ball: BallState,
pub ball_rot: Quat,
pub pads: Box<[BoostPad]>,
pub cars: Box<[CarInfo]>,
}
18 changes: 1 addition & 17 deletions src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,6 @@ impl ToBevyMat for Mat3A {
}
}

trait ToBevyQuat {
fn to_bevy(self) -> Quat;
}

impl ToBevyQuat for Quat {
#[inline]
fn to_bevy(self) -> Quat {
// In RocketSim, the Z axis is up, but in Bevy, the Z and Y axis are swapped
// We also need to rotate 90 degrees around the X axis and 180 degrees around the Y axis
Self::from_axis_angle(Vec3::Y, PI)
* Self::from_axis_angle(Vec3::X, PI / 2.)
* self
* Self::from_mat3a(&Mat3A::from_cols(Vec3A::X, -Vec3A::Z, Vec3A::Y))
}
}

const NUM_CAR_BODIES: usize = 6;

const CAR_BODIES: [&str; NUM_CAR_BODIES] = [
Expand Down Expand Up @@ -409,7 +393,7 @@ fn update_ball(
Color::rgb(0.5, 0.5, amount.max(0.5))
};

transform.rotation = state.ball_rot.to_bevy();
transform.rotation = state.ball.rot_mat.to_bevy();
}

const MIN_DIST_FROM_BALL: f32 = 200.;
Expand Down

0 comments on commit 97caeaa

Please sign in to comment.