Skip to content

Commit

Permalink
syncs vr transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
MalekiRe committed Sep 27, 2024
1 parent a79e3f4 commit 9f314b3
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 15 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ bevy_embedded_assets = "0.11.0"
bevy_web_file_drop = "0.0.6"
bevy_vr_controller = { path = "../bevy_vr_controller"}
bevy_mod_openxr = { git = "https://github.com/awtterpip/bevy_oxr" }
bevy_xr_utils = { git = "https://github.com/awtterpip/bevy_oxr"}
bevy_spatial_egui = { path = "../bevy_spatial_egui" }
bevy_egui = "0.29.0"
bevy-suis = { git = "https://github.com/Schmarni-Dev/bevy-suis", branch = "further_work" }
bevy_vrm = "0.0.12"

[profile.release]
lto = "thin"
lto = true
codegen-units = 1
opt-level = "z"
panic="abort"

[profile.dev]
opt-level = 3
14 changes: 14 additions & 0 deletions Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[build]
# Point to our `index.html`.
target = "index.html"
# Set the output directory for the web build.
dist = "target/trunk"
# This is needed in order to host the game on itch.io.
public_url = "./"

[serve]
# Required in order to receive 404s for missing assets, which is what Bevy expects.
no_spa = true
# Open a browser tab once the initial build is complete.
open = true
no_autoreload = true
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>Nexus</title> <!-- ToDo -->
<link data-trunk rel="copy-dir" href="assets"/>
<link data-trunk rel="copy-file" href="build/windows/icon.ico"/>
<link data-trunk rel="icon" type = "image/ico" href="build/windows/icon.ico">
<link data-trunk rel="inline" href="build/web/styles.css"/>
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ pub fn main() {
.add_plugins((
EmbeddedAssetPlugin::default(),
bevy_web_file_drop::WebFileDropPlugin,
DefaultPlugins.set(AssetPlugin {
/*bevy_mod_openxr::add_xr_plugins(*/DefaultPlugins.set(AssetPlugin {
meta_check: AssetMetaCheck::Never,
..AssetPlugin::default()
}),
})/*)*/,
//bevy_xr_utils::xr_utils_actions::XRUtilsActionsPlugin,
PhysicsPlugins::default(),
VrControllerPlugin,
))
Expand Down
24 changes: 15 additions & 9 deletions src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ pub struct UpdatePhysicsPosition {

#[derive(Clone, Debug, Serialize, Deserialize, Event)]
pub struct PlayerPositionUpdate {
uuid: Uuid,
head: Transform,
left_upper_arm: Transform,
right_upper_arm: Transform,
left_lower_arm: Transform,
right_lower_arm: Transform,
left_hand: Transform,
right_hand: Transform,
pub uuid: Uuid,
pub head: Transform,
pub left_shoulder: Transform,
pub right_shoulder: Transform,
pub left_upper_arm: Transform,
pub right_upper_arm: Transform,
pub left_lower_arm: Transform,
pub right_lower_arm: Transform,
pub left_hand: Transform,
pub right_hand: Transform,
}

#[derive(
Expand Down Expand Up @@ -88,6 +90,7 @@ impl Plugin for NetworkingPlugin {
app.add_event::<PlayerConnected>();
app.add_event::<PlayerDisconnected>();
app.add_event::<UpdatePhysicsPosition>();
app.add_event::<PlayerPositionUpdate>();

app.add_systems(
FixedUpdate,
Expand Down Expand Up @@ -122,14 +125,17 @@ fn handle_messages_reliable(
fn handle_messages_unreliable(
mut connection: Connection,
mut update_physics_positions: EventWriter<UpdatePhysicsPosition>,
mut player_position_updates: EventWriter<PlayerPositionUpdate>,
) {
for (message, _peer_id) in connection.recv() {
let message: UnreliableMessage = message;
match message {
UnreliableMessage::UpdatePhysicsPosition(update_physics_position) => {
update_physics_positions.send(update_physics_position);
}
UnreliableMessage::PlayerPositionUpdate(_) => todo!(),
UnreliableMessage::PlayerPositionUpdate(player_position_update) => {
player_position_updates.send(player_position_update);
},
}
}
}
Expand Down
44 changes: 42 additions & 2 deletions src/player_networking.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::networking::{Connection, ConnectionTrait, LocalPlayer, PlayerConnected, PlayerDisconnected, ReliableMessage, RemotePlayer, SpawnPlayer, UnreliableMessage, UpdatePhysicsPosition};
use crate::networking::{Connection, ConnectionTrait, LocalPlayer, PlayerConnected, PlayerDisconnected, PlayerPositionUpdate, ReliableMessage, RemotePlayer, SpawnPlayer, UnreliableMessage, UpdatePhysicsPosition};
use crate::spawn_avatar;
use avian3d::prelude::{AngularVelocity, LinearVelocity, Position, Rotation};
use bevy::prelude::*;
use bevy_matchbox::prelude::MultipleChannels;
use bevy_matchbox::MatchboxSocket;
use bevy_mod_openxr::session::OxrSession;
use bevy_vr_controller::animation::defaults::default_character_animations;
use bevy_vr_controller::player::PlayerSettings;
use bevy_vrm::BoneName;

pub struct PlayerNetworking;

Expand Down Expand Up @@ -79,6 +81,7 @@ fn send_players_when_connected(
fn update_player_position(
local_player: Query<
(
Entity,
&Position,
&Rotation,
&LinearVelocity,
Expand All @@ -94,8 +97,11 @@ fn update_player_position(
),
>,
mut connection: Connection,
transforms: Query<(&BoneName, &Transform), Changed<Transform>>,
children: Query<&Children>,
session: Option<Res<OxrSession>>,
) {
let Ok((position, rotation, lin_vel, ang_vel, local_player)) = local_player.get_single() else {return};
let Ok((entity, position, rotation, lin_vel, ang_vel, local_player)) = local_player.get_single() else {return};
let message = UnreliableMessage::UpdatePhysicsPosition(UpdatePhysicsPosition {
authority: 0,
uuid: local_player.0,
Expand All @@ -106,4 +112,38 @@ fn update_player_position(
});

let _ = connection.send_all(&message);

if session.is_none() {
return;
}

let mut player_position_update = PlayerPositionUpdate {
uuid: local_player.0.clone(),
head: Default::default(),
left_shoulder: Default::default(),
right_shoulder: Default::default(),
left_upper_arm: Default::default(),
right_upper_arm: Default::default(),
left_lower_arm: Default::default(),
right_lower_arm: Default::default(),
left_hand: Default::default(),
right_hand: Default::default(),
};
for children in children.iter_descendants(entity) {
for (bone_name, transform) in transforms.get(children) {
match bone_name {
BoneName::Head => player_position_update.head = transform.clone(),
BoneName::LeftShoulder => player_position_update.left_shoulder = transform.clone(),
BoneName::RightShoulder => player_position_update.right_shoulder = transform.clone(),
BoneName::LeftUpperArm => player_position_update.left_upper_arm = transform.clone(),
BoneName::RightUpperArm => player_position_update.right_upper_arm = transform.clone(),
BoneName::LeftLowerArm => player_position_update.left_lower_arm = transform.clone(),
BoneName::RightLowerArm => player_position_update.right_lower_arm = transform.clone(),
BoneName::LeftHand => player_position_update.left_hand = transform.clone(),
BoneName::RightHand => player_position_update.right_hand = transform.clone(),
_ => {}
}
}
}
let _ = connection.send_all(&UnreliableMessage::PlayerPositionUpdate(player_position_update));
}

0 comments on commit 9f314b3

Please sign in to comment.