started cam

This commit is contained in:
Back777space
2025-04-05 16:46:08 +02:00
parent 9ff0fcc3cb
commit 96b3f15292
2 changed files with 64 additions and 14 deletions

58
src/camera.rs Normal file
View File

@@ -0,0 +1,58 @@
use bevy::{input::mouse::MouseMotion, prelude::*, input::mouse::a};
#[derive(Debug, Component)]
struct Player;
#[derive(Debug, Component, Deref, DerefMut)]
struct CameraSensitivity(Vec2);
impl Default for CameraSensitivity {
fn default() -> Self {
Self(
Vec2::new(0.003, 0.002),
)
}
}
#[derive(Debug, Component)]
struct WorldModelCamera;
use std::f32::consts::FRAC_PI_2;
pub fn move_camera(
mut motion_evr: EventReader<MouseMotion>,
time: Res<Time>,
mut camera_query: Query<(&mut Transform, &mut Camera)>,
) {
let Ok((mut transform, camera_sensitivity)) = player.get_single_mut() else {
return;
};
let delta = accumulated_mouse_motion.delta;
if delta != Vec2::ZERO {
// Note that we are not multiplying by delta_time here.
// The reason is that for mouse movement, we already get the full movement that happened since the last frame.
// This means that if we multiply by delta_time, we will get a smaller rotation than intended by the user.
// This situation is reversed when reading e.g. analog input from a gamepad however, where the same rules
// as for keyboard input apply. Such an input should be multiplied by delta_time to get the intended rotation
// independent of the framerate.
let delta_yaw = -delta.x * camera_sensitivity.x;
let delta_pitch = -delta.y * camera_sensitivity.y;
let (yaw, pitch, roll) = transform.rotation.to_euler(EulerRot::YXZ);
let yaw = yaw + delta_yaw;
// If the pitch was ±¹⁄₂ π, the camera would look straight up or down.
// When the user wants to move the camera back to the horizon, which way should the camera face?
// The camera has no way of knowing what direction was "forward" before landing in that extreme position,
// so the direction picked will for all intents and purposes be arbitrary.
// Another issue is that for mathematical reasons, the yaw will effectively be flipped when the pitch is at the extremes.
// To not run into these issues, we clamp the pitch to a safe range.
const PITCH_LIMIT: f32 = FRAC_PI_2 - 0.01;
let pitch = (pitch + delta_pitch).clamp(-PITCH_LIMIT, PITCH_LIMIT);
transform.rotation = Quat::from_euler(EulerRot::YXZ, yaw, pitch, roll);
}
}

View File

@@ -1,26 +1,18 @@
use bevy::prelude::*;
use bevy::{prelude::*};
use blenvy::{BlenvyPlugin, BlueprintInfo, GameWorldTag, HideUntilReady, SpawnBlueprint};
pub mod camera;
pub mod Camera;
fn main() -> AppExit {
App::new()
.add_plugins((DefaultPlugins, BlenvyPlugin::default()))
// We need to register components to make them visible to Blenvy
.register_type::<Player>()
.add_plugins((DefaultPlugins, BlenvyPlugin::default()))
.add_systems(Startup, setup)
.add_systems(Update, camera::move_camera)
.run()
}
#[derive(Component, Reflect)]
struct Player {
strength: f32,
perception: f32,
endurance: f32,
charisma: f32,
intelligence: f32,
agility: f32,
luck: f32,
}
fn setup(mut commands: Commands) {
commands.spawn((
BlueprintInfo::from_path("scenes/World.glb"),