implement controls and basic collisions

This commit is contained in:
yuni 2024-03-29 16:33:12 +01:00
parent 198da2c850
commit 25db91f39e
4 changed files with 29 additions and 25 deletions

View file

@ -11,7 +11,6 @@ impl Plugin for ActorPlugin {
app.register_type::<ChatBranch>(); app.register_type::<ChatBranch>();
app.add_systems(FixedUpdate, ( app.add_systems(FixedUpdate, (
update_physics_lifeforms, update_physics_lifeforms,
update_physics_actors,
)); ));
app.add_systems(Update, ( app.add_systems(Update, (
handle_new_conversations, handle_new_conversations,
@ -187,19 +186,6 @@ const SUIT_SIMPLE: Suit = Suit {
integrity: 1e5, integrity: 1e5,
}; };
pub fn update_physics_actors(
time: Res<Time>,
mut q_actors: Query<(&mut Actor, &mut Transform)>,
) {
let d = time.delta_seconds();
for (actor, mut transform) in q_actors.iter_mut() {
transform.rotate(actor.angular_momentum);
// TODO: animate only a step based on time between update:
//transform.rotate(actor.angular_momentum.slerp(Quat::IDENTITY, d)); // not working
transform.translation += d * actor.v;
}
}
pub fn update_physics_lifeforms( pub fn update_physics_lifeforms(
time: Res<Time>, time: Res<Time>,
mut query: Query<(&mut LifeForm, &mut Suit, &Actor)>, mut query: Query<(&mut LifeForm, &mut Suit, &Actor)>,

View file

@ -1,6 +1,7 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy::input::mouse::MouseMotion; use bevy::input::mouse::MouseMotion;
use bevy::window::PrimaryWindow; use bevy::window::PrimaryWindow;
use bevy_xpbd_3d::prelude::*;
use std::f32::consts::*; use std::f32::consts::*;
use crate::{settings, audio, actor}; use crate::{settings, audio, actor};
@ -55,7 +56,9 @@ fn run_camera_controller(
&mut Projection, &mut Projection,
&mut actor::Actor, &mut actor::Actor,
&actor::LifeForm, &actor::LifeForm,
&mut actor::Engine &mut actor::Engine,
&mut AngularVelocity,
&mut LinearVelocity,
), (With<Camera>, Without<actor::PlayerDrivesThis>)>, ), (With<Camera>, Without<actor::PlayerDrivesThis>)>,
) { ) {
let dt = time.delta_seconds(); let dt = time.delta_seconds();
@ -67,7 +70,7 @@ fn run_camera_controller(
focused = window_result.unwrap().focused; focused = window_result.unwrap().focused;
} }
if let Ok((mut transform, mut controller, mut projection, mut actor, lifeform, player_engine)) = query.get_single_mut() { if let Ok((mut transform, mut controller, mut projection, mut actor, lifeform, player_engine, mut angularvelocity, mut v)) = query.get_single_mut() {
if !controller.initialized { if !controller.initialized {
controller.initialized = true; controller.initialized = true;
transform.rotation = transform.rotation =
@ -78,6 +81,8 @@ fn run_camera_controller(
return; return;
} }
angularvelocity.0 = Vec3::splat(0.0);
// Handle key input // Handle key input
let mut axis_input = Vec3::ZERO; let mut axis_input = Vec3::ZERO;
if focused { if focused {
@ -100,7 +105,7 @@ fn run_camera_controller(
axis_input.y -= 1.2; axis_input.y -= 1.2;
} }
if key_input.pressed(settings.key_stop) { if key_input.pressed(settings.key_stop) {
let stop_direction = -actor.v.normalize(); let stop_direction = -v.normalize();
if stop_direction.length_squared() > 0.3 { if stop_direction.length_squared() > 0.3 {
axis_input += 1.0 * (transform.rotation.inverse() * stop_direction); axis_input += 1.0 * (transform.rotation.inverse() * stop_direction);
} }
@ -131,17 +136,17 @@ fn run_camera_controller(
let threshold = 1e-5; let threshold = 1e-5;
if key_input.pressed(settings.key_stop) { if key_input.pressed(settings.key_stop) {
for i in 0..3 { for i in 0..3 {
if actor.v[i].abs() < threshold { if v[i].abs() < threshold {
actor.v[i] = 0.0; v[i] = 0.0;
} }
else if actor.v[i].signum() != (actor.v[i] + acceleration_total[i]).signum() { else if v[i].signum() != (v[i] + acceleration_total[i]).signum() {
// Overshoot // Overshoot
actor.v[i] = 0.0; v[i] = 0.0;
acceleration_total[i] = 0.0; acceleration_total[i] = 0.0;
} }
} }
} }
actor.v += acceleration_total; v.0 += acceleration_total;
engine.current_warmup = (engine.current_warmup + dt / engine.warmup_seconds).clamp(0.0, 1.0); engine.current_warmup = (engine.current_warmup + dt / engine.warmup_seconds).clamp(0.0, 1.0);
play_thruster_sound = !settings.mute_sfx; play_thruster_sound = !settings.mute_sfx;
} }

View file

@ -28,9 +28,7 @@ fn main() {
} }
} }
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
App::new().add_plugins(( App::new().add_plugins(OutFlyPlugin).run();
OutFlyPlugin,
)).run();
} else { } else {
// In release builds, embed assets into the binary // In release builds, embed assets into the binary
App::new().add_plugins(( App::new().add_plugins((

View file

@ -7,6 +7,7 @@ use bevy::prelude::*;
//use bevy::render::render_resource::{TextureViewDescriptor, TextureViewDimension}; //use bevy::render::render_resource::{TextureViewDescriptor, TextureViewDimension};
use bevy::pbr::CascadeShadowConfigBuilder; use bevy::pbr::CascadeShadowConfigBuilder;
use bevy::core_pipeline::bloom::{BloomCompositeMode, BloomSettings}; use bevy::core_pipeline::bloom::{BloomCompositeMode, BloomSettings};
use bevy_xpbd_3d::prelude::*;
use std::f32::consts::PI; use std::f32::consts::PI;
const ASTEROID_SIZE: f32 = 100.0; const ASTEROID_SIZE: f32 = 100.0;
@ -40,6 +41,7 @@ impl Plugin for WorldPlugin {
app.add_systems(Startup, (setup, load_defs)); app.add_systems(Startup, (setup, load_defs));
//app.add_systems(Update, asset_loaded.after(load_cubemap_asset)); //app.add_systems(Update, asset_loaded.after(load_cubemap_asset));
//app.add_systems(Update, swap_world_on_ar_toggle); //app.add_systems(Update, swap_world_on_ar_toggle);
app.add_plugins(PhysicsPlugins::default());
app.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0))); app.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)));
} }
} }
@ -81,6 +83,9 @@ pub fn setup(
// Add player // Add player
commands.spawn(( commands.spawn((
actor::Player, actor::Player,
RigidBody::Dynamic,
AngularVelocity(Vec3::new(0.0, 0.0, 0.0)),
Collider::cuboid(1.0, 1.0, 1.0),
actor::Actor { actor::Actor {
angular_momentum: Quat::IDENTITY, angular_momentum: Quat::IDENTITY,
..default() ..default()
@ -391,6 +396,8 @@ impl ParserState {
component_suit, component_suit,
component_talker, component_talker,
component_model, component_model,
RigidBody::Dynamic,
Collider::cuboid(1.0, 1.0, 1.0),
)); ));
} }
else { else {
@ -399,6 +406,8 @@ impl ParserState {
component_lifeform, component_lifeform,
component_suit, component_suit,
component_model, component_model,
RigidBody::Dynamic,
Collider::cuboid(1.0, 1.0, 1.0),
)); ));
} }
} }
@ -408,6 +417,8 @@ impl ParserState {
component_actor, component_actor,
component_talker, component_talker,
component_model, component_model,
RigidBody::Dynamic,
Collider::cuboid(1.0, 1.0, 1.0),
)); ));
} }
else { else {
@ -417,12 +428,16 @@ impl ParserState {
component_model, component_model,
component_vehicle, component_vehicle,
component_engine, component_engine,
RigidBody::Dynamic,
Collider::cuboid(1.0, 1.0, 1.0),
)); ));
} }
else { else {
commands.spawn(( commands.spawn((
component_actor, component_actor,
component_model, component_model,
RigidBody::Dynamic,
Collider::cuboid(1.0, 1.0, 1.0),
)); ));
} }
} }