From 8e8ab28f1938f67054a54990b58937b9a367bbdc Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Mon, 27 Nov 2023 23:50:08 -0600 Subject: [PATCH] movement added, feels terrible. gravity is also very weak right now --- server/src/main.rs | 70 +++++++++++++++++++++++----------- starkingdoms-client/src/hub.ts | 4 +- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index 11da567f290c56d6a853f893e67c48bdae45f0ff..4e1b5949700aed6b6a5dd352d6cdb71e71b88f54 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -17,8 +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; +const PART_HALF_SIZE: f32 = 25.0 / SCALE; +const THRUSTER_FORCE: f32 = 0.001; fn main() { let subscriber = tracing_subscriber::FmtSubscriber::new(); @@ -104,7 +104,11 @@ fn on_message( }, }) .insert(Collider::cuboid(PART_HALF_SIZE / SCALE, PART_HALF_SIZE / SCALE)) - .insert(AdditionalMassProperties::Mass(10.0)) + .insert(AdditionalMassProperties::MassProperties(MassProperties { + local_center_of_mass: vec2(0., 0.), + mass: 0.01, + principal_inertia: 50.0, + })) .insert(ExternalImpulse { impulse: Vec2::ZERO, torque_impulse: 0., @@ -350,7 +354,7 @@ fn on_position_change( 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 { + for (_, player, mut 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; @@ -358,8 +362,8 @@ fn player_input_update( 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; + fmul_bottom_left_thruster -= 1.0; + fmul_bottom_right_thruster -= 1.0; } if player.input.down { fmul_top_left_thruster += 1.0; @@ -367,46 +371,68 @@ fn player_input_update( } if player.input.left { fmul_top_left_thruster += 1.0; - fmul_bottom_right_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; + fmul_bottom_left_thruster -= 1.0; } let rot = transform.rotation.to_euler(EulerRot::ZYX).0; - if fmul_bottom_left_thruster != 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); + let bl_thruster_uncast = 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_cast = bl_thruster_cast + transform.translation.xy(); + 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 + + let bl_thruster_vec = vec2(-bl_thruster_force * rot.sin(), bl_thruster_force * rot.cos()); + let bl_impulse = ExternalImpulse::at_point(bl_thruster_vec, bl_thruster_cast, transform.translation.xy()); + impulses.impulse += bl_impulse.impulse; + impulses.torque_impulse += bl_impulse.torque_impulse; } - if fmul_bottom_right_thruster != 0 { - let br_thruster_uncast = transform.translation + vec3(PART_HALF_SIZE, -PART_HALF_SIZE, 0.0); + if fmul_bottom_right_thruster != 0. { + let br_thruster_uncast = 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_cast = br_thruster_cast + transform.translation.xy(); + let br_thruster_force = fmul_bottom_right_thruster * THRUSTER_FORCE; - // todo: add impulse + + let br_thruster_vec = vec2(-br_thruster_force * rot.sin(), br_thruster_force * rot.cos()); + let br_impulse = ExternalImpulse::at_point(br_thruster_vec, br_thruster_cast, transform.translation.xy()); + impulses.impulse += br_impulse.impulse; + impulses.torque_impulse += br_impulse.torque_impulse; } - if fmul_top_left_thruster != 0 { - let tl_thruster_uncast = transform.translation + vec3(-PART_HALF_SIZE, PART_HALF_SIZE, 0.0); + if fmul_top_left_thruster != 0. { + let tl_thruster_uncast = 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_cast = tl_thruster_cast + transform.translation.xy(); let tl_thruster_force = fmul_top_left_thruster * THRUSTER_FORCE; - // todo: add impulse + + let tl_thruster_vec = vec2(-tl_thruster_force * rot.sin(), tl_thruster_force * rot.cos()); + let tl_impulse = ExternalImpulse::at_point(tl_thruster_vec, tl_thruster_cast, transform.translation.xy()); + impulses.impulse += tl_impulse.impulse; + impulses.torque_impulse += tl_impulse.torque_impulse; } - if fmul_top_right_thruster != 0 { - let tr_thruster_uncast = transform.translation + vec3(PART_HALF_SIZE, PART_HALF_SIZE, 0.0); + if fmul_top_right_thruster != 0. { + let tr_thruster_uncast = 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 + let tr_thruster_cast = tr_thruster_cast + transform.translation.xy(); + let tr_thruster_force = fmul_top_right_thruster * THRUSTER_FORCE; + + let tr_thruster_vec = vec2(-tr_thruster_force * rot.sin(), tr_thruster_force * rot.cos()); + let tr_impulse = ExternalImpulse::at_point(tr_thruster_vec, tr_thruster_cast, transform.translation.xy()); + impulses.impulse += tr_impulse.impulse; + impulses.torque_impulse += tr_impulse.torque_impulse; } + } } diff --git a/starkingdoms-client/src/hub.ts b/starkingdoms-client/src/hub.ts index ea2baff9d8cbdf262fa145bca53e40c1783e9be3..65f07647e312a4c9e1cebef520c01705d9339442 100644 --- a/starkingdoms-client/src/hub.ts +++ b/starkingdoms-client/src/hub.ts @@ -183,7 +183,7 @@ export async function hub_connect( let id = p.parts[i][0]; let new_part = p.parts[i][1]; - if (global.parts_map.has(id)) { + /*if (global.parts_map.has(id)) { let old_part = global.parts_map.get(id)!; let dx = new_part.transform.x - old_part.transform.x; let dy = new_part.transform.y - old_part.transform.y; @@ -193,7 +193,7 @@ export async function hub_connect( if (dx < CUTOFF_XY && dy < CUTOFF_XY && drot < CUTOFF_ROT) { continue; // this packet is under the cutoff, we don't care about it } - } + }*/ global.parts_map.set(id, new_part); if (id === global.me?.part_id) { document.getElementById("pos-val-x")!.innerText =