rendering, physics, player and camera WIP

This commit is contained in:
Jonas H
2026-01-01 19:54:00 +01:00
commit 5d2eca0393
51 changed files with 8734 additions and 0 deletions

61
src/components/camera.rs Normal file
View File

@@ -0,0 +1,61 @@
use glam::Mat4;
#[derive(Clone, Copy)]
pub struct CameraComponent
{
pub fov: f32,
pub aspect: f32,
pub near: f32,
pub far: f32,
pub yaw: f32,
pub pitch: f32,
pub is_active: bool,
}
impl CameraComponent
{
pub fn new(aspect: f32) -> Self
{
Self {
fov: 45.0_f32.to_radians(),
aspect,
near: 0.1,
far: 100.0,
yaw: -135.0_f32.to_radians(),
pitch: -30.0_f32.to_radians(),
is_active: true,
}
}
pub fn projection_matrix(&self) -> Mat4
{
Mat4::perspective_rh(self.fov, self.aspect, self.near, self.far)
}
pub fn get_forward(&self) -> glam::Vec3
{
glam::Vec3::new(
self.yaw.cos() * self.pitch.cos(),
self.pitch.sin(),
self.yaw.sin() * self.pitch.cos(),
)
.normalize()
}
pub fn get_right(&self) -> glam::Vec3
{
self.get_forward().cross(glam::Vec3::Y).normalize()
}
pub fn get_forward_horizontal(&self) -> glam::Vec3
{
glam::Vec3::new(self.yaw.cos(), 0.0, self.yaw.sin()).normalize()
}
pub fn get_right_horizontal(&self) -> glam::Vec3
{
self.get_forward_horizontal()
.cross(glam::Vec3::Y)
.normalize()
}
}

View File

@@ -0,0 +1,32 @@
use glam::Vec3;
use crate::entity::EntityHandle;
#[derive(Clone, Copy)]
pub struct CameraFollowComponent
{
pub target_entity: EntityHandle,
pub offset: Vec3,
pub is_following: bool,
}
impl CameraFollowComponent
{
pub fn new(target_entity: EntityHandle) -> Self
{
Self {
target_entity,
offset: Vec3::ZERO,
is_following: false,
}
}
pub fn with_offset(target_entity: EntityHandle, offset: Vec3) -> Self
{
Self {
target_entity,
offset,
is_following: true,
}
}
}

9
src/components/input.rs Normal file
View File

@@ -0,0 +1,9 @@
use glam::Vec3;
#[derive(Clone, Default)]
pub struct InputComponent
{
pub move_direction: Vec3,
pub jump_pressed: bool,
pub jump_just_pressed: bool,
}

82
src/components/jump.rs Normal file
View File

@@ -0,0 +1,82 @@
use glam::Vec3;
use kurbo::CubicBez;
#[derive(Clone)]
pub struct JumpComponent
{
pub jump_config: JumpConfig,
}
impl JumpComponent
{
pub fn new() -> Self
{
Self {
jump_config: JumpConfig::default(),
}
}
}
#[derive(Clone, Copy)]
pub struct JumpConfig
{
pub jump_height: f32,
pub jump_duration: f32,
pub air_control_force: f32,
pub max_air_momentum: f32,
pub air_damping_active: f32,
pub air_damping_passive: f32,
pub jump_curve: CubicBez,
pub jump_context: JumpContext,
}
impl Default for JumpConfig
{
fn default() -> Self
{
Self {
jump_height: 2.0,
jump_duration: 0.15,
air_control_force: 10.0,
max_air_momentum: 8.0,
air_damping_active: 0.4,
air_damping_passive: 0.9,
jump_curve: CubicBez::new(
(0.0, 0.0),
(0.4, 0.75),
(0.7, 0.9),
(1.0, 1.0),
),
jump_context: JumpContext::default(),
}
}
}
#[derive(Default, Clone, Copy)]
pub struct JumpContext
{
pub in_progress: bool,
pub duration: f32,
pub execution_time: f32,
pub origin_height: f32,
pub normal: Vec3,
}
impl JumpContext
{
fn start(time: f32, current_height: f32, surface_normal: Vec3) -> Self
{
Self {
in_progress: false,
duration: 0.0,
execution_time: time,
origin_height: current_height,
normal: surface_normal,
}
}
pub fn stop(&mut self)
{
self.in_progress = false;
}
}

11
src/components/mesh.rs Normal file
View File

@@ -0,0 +1,11 @@
use std::rc::Rc;
use crate::mesh::Mesh;
use crate::render::Pipeline;
#[derive(Clone)]
pub struct MeshComponent
{
pub mesh: Rc<Mesh>,
pub pipeline: Pipeline,
}

16
src/components/mod.rs Normal file
View File

@@ -0,0 +1,16 @@
pub mod camera;
pub mod camera_follow;
pub mod input;
pub mod jump;
pub mod mesh;
pub mod movement;
pub mod physics;
pub mod player_tag;
pub mod state_machine;
pub use camera::CameraComponent;
pub use camera_follow::CameraFollowComponent;
pub use input::InputComponent;
pub use mesh::MeshComponent;
pub use movement::MovementComponent;
pub use physics::PhysicsComponent;

View File

@@ -0,0 +1,73 @@
use glam::Vec3;
use kurbo::CubicBez;
#[derive(Clone)]
pub struct MovementComponent
{
pub movement_config: MovementConfig,
}
impl MovementComponent
{
pub fn new() -> Self
{
Self {
movement_config: MovementConfig::new(),
}
}
}
#[derive(Clone)]
pub struct MovementConfig
{
pub walking_acceleration: f32,
pub walking_acceleration_duration: f32,
pub walking_acceleration_curve: CubicBez,
pub walking_damping: f32,
pub max_walking_speed: f32,
pub idle_damping: f32,
pub movement_context: MovementContext,
}
impl MovementConfig
{
pub fn new() -> Self
{
Self {
walking_acceleration: 250.0,
walking_acceleration_duration: 0.1,
walking_acceleration_curve: CubicBez::new(
(0.0, 0.0),
(0.5, 0.3),
(0.75, 0.9),
(1.0, 1.0),
),
walking_damping: 0.8,
max_walking_speed: 6.0,
idle_damping: 0.1,
movement_context: MovementContext::new(),
}
}
}
#[derive(Clone, Copy)]
pub struct MovementContext
{
pub forward_direction: Vec3,
pub is_floored: bool,
pub last_floored_time: u64,
pub surface_normal: Vec3,
}
impl MovementContext
{
pub fn new() -> Self
{
Self {
forward_direction: Vec3::Z,
is_floored: false,
last_floored_time: 0,
surface_normal: Vec3::Y,
}
}
}

View File

@@ -0,0 +1,8 @@
use rapier3d::prelude::{ColliderHandle, RigidBodyHandle};
#[derive(Copy, Clone, Debug)]
pub struct PhysicsComponent
{
pub rigidbody: RigidBodyHandle,
pub collider: Option<ColliderHandle>,
}

View File

@@ -0,0 +1,2 @@
#[derive(Copy, Clone, Debug)]
pub struct PlayerTag;

View File

@@ -0,0 +1 @@