From 1962d4d325ddf67efad4b1d13e0681067b69f4c0 Mon Sep 17 00:00:00 2001 From: Franklin Date: Mon, 27 Nov 2023 15:36:57 -0400 Subject: [PATCH 1/2] Velocity and velocity shed --- src/comps/core/markers/bullet.rs | 6 +++-- src/comps/core/weapons/caliber.rs | 27 ++++++++++++++++--- .../core/guns/equipped_gun_collisions.rs | 2 -- src/logic/core/guns/mod.rs | 3 ++- src/logic/core/guns/shoot.rs | 20 ++------------ src/logic/core/guns/update_bullet.rs | 17 ++++++++++++ src/scenes/scene1/init.rs | 3 ++- src/ui/editor/plugin.rs | 3 ++- 8 files changed, 52 insertions(+), 29 deletions(-) create mode 100644 src/logic/core/guns/update_bullet.rs diff --git a/src/comps/core/markers/bullet.rs b/src/comps/core/markers/bullet.rs index a61b575..ff99f66 100644 --- a/src/comps/core/markers/bullet.rs +++ b/src/comps/core/markers/bullet.rs @@ -1,14 +1,16 @@ use bevy::{ prelude::{Component, Vec3}, reflect::Reflect, - time::Timer, + time::Timer, ecs::reflect::ReflectComponent, }; use crate::comps::core::weapons::caliber::Caliber; -#[derive(Component, Reflect)] +#[derive(Component, Reflect, Default)] +#[reflect(Component)] pub struct BulletMarker { pub caliber: Caliber, pub starting_point: Vec3, pub timer: Timer, + pub current_velocity: Vec3, } diff --git a/src/comps/core/weapons/caliber.rs b/src/comps/core/weapons/caliber.rs index 618bf71..f1fa7b2 100644 --- a/src/comps/core/weapons/caliber.rs +++ b/src/comps/core/weapons/caliber.rs @@ -1,13 +1,15 @@ use bevy::reflect::Reflect; #[allow(unused)] -#[derive(Clone, Reflect)] +#[derive(Clone, Reflect, Default)] pub enum Caliber { /// 5.56x45mm NATO556, /// 9x19mm + #[default] Parabellum9mm, - /// 5.45x39mm + /// 5.45x39mm (7N6M) + /// Reference: [Wikipedia](https://en.wikipedia.org/wiki/5.45%C3%9739mm) RU545 } impl Caliber { @@ -32,11 +34,12 @@ impl Caliber { Caliber::RU545 => 75.0, } } + /// Unit = kg pub fn mass(&self) -> f32 { match self { - Caliber::NATO556 => 0.1, + Caliber::NATO556 => 0.00402, Caliber::Parabellum9mm => 0.05, - Caliber::RU545 => 0.07, + Caliber::RU545 => 0.00343, } } pub fn linear_damping(&self) -> f32 { @@ -60,4 +63,20 @@ impl Caliber { Caliber::RU545 => 0.06 } } + /// Unit = m/s + pub fn muzzle_velocity(&self) -> f32 { + match self { + Caliber::NATO556 => 991.0, + Caliber::Parabellum9mm => todo!(), + Caliber::RU545 => 880.0, + } + } + /// Unit = m/s + pub fn velocity_shed(&self) -> f32 { + match self { + Caliber::NATO556 => todo!(), + Caliber::Parabellum9mm => todo!(), + Caliber::RU545 => 120.0, + } + } } diff --git a/src/logic/core/guns/equipped_gun_collisions.rs b/src/logic/core/guns/equipped_gun_collisions.rs index dad9027..9f3ae13 100644 --- a/src/logic/core/guns/equipped_gun_collisions.rs +++ b/src/logic/core/guns/equipped_gun_collisions.rs @@ -10,7 +10,6 @@ pub fn update_gun_position_on_collision( mut events: EventReader, ) { for event in events.read() { - println!("Collision"); for mut player_firing_info in query.iter_mut() { match event.collision_type { CollisionEventType::Start => { @@ -19,7 +18,6 @@ pub fn update_gun_position_on_collision( }, CollisionEventType::End => { player_firing_info.gun_colliding_with_object = false; - //player_firing_info.gun_ready_pose = GunReadyPose::HighReady; }, } } diff --git a/src/logic/core/guns/mod.rs b/src/logic/core/guns/mod.rs index 9c668ea..40b27fb 100644 --- a/src/logic/core/guns/mod.rs +++ b/src/logic/core/guns/mod.rs @@ -2,4 +2,5 @@ pub mod despawn_shots; pub mod player_firing; pub mod shoot; pub mod inspect; -pub mod equipped_gun_collisions; \ No newline at end of file +pub mod equipped_gun_collisions; +pub mod update_bullet; \ No newline at end of file diff --git a/src/logic/core/guns/shoot.rs b/src/logic/core/guns/shoot.rs index cb5269f..fb3ef72 100644 --- a/src/logic/core/guns/shoot.rs +++ b/src/logic/core/guns/shoot.rs @@ -64,8 +64,6 @@ pub fn shoot_bullet( meshes, materials, firing_point, - forward, - up, caliber, ); } @@ -75,8 +73,6 @@ pub fn spawn_bullet( meshes: &mut ResMut>, materials: &mut ResMut>, firing_point: Transform, - forward: Vec3, - _up: Vec3, caliber: Caliber, ) { commands.spawn(( @@ -88,6 +84,7 @@ pub fn spawn_bullet( Duration::from_secs_f32(caliber.max_airtime_secs()), TimerMode::Once, ), + current_velocity: firing_point.forward() * caliber.muzzle_velocity(), }, MaterialMeshBundle { mesh: { @@ -113,21 +110,8 @@ pub fn spawn_bullet( transform: firing_point, ..Default::default() }, - RigidBody::Dynamic, - GravityScale(4.0), - Collider::ball(caliber.size()), - Velocity::zero(), - Damping { - linear_damping: caliber.linear_damping(), - angular_damping: caliber.angular_damping(), - }, - ColliderMassProperties::Mass(caliber.mass()), - ExternalImpulse { - impulse: forward * caliber.impulse(), - torque_impulse: Vec3::ZERO, - }, + RigidBody::KinematicPositionBased, ActiveEvents::COLLISION_EVENTS, CollisionGroups::new(Group::from_bits_retain(ColliderFlags::BULLETS.bits()), Group::from_bits_retain((ColliderFlags::SOLIDS | ColliderFlags::FLOOR).bits())), - Ccd::enabled(), )); } diff --git a/src/logic/core/guns/update_bullet.rs b/src/logic/core/guns/update_bullet.rs new file mode 100644 index 0000000..1533040 --- /dev/null +++ b/src/logic/core/guns/update_bullet.rs @@ -0,0 +1,17 @@ +use bevy::prelude::*; +use bevy_rapier3d::prelude::*; + +use crate::comps::core::markers::bullet::BulletMarker; + +pub fn update_bullet( + mut commands: Commands, + mut bullets: Query<(Entity, &mut BulletMarker, &mut Transform)>, + time: Res