From 6c1f60ae59d5e998090c9c0356005c213b57f408 Mon Sep 17 00:00:00 2001 From: core Date: Mon, 27 Nov 2023 22:00:14 -0500 Subject: [PATCH] a failed movement attempt. math is hard and bevy is annoying --- server/src/main.rs | 81 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 8 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index a552998537ae2bb5cfbf1a9831537b747cc99bcf..11da567f290c56d6a853f893e67c48bdae45f0ff 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -2,6 +2,8 @@ use std::net::Ipv4Addr; use bevy::utils::tracing; use bevy::{ecs::event::ManualEventReader, prelude::*}; +use bevy::math::{vec2, vec3}; +use bevy_rapier2d::na::{Rotation2, SVector}; use bevy_rapier2d::prelude::*; use bevy_twite::{twite::frame::MessageType, ServerEvent, TwiteServerConfig, TwiteServerPlugin}; use component::*; @@ -15,6 +17,8 @@ pub mod packet; const SCALE: f32 = 10.0; const EARTH_SIZE: f32 = 1000.0; const GRAVITY: f32 = 0.02; +const PART_HALF_SIZE: f32 = 25.0; +const THRUSTER_FORCE: f32 = 1.0; fn main() { let subscriber = tracing_subscriber::FmtSubscriber::new(); @@ -40,7 +44,8 @@ fn main() { .add_systems(Update, on_message) .add_systems(Update, on_close) .add_systems(FixedUpdate, on_position_change) - .add_systems(FixedUpdate, do_gravity) + .add_systems(FixedUpdate, gravity_update) + .add_systems(FixedUpdate, player_input_update) .run(); info!("Goodbye!"); @@ -76,13 +81,10 @@ fn on_message( match packet { Packet::ClientLogin { username, .. } => { - /*let angle: f32 = { + let angle: f32 = { let mut rng = rand::thread_rng(); rng.gen::() * std::f32::consts::PI * 2. - };* - - */ - let angle = 3f32; + }; let mut transform = Transform::from_xyz( angle.cos() * 1100. / SCALE, angle.sin() * 1100. / SCALE, @@ -101,7 +103,7 @@ fn on_message( input: component::Input::default(), }, }) - .insert(Collider::cuboid(25.0 / SCALE, 25.0 / SCALE)) + .insert(Collider::cuboid(PART_HALF_SIZE / SCALE, PART_HALF_SIZE / SCALE)) .insert(AdditionalMassProperties::Mass(10.0)) .insert(ExternalImpulse { impulse: Vec2::ZERO, @@ -345,7 +347,70 @@ fn on_position_change( } } -fn do_gravity( +fn player_input_update( + mut player_and_body_query: Query<(Entity, &mut Player, &mut ExternalImpulse, &Transform)>, +) { + for (_, player, impulses, transform) in &mut player_and_body_query { + if !(player.input.up || player.input.down || player.input.right || player.input.left) { continue; } + + let mut fmul_bottom_left_thruster = 0.0; + let mut fmul_bottom_right_thruster = 0.0; + let mut fmul_top_left_thruster = 0.0; + let mut fmul_top_right_thruster = 0.0; + if player.input.up { + fmul_bottom_left_thruster += 1.0; + fmul_bottom_right_thruster += 1.0; + } + if player.input.down { + fmul_top_left_thruster += 1.0; + fmul_top_right_thruster += 1.0; + } + if player.input.left { + fmul_top_left_thruster += 1.0; + fmul_bottom_right_thruster += 1.0; + } + if player.input.right { + fmul_top_right_thruster += 1.0; + fmul_bottom_right_thruster += 1.0; + } + + let rot = transform.rotation.to_euler(EulerRot::ZYX).0; + + if fmul_bottom_left_thruster != 0 { + // we are applying a force to the bottom left thruster, so do the math to figure out where it is + let bl_thruster_uncast = transform.translation + vec3(-PART_HALF_SIZE, -PART_HALF_SIZE, 0.0); + // so it turns out nalgeba is useless. because bevy. bevy makes everything difficult for physics + let bl_thruster_cast = vec2(bl_thruster_uncast.x * rot.cos() - bl_thruster_uncast.y * rot.sin(), + bl_thruster_uncast.x * rot.sin() + bl_thruster_uncast.y * rot.cos()); + let bl_thruster_force = fmul_bottom_left_thruster * THRUSTER_FORCE; + let impulse = ExternalImpulse::at_point(bl_thruster_force, bl_thruster_cast, transform.translation); + // todo: add impulse + } + if fmul_bottom_right_thruster != 0 { + let br_thruster_uncast = transform.translation + vec3(PART_HALF_SIZE, -PART_HALF_SIZE, 0.0); + let br_thruster_cast = vec2(br_thruster_uncast.x * rot.cos() - br_thruster_uncast.y * rot.sin(), + br_thruster_uncast.x * rot.sin() + br_thruster_uncast.y * rot.cos()); + let br_thruster_force = fmul_bottom_right_thruster * THRUSTER_FORCE; + // todo: add impulse + } + if fmul_top_left_thruster != 0 { + let tl_thruster_uncast = transform.translation + vec3(-PART_HALF_SIZE, PART_HALF_SIZE, 0.0); + let tl_thruster_cast = vec2(tl_thruster_uncast.x * rot.cos() - tl_thruster_uncast.y * rot.sin(), + tl_thruster_uncast.x * rot.sin() + tl_thruster_uncast.y * rot.cos()); + let tl_thruster_force = fmul_top_left_thruster * THRUSTER_FORCE; + // todo: add impulse + } + if fmul_top_right_thruster != 0 { + let tr_thruster_uncast = transform.translation + vec3(PART_HALF_SIZE, PART_HALF_SIZE, 0.0); + let tr_thruster_cast = vec2(tr_thruster_uncast.x * rot.cos() - tr_thruster_uncast.y * rot.sin(), + tr_thruster_uncast.x * rot.sin() + tr_thruster_uncast.y * rot.cos()); + let tr_thruster_force = fmul_bottom_left_thruster * THRUSTER_FORCE; + // todo: add impulse + } + } +} + +fn gravity_update( mut part_query: Query<(&Transform, &ReadMassProperties, &mut ExternalImpulse), With>, planet_query: Query<(&Transform, &ReadMassProperties), With>, ) {