switch to using bevy_ballistic

This commit is contained in:
2025-04-01 16:00:06 +08:00
parent 1fa1b110db
commit 7c9249fa8f
3 changed files with 13 additions and 46 deletions

View File

@@ -12,6 +12,7 @@ use crate::{
};
use avian3d::prelude::*;
use bevy::{pbr::NotShadowCaster, prelude::*};
use bevy_ballistic::launch_velocity;
use bevy_sprite3d::{Sprite3dBuilder, Sprite3dParams};
use std::f32::consts::PI;
@@ -47,52 +48,6 @@ fn setup(mut commands: Commands, assets: Res<GameAssets>, mut sprite_params: Spr
});
}
fn launch_velocity(
start_pos: Vec3,
target_pos: Vec3,
initial_velocity: f32,
gravity: f32,
) -> Option<Vec3> {
// Calculate displacement
let delta = target_pos - start_pos;
// Calculate horizontal distance
let horizontal_dist = (delta.x * delta.x + delta.z * delta.z).sqrt();
// Calculate terms for the quadratic formula
let v_squared = initial_velocity * initial_velocity;
let discriminant = v_squared * v_squared
- gravity * (gravity * horizontal_dist * horizontal_dist + 2.0 * delta.y * v_squared);
if discriminant < 0.0 {
return None;
}
// Calculate pitch angles
let term1 = v_squared / (gravity * horizontal_dist);
let term2 = discriminant.sqrt() / (gravity * horizontal_dist);
// let pitch_high = (term1 + term2).atan();
let pitch_low = (term1 - term2).atan();
// Calculate yaw angle
let yaw = delta.z.atan2(delta.x);
let pitch = pitch_low;
let dir_x = pitch.cos() * yaw.cos();
let dir_y = pitch.sin();
let dir_z = pitch.cos() * yaw.sin();
let result = Vec3::new(
initial_velocity * dir_x,
initial_velocity * dir_y,
initial_velocity * dir_z,
);
Some(result)
}
fn on_trigger_state(
trigger: Trigger<TriggerState>,
mut commands: Commands,
@@ -135,6 +90,7 @@ fn on_trigger_state(
SPEED,
9.81,
)
.map(|(low, _)| low)
.unwrap()
} else {
rotation.mul_quat(Quat::from_rotation_y(-PI / 2.))