1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-05-12 22:57:45 +00:00

Fix rotations sent to other players

There was two problems I was running into:
1. The move packet changed slightly, using a different way to encode the
rotation. In hindsight, it should seem obvious they would encode it the
same way for CommonSpawn and ActorMove.
2. write_quantized_rotation did it's order of operations wrong, and
it would spit out nonsensical rotations.

Both issues are fixed and you can see the other player's rotation
correctly now!
This commit is contained in:
Joshua Goins 2025-05-08 21:52:50 -04:00
parent e43ab6c42a
commit f338530e6d
3 changed files with 9 additions and 28 deletions

View file

@ -82,7 +82,7 @@ pub(crate) fn write_quantized_rotation(quantized: &f32) -> u16 {
let max = u16::MAX as f32;
let pi = std::f32::consts::PI;
((quantized + pi / (2.0 * pi)) * max) as u16
(((quantized + pi) / (2.0 * pi)) * max) as u16
}
pub(crate) fn read_packed_float(packed: u16) -> f32 {
@ -93,16 +93,6 @@ pub(crate) fn write_packed_float(float: &f32) -> u16 {
(((float + 1000.0) * 100.0) * 0.327675) as u16
}
pub(crate) fn write_packed_rotation_float(float: &f32) -> u8 {
let pi = std::f32::consts::PI;
(0x80 as f32 * (float + pi) / pi) as u8
}
pub(crate) fn read_packed_rotation_float(_packed: u8) -> f32 {
0.0
}
pub(crate) fn read_packed_position(packed: [u16; 3]) -> Position {
Position {
x: read_packed_float(packed[0]),
@ -212,9 +202,5 @@ mod tests {
fn packed_floats() {
assert_eq!(read_packed_float(32931), 4.989685);
assert_eq!(write_packed_float(&5.0), 32931);
assert_eq!(write_packed_rotation_float(&0.0), 128);
assert_eq!(write_packed_rotation_float(&-2.7768986), 14);
assert_eq!(write_packed_rotation_float(&3.128286), 255);
}
}

View file

@ -1,21 +1,18 @@
use binrw::binrw;
use crate::common::{
Position, read_packed_position, read_packed_rotation_float, write_packed_position,
write_packed_rotation_float,
Position, read_packed_position, read_quantized_rotation, write_packed_position,
write_quantized_rotation,
};
#[binrw]
#[derive(Debug, Clone, Default)]
pub struct Move {
#[bw(map = write_packed_rotation_float)]
#[br(map = read_packed_rotation_float)]
#[br(map = read_quantized_rotation)]
#[bw(map = write_quantized_rotation)]
pub rotation: f32,
pub dir_before_slip: u8,
pub flag1: u8,
pub flag2: u8,
pub speed: u8,
#[brw(pad_before = 1)] // padding
pub flag1: u16,
pub flag2: u16,
#[brw(pad_after = 4)] // empty
#[br(map = read_packed_position)]
#[bw(map = write_packed_position)]

View file

@ -259,10 +259,8 @@ impl ZoneConnection {
timestamp: timestamp_secs(),
data: ServerZoneIpcData::Move(Move {
rotation,
dir_before_slip: 0x7F,
flag1: 0,
flag2: 0,
speed: 0x3C,
flag1: 128,
flag2: 60,
position,
}),
..Default::default()