Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
395 changes: 390 additions & 5 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"ZeroEngine/crates/ze_core",
"ZeroEngine/crates/ze_input",
"ZeroEngine/crates/ze_log",
"ZeroEngine/crates/ze_physics",
"ZeroEngine/crates/ze_renderer",
"ZeroEngine/crates/ze_ecs",
"ZeroEngine/crates/zeroengine",
Expand Down
1 change: 1 addition & 0 deletions ZeroEngine/crates/ze_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ze_core = { path = "../ze_core" }
ze_input = { path = "../ze_input" }
ze_log = { path = "../ze_log" }
ze_ecs = { path = "../ze_ecs" }
ze_physics = { path = "../ze_physics" }
winit = "0.30.13"
ze_renderer = { version = "0.1.0", path = "../ze_renderer" }
tokio = { version = "1.52.3", features = ["full"] }
2 changes: 2 additions & 0 deletions ZeroEngine/crates/ze_app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use winit::{
use ze_core::{ResourceManager, Result, bail};
use ze_ecs::{Scene, System, registry};
use ze_input::*;
use ze_physics::PhysicsSystem;
use ze_renderer::{EditorCameraSystem, RenderSystem, register_renderer_components};

const DEFAULT_SCENE_NAME: &str = "main";
Expand Down Expand Up @@ -130,6 +131,7 @@ pub fn load_main_scene(resources: &ResourceManager) -> Result<Scene> {
)
})?;
scene.add_system(EditorCameraSystem::new());
scene.add_system(PhysicsSystem::new());
scene.add_system(RenderSystem::new());
Ok(scene)
}
Expand Down
120 changes: 119 additions & 1 deletion ZeroEngine/crates/ze_ecs/src/components.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use shipyard::{Component, EntityId};
use ze_core::{Quat, Vec3};
use ze_core::{Quat, Vec2, Vec3};

#[derive(Component, Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct Name {
Expand Down Expand Up @@ -47,3 +47,121 @@ pub struct Children {

#[derive(Component, Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct Inactive;

#[derive(Component, Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct RigidBody {
pub body_type: RigidBodyType,
#[serde(default = "default_true")]
pub use_gravity: bool,
pub gravity_scale: f32,
pub linear_damping: f32,
pub angular_damping: f32,
#[serde(default)]
pub mass: Option<f32>,
#[serde(default)]
pub freeze_position_x: bool,
#[serde(default)]
pub freeze_position_y: bool,
#[serde(default)]
pub freeze_rotation_x: bool,
#[serde(default)]
pub freeze_rotation_y: bool,
#[serde(default)]
pub freeze_rotation_z: bool,
#[serde(default)]
pub collision_detection: CollisionDetection,
}

#[derive(Component, Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct PhysicsSettings {
#[schemars(with = "[f32; 2]")]
pub gravity: Vec2,
pub enable_debug_draw: bool,
}

impl Default for PhysicsSettings {
fn default() -> Self {
Self {
gravity: Vec2::new(0.0, -9.81),
enable_debug_draw: false,
}
}
}

impl Default for RigidBody {
fn default() -> Self {
Self {
body_type: RigidBodyType::Dynamic,
use_gravity: true,
gravity_scale: 1.0,
linear_damping: 0.05,
angular_damping: 0.0,
mass: None,
freeze_position_x: false,
freeze_position_y: false,
freeze_rotation_x: false,
freeze_rotation_y: false,
freeze_rotation_z: false,
collision_detection: CollisionDetection::Discrete,
}
}
}

fn default_true() -> bool { true }

#[derive(Debug, Clone, Copy, Serialize, Deserialize, JsonSchema)]
pub enum RigidBodyType {
Static,
Dynamic,
KinematicPositionBased,
KinematicVelocityBased,
}

#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
pub enum CollisionDetection {
#[default]
Discrete,
Continuous,
}

#[derive(Component, Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct Collider {
pub shape: ColliderShape,
pub friction: f32,
pub restitution: f32,
pub density: f32,
pub is_sensor: bool,
}

impl Default for Collider {
fn default() -> Self {
Self {
shape: ColliderShape::Box {
half_extents: Vec2::splat(0.5),
},
friction: 0.5,
restitution: 0.0,
density: 1.0,
is_sensor: false,
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub enum ColliderShape {
Box {
#[schemars(with = "[f32; 2]")]
half_extents: Vec2,
},
Circle {
radius: f32,
},
Capsule {
half_height: f32,
radius: f32,
},
ConvexPolygon {
#[schemars(with = "Vec<[f32; 2]>")]
points: Vec<Vec2>,
},
}
5 changes: 4 additions & 1 deletion ZeroEngine/crates/ze_ecs/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use shipyard::{Component, EntitiesView, EntityId, World};
use ze_core::{Result, anyhow};

use crate::{
components::{Inactive, Name, Tag, Transform},
components::{Collider, Inactive, Name, PhysicsSettings, RigidBody, Tag, Transform},
definitions::SaveFile,
entity::Entity,
registry::ComponentRegistry,
Expand Down Expand Up @@ -119,6 +119,9 @@ impl Scene {
registry.register::<Tag>("ze.tag");
registry.register::<Inactive>("ze.inactive");
registry.register::<Transform>("ze.transform");
registry.register::<RigidBody>("ze.physics_2d.rigidbody");
registry.register::<Collider>("ze.physics_2d.collider");
registry.register::<PhysicsSettings>("ze.physics_2d.settings");
}
}

Expand Down
9 changes: 9 additions & 0 deletions ZeroEngine/crates/ze_physics/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ze_physics"
version = "0.1.0"
edition = "2024"

[dependencies]
rapier2d = "0.29"
ze_core = { version = "0.1.0", path = "../ze_core" }
ze_ecs = { version = "0.1.0", path = "../ze_ecs" }
Loading