Velocity and velocity shed

This commit is contained in:
Franklin 2023-11-27 15:36:57 -04:00
parent 2538e003fc
commit 1962d4d325
8 changed files with 52 additions and 29 deletions

View File

@ -1,14 +1,16 @@
use bevy::{ use bevy::{
prelude::{Component, Vec3}, prelude::{Component, Vec3},
reflect::Reflect, reflect::Reflect,
time::Timer, time::Timer, ecs::reflect::ReflectComponent,
}; };
use crate::comps::core::weapons::caliber::Caliber; use crate::comps::core::weapons::caliber::Caliber;
#[derive(Component, Reflect)] #[derive(Component, Reflect, Default)]
#[reflect(Component)]
pub struct BulletMarker { pub struct BulletMarker {
pub caliber: Caliber, pub caliber: Caliber,
pub starting_point: Vec3, pub starting_point: Vec3,
pub timer: Timer, pub timer: Timer,
pub current_velocity: Vec3,
} }

View File

@ -1,13 +1,15 @@
use bevy::reflect::Reflect; use bevy::reflect::Reflect;
#[allow(unused)] #[allow(unused)]
#[derive(Clone, Reflect)] #[derive(Clone, Reflect, Default)]
pub enum Caliber { pub enum Caliber {
/// 5.56x45mm /// 5.56x45mm
NATO556, NATO556,
/// 9x19mm /// 9x19mm
#[default]
Parabellum9mm, Parabellum9mm,
/// 5.45x39mm /// 5.45x39mm (7N6M)
/// Reference: [Wikipedia](https://en.wikipedia.org/wiki/5.45%C3%9739mm)
RU545 RU545
} }
impl Caliber { impl Caliber {
@ -32,11 +34,12 @@ impl Caliber {
Caliber::RU545 => 75.0, Caliber::RU545 => 75.0,
} }
} }
/// Unit = kg
pub fn mass(&self) -> f32 { pub fn mass(&self) -> f32 {
match self { match self {
Caliber::NATO556 => 0.1, Caliber::NATO556 => 0.00402,
Caliber::Parabellum9mm => 0.05, Caliber::Parabellum9mm => 0.05,
Caliber::RU545 => 0.07, Caliber::RU545 => 0.00343,
} }
} }
pub fn linear_damping(&self) -> f32 { pub fn linear_damping(&self) -> f32 {
@ -60,4 +63,20 @@ impl Caliber {
Caliber::RU545 => 0.06 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,
}
}
} }

View File

@ -10,7 +10,6 @@ pub fn update_gun_position_on_collision(
mut events: EventReader<EquippedGunCollisionEvent>, mut events: EventReader<EquippedGunCollisionEvent>,
) { ) {
for event in events.read() { for event in events.read() {
println!("Collision");
for mut player_firing_info in query.iter_mut() { for mut player_firing_info in query.iter_mut() {
match event.collision_type { match event.collision_type {
CollisionEventType::Start => { CollisionEventType::Start => {
@ -19,7 +18,6 @@ pub fn update_gun_position_on_collision(
}, },
CollisionEventType::End => { CollisionEventType::End => {
player_firing_info.gun_colliding_with_object = false; player_firing_info.gun_colliding_with_object = false;
//player_firing_info.gun_ready_pose = GunReadyPose::HighReady;
}, },
} }
} }

View File

@ -3,3 +3,4 @@ pub mod player_firing;
pub mod shoot; pub mod shoot;
pub mod inspect; pub mod inspect;
pub mod equipped_gun_collisions; pub mod equipped_gun_collisions;
pub mod update_bullet;

View File

@ -64,8 +64,6 @@ pub fn shoot_bullet(
meshes, meshes,
materials, materials,
firing_point, firing_point,
forward,
up,
caliber, caliber,
); );
} }
@ -75,8 +73,6 @@ pub fn spawn_bullet(
meshes: &mut ResMut<Assets<Mesh>>, meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<StandardMaterial>>, materials: &mut ResMut<Assets<StandardMaterial>>,
firing_point: Transform, firing_point: Transform,
forward: Vec3,
_up: Vec3,
caliber: Caliber, caliber: Caliber,
) { ) {
commands.spawn(( commands.spawn((
@ -88,6 +84,7 @@ pub fn spawn_bullet(
Duration::from_secs_f32(caliber.max_airtime_secs()), Duration::from_secs_f32(caliber.max_airtime_secs()),
TimerMode::Once, TimerMode::Once,
), ),
current_velocity: firing_point.forward() * caliber.muzzle_velocity(),
}, },
MaterialMeshBundle { MaterialMeshBundle {
mesh: { mesh: {
@ -113,21 +110,8 @@ pub fn spawn_bullet(
transform: firing_point, transform: firing_point,
..Default::default() ..Default::default()
}, },
RigidBody::Dynamic, RigidBody::KinematicPositionBased,
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,
},
ActiveEvents::COLLISION_EVENTS, ActiveEvents::COLLISION_EVENTS,
CollisionGroups::new(Group::from_bits_retain(ColliderFlags::BULLETS.bits()), Group::from_bits_retain((ColliderFlags::SOLIDS | ColliderFlags::FLOOR).bits())), CollisionGroups::new(Group::from_bits_retain(ColliderFlags::BULLETS.bits()), Group::from_bits_retain((ColliderFlags::SOLIDS | ColliderFlags::FLOOR).bits())),
Ccd::enabled(),
)); ));
} }

View File

@ -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<Time>,
) {
for (bullet, mut marker, mut transform) in bullets.iter_mut() {
transform.translation += (marker.current_velocity * time.delta_seconds()) + (Vec3 { x: 0.0, y: -9.81, z: 0.0 } * time.delta_seconds() * marker.caliber.mass());
let vel_shed = marker.caliber.velocity_shed();
marker.current_velocity -= transform.forward() * vel_shed * time.delta_seconds();
}
}

View File

@ -7,7 +7,7 @@ use crate::{
spawners::{player::player_spawner, spawn::SpawnerPlugin}, events::{bullet_collision::BulletCollisionEvent, equipped_gun_collision::EquippedGunCollisionEvent}, spawners::{player::player_spawner, spawn::SpawnerPlugin}, events::{bullet_collision::BulletCollisionEvent, equipped_gun_collision::EquippedGunCollisionEvent},
}, },
logic::core::{ logic::core::{
guns::{despawn_shots::{despawn_muzzle_flashes, despawn_stray_bullets}, inspect::inspect_firearm, equipped_gun_collisions::update_gun_position_on_collision}, guns::{despawn_shots::{despawn_muzzle_flashes, despawn_stray_bullets}, inspect::inspect_firearm, equipped_gun_collisions::update_gun_position_on_collision, update_bullet::update_bullet},
player::{ player::{
camera_player_sync::{ camera_player_sync::{
follow_cursor_with_camera, update_camera_vertical_position, MouseMovementSettings, follow_cursor_with_camera, update_camera_vertical_position, MouseMovementSettings,
@ -62,6 +62,7 @@ pub fn load_scene(application: &mut App) {
application.add_systems(Update, inspect_firearm); application.add_systems(Update, inspect_firearm);
application.add_systems(Update, collision_handler); application.add_systems(Update, collision_handler);
application.add_systems(Update, update_gun_position_on_collision); application.add_systems(Update, update_gun_position_on_collision);
application.add_systems(Update, update_bullet);
//application.add_systems(Update, animate_player); //application.add_systems(Update, animate_player);
//application.add_systems(Update, register_bullet_hits); //application.add_systems(Update, register_bullet_hits);

View File

@ -5,7 +5,7 @@ use bevy_inspector_egui::bevy_egui::EguiPlugin;
use crate::{ use crate::{
comps::core::{markers::{ comps::core::{markers::{
holdable::{HoldableObjectData, InPlayerHands}, holdable::{HoldableObjectData, InPlayerHands},
player::{Player, PlayerData, PlayerHand}, inspect_screen::InspectScreenSlotUiMarker, player::{Player, PlayerData, PlayerHand}, inspect_screen::InspectScreenSlotUiMarker, bullet::BulletMarker,
}, weapons::{firearm_data::FirearmData, magazine_data::MagazineData, caliber::Caliber, firearm::Firearm, spray_pattern::FirearmSprayPattern, firearm_state::FirearmState}}, }, weapons::{firearm_data::FirearmData, magazine_data::MagazineData, caliber::Caliber, firearm::Firearm, spray_pattern::FirearmSprayPattern, firearm_state::FirearmState}},
logic::core::{ logic::core::{
guns::player_firing::PlayerFiringInfo, guns::player_firing::PlayerFiringInfo,
@ -39,6 +39,7 @@ impl Plugin for MainEditorUiPlugin {
.register_type::<PlayerValuesState>() .register_type::<PlayerValuesState>()
.register_type::<FirearmData>() .register_type::<FirearmData>()
.register_type::<MagazineData>() .register_type::<MagazineData>()
.register_type::<BulletMarker>()
.register_type::<HoldableObjectData>() .register_type::<HoldableObjectData>()
.register_type::<InPlayerHands>() .register_type::<InPlayerHands>()
.register_type::<PlayerData>() .register_type::<PlayerData>()