Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rasmusgo/parent hierarchy #441

Closed
wants to merge 5 commits into from
Closed
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
54 changes: 25 additions & 29 deletions benchmarks/stress-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ pub mod systems;

use hotham::{
asset_importer::{self, add_model_to_world},
components::{GlobalTransform, LocalTransform, Mesh, Visible},
components::{GlobalTransform, Mesh, Visible},
contexts::RenderContext,
glam::{EulerRot, Quat, Vec3},
glam::{vec3, Affine3A, EulerRot, Quat, Vec3},
hecs::{With, World},
rendering::{
light::Light,
Expand All @@ -19,7 +19,7 @@ use hotham::{
},
systems::{
animation_system, debug::debug_system, grabbing_system, hands_system, physics_system,
rendering::rendering_system, skinning::skinning_system, update_global_transform_system,
rendering::rendering_system, skinning::skinning_system,
update_global_transform_with_parent_system,
},
xr, Engine, HothamResult, TickData,
Expand Down Expand Up @@ -153,8 +153,8 @@ fn init(engine: &mut Engine, test: &StressTest) -> HashMap<String, World> {
for _ in 0..20 {
let e = add_model_to_world("Damaged Helmet", &models, world, None)
.expect("Could not find cube?");
let mut t = world.get::<&mut LocalTransform>(e).unwrap();
t.rotation = Quat::from_axis_angle(Vec3::X, std::f32::consts::FRAC_PI_2);
let mut t = world.get::<&mut GlobalTransform>(e).unwrap();
t.0 = Affine3A::from_axis_angle(Vec3::X, std::f32::consts::FRAC_PI_2);
}
models
}
Expand Down Expand Up @@ -249,7 +249,6 @@ fn tick(tick_props: &mut TickProps, tick_data: TickData) {
debug_system(engine);

animation_system(engine);
update_global_transform_system(engine);
update_global_transform_with_parent_system(engine);
skinning_system(engine);
}
Expand Down Expand Up @@ -290,18 +289,23 @@ fn model_system(
}

fn rotate_models(world: &mut World, total_time: f32) {
for (_, local_transform) in world.query_mut::<With<&mut LocalTransform, &Mesh>>() {
local_transform.rotation = Quat::from_euler(
EulerRot::XYZ,
90.0_f32.to_radians(),
total_time.sin() * 2.,
0.,
for (_, global_transform) in world.query_mut::<With<&mut GlobalTransform, &Mesh>>() {
let (scale, _, translation) = global_transform.to_scale_rotation_translation();
global_transform.0 = Affine3A::from_scale_rotation_translation(
scale,
Quat::from_euler(
EulerRot::XYZ,
90.0_f32.to_radians(),
total_time.sin() * 2.,
0.,
),
translation,
);
}
}

fn rearrange_models(world: &mut World) {
let query = world.query_mut::<With<&mut LocalTransform, &Mesh>>();
let query = world.query_mut::<With<&mut GlobalTransform, &Mesh>>();
let query_iter = query.into_iter();
let num_models = query_iter.len() as f32;

Expand All @@ -311,17 +315,17 @@ fn rearrange_models(world: &mut World) {
let mut row = 0;
let mut column = 0;

for (_, local_transform) in query_iter {
for (_, global_transform) in query_iter {
if column >= column_size {
column = 0;
row += 1;
}

local_transform.translation.x = (column as f32) - half_column_size;
local_transform.translation.y = (row as f32) - 0.5;
local_transform.translation.z = -4.0;

local_transform.scale = Vec3::splat(scale);
global_transform.0 = Affine3A::from_scale_rotation_translation(
Vec3::splat(scale),
Quat::IDENTITY,
vec3((column as f32) - half_column_size, (row as f32) - 0.5, -4.0),
);

column += 1;
}
Expand All @@ -344,17 +348,9 @@ fn create_mesh(render_context: &mut RenderContext, world: &mut World) {
render_context,
);
update_mesh(1, &mesh, render_context);
let local_transform = LocalTransform {
translation: [0., 1., -1.].into(),
..Default::default()
};
let global_transform = GlobalTransform::from_translation(vec3(0., 1., -1.));

world.spawn((
Visible {},
mesh,
local_transform,
GlobalTransform::default(),
));
world.spawn((Visible {}, mesh, global_transform));
}

fn update_mesh(step: usize, mesh: &Mesh, render_context: &mut RenderContext) {
Expand Down
14 changes: 7 additions & 7 deletions benchmarks/stress-test/src/systems/cube.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use hotham::{
asset_importer::{add_model_to_world, Models},
components::LocalTransform,
glam::Vec3,
components::GlobalTransform,
glam::{Mat3A, Vec3},
hecs::World,
};

Expand All @@ -16,11 +16,11 @@ pub fn setup_cubes(world: &mut World, resolution: usize, models: &Models) {
for row in 0..resolution {
for column in 0..resolution {
let c = add_model_to_world("Cube", models, world, None).unwrap();
let mut t = world.get::<&mut LocalTransform>(c).unwrap();
t.scale = scale;
t.translation.y = floor as f32 / scale_factor;
t.translation.x = column as f32 / scale_factor - x_offset;
t.translation.z = row as f32 / scale_factor - half_resolution - 2.0;
let mut t = world.get::<&mut GlobalTransform>(c).unwrap();
t.0.matrix3 = Mat3A::from_diagonal(scale);
t.0.translation.y = floor as f32 / scale_factor;
t.0.translation.x = column as f32 / scale_factor - x_offset;
t.0.translation.z = row as f32 / scale_factor - half_resolution - 2.0;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"swapchain",
"swapchains",
"Swatinem",
"Teleporting",
"thiserror",
"thumbstick",
"tonemap",
Expand Down
15 changes: 9 additions & 6 deletions examples/complex-scene/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use hotham::{
components::{
hand::Handedness,
physics::{BodyType, SharedShape},
Collider, Grabbable, LocalTransform, RigidBody,
Collider, GlobalTransform, Grabbable, RigidBody,
},
glam,
hecs::World,
na,
systems::{
animation_system, debug::debug_system, grabbing_system, hands::add_hand, hands_system,
physics_system, rendering::rendering_system, skinning::skinning_system,
update_global_transform_system, update_global_transform_with_parent_system,
update_global_transform_with_parent_system,
},
xr, Engine, HothamResult, TickData,
};
Expand Down Expand Up @@ -44,7 +44,6 @@ fn tick(tick_data: TickData, engine: &mut Engine, state: &mut State) {
physics_system(engine);
animation_system(engine);
navigation_system(engine, state);
update_global_transform_system(engine);
update_global_transform_with_parent_system(engine);
skinning_system(engine);
debug_system(engine);
Expand Down Expand Up @@ -108,9 +107,13 @@ fn add_helmet(
.expect("Could not find Damaged Helmet");

{
let mut local_transform = world.get::<&mut LocalTransform>(helmet).unwrap();
local_transform.translation = translation;
local_transform.scale = [0.5, 0.5, 0.5].into();
let mut global_transform = world.get::<&mut GlobalTransform>(helmet).unwrap();
let (_, rotation, _) = global_transform.0.to_scale_rotation_translation();
global_transform.0 = glam::Affine3A::from_scale_rotation_translation(
[0.5, 0.5, 0.5].into(),
rotation,
translation,
);
Comment on lines +110 to +116
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example that could be simplified by enforcing uniform scaling. However, glam does not have primitives for enforcing uniform scaling ("similarity transforms" aka "conformal") which makes it less worth pursuing. There is a long discussion thread in the glam repo which ends in sticking with only the affine transformation type.

}

let collider = Collider::new(SharedShape::ball(0.35));
Expand Down
10 changes: 3 additions & 7 deletions examples/crab-saber/src/game_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use hotham::{
hand::Handedness,
physics::{ActiveCollisionTypes, BodyType, SharedShape},
ui_panel::add_ui_panel_to_world,
Collider, GlobalTransform, LocalTransform, Pointer, RigidBody, SoundEmitter, Visible,
Collider, GlobalTransform, Pointer, RigidBody, SoundEmitter, Visible,
},
contexts::{audio_context::MusicTrack, physics_context::DEFAULT_COLLISION_GROUP, AudioContext},
hecs::{Entity, World},
Expand Down Expand Up @@ -224,10 +224,7 @@ pub fn pre_spawn_cube(world: &mut World, models: &HashMap<String, World>) {
};

let cube = add_model_to_world(model_name, models, world, None).unwrap();
let local_transform = LocalTransform {
translation: [0., -100., 0.].into(),
..Default::default()
};
let global_transform = GlobalTransform::from_translation([0., -100., 0.].into());

world.remove_one::<Visible>(cube).unwrap();
world
Expand All @@ -249,8 +246,7 @@ pub fn pre_spawn_cube(world: &mut World, models: &HashMap<String, World>) {
offset_from_parent: [0., 0.2, 0.].into(),
..Default::default()
},
local_transform,
GlobalTransform::from(local_transform),
global_transform,
),
)
.unwrap();
Expand Down
4 changes: 1 addition & 3 deletions examples/crab-saber/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use hotham::{
hecs::{Entity, World},
systems::{
audio_system, draw_gui_system, haptics_system, physics_system, pointers_system,
rendering_system, update_global_transform_system,
update_global_transform_with_parent_system,
rendering_system, update_global_transform_with_parent_system,
},
xr::{self, SessionState},
Engine, HothamResult, TickData,
Expand Down Expand Up @@ -52,7 +51,6 @@ fn tick(tick_data: TickData, engine: &mut Engine, game_context: &mut GameContext

// Update world
update_global_transform_with_parent_system(engine);
update_global_transform_system(engine);

// Sync world with output contexts
haptics_system(engine);
Expand Down
14 changes: 7 additions & 7 deletions examples/crab-saber/src/systems/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
use hotham::{
components::{
hand::Handedness, physics::Teleport, sound_emitter::SoundState, ui_panel::UIPanelButton,
Collider, LocalTransform, RigidBody, UIPanel, Visible,
Collider, GlobalTransform, RigidBody, UIPanel, Visible,
},
contexts::{AudioContext, HapticContext},
glam,
Expand Down Expand Up @@ -332,10 +332,10 @@ fn should_spawn_cube(last_spawn_time: Instant, beat_length: Duration) -> bool {
fn revive_cube(cube_entity: Entity, world: &mut World, song: &Song) {
// Update its position and velocity
{
let (local_transform, rigid_body) = world
.query_one_mut::<(&mut LocalTransform, &mut RigidBody)>(cube_entity)
let (global_transform, rigid_body) = world
.query_one_mut::<(&mut GlobalTransform, &mut RigidBody)>(cube_entity)
.unwrap();
let translation = &mut local_transform.translation;
let translation = &mut global_transform.0.translation;

let mut rng = thread_rng();
translation.x = CUBE_X_OFFSETS[rng.gen_range(0..4)];
Expand Down Expand Up @@ -448,13 +448,13 @@ mod tests {
assert_score_is(world, game_context, 0);

let mut q = world
.query::<(&Color, &RigidBody, &LocalTransform, &Collider)>()
.query::<(&Color, &RigidBody, &GlobalTransform, &Collider)>()
.with::<(&Visible, &Cube)>();
let mut i = q.iter();
assert_eq!(i.len(), 1);
let (_, (_, rigid_body, local_transform, _)) = i.next().unwrap();
let (_, (_, rigid_body, global_transform, _)) = i.next().unwrap();

let t = local_transform.translation;
let t = global_transform.0.translation;
assert!(
t.x == CUBE_X_OFFSETS[0]
|| t.x == CUBE_X_OFFSETS[1]
Expand Down
21 changes: 8 additions & 13 deletions examples/crab-saber/src/systems/sabers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use hotham::{
asset_importer::{add_model_to_world, Models},
components::{
physics::{BodyType, SharedShape},
stage, Collider, LocalTransform, RigidBody,
stage, Collider, GlobalTransform, RigidBody,
},
contexts::InputContext,
glam::Affine3A,
Expand Down Expand Up @@ -30,8 +30,8 @@ fn sabers_system_inner(world: &mut World, input_context: &InputContext) {
// Create a transform from local space to grip space.
let grip_from_local = Affine3A::from_rotation_translation(ROTATION_OFFSET, POSITION_OFFSET);

for (_, (color, local_transform)) in
world.query_mut::<With<(&Color, &mut LocalTransform), &Saber>>()
for (_, (color, global_transform)) in
world.query_mut::<With<(&Color, &mut GlobalTransform), &Saber>>()
{
// Get our the space and path of the hand.
let stage_from_grip = match color {
Expand All @@ -41,7 +41,7 @@ fn sabers_system_inner(world: &mut World, input_context: &InputContext) {

// Apply transform
let global_from_local = global_from_stage * stage_from_grip * grip_from_local;
local_transform.update_from_affine(&global_from_local);
global_transform.0 = global_from_local;
}
}

Expand Down Expand Up @@ -78,21 +78,16 @@ mod tests {

#[test]
fn test_sabers() {
use hotham::components::{GlobalTransform, LocalTransform};
use hotham::components::GlobalTransform;

let mut world = World::new();
let input_context = InputContext::testing();
let saber = world.spawn((
Color::Red,
Saber {},
LocalTransform::default(),
GlobalTransform::default(),
));
let saber = world.spawn((Color::Red, Saber {}, GlobalTransform::default()));
sabers_system_inner(&mut world, &input_context);

let local_transform = world.get::<&LocalTransform>(saber).unwrap();
let global_transform = world.get::<&GlobalTransform>(saber).unwrap();
approx::assert_relative_eq!(
local_transform.translation,
global_transform.0.translation,
[-0.2, 1.3258567, -0.47001815].into()
);
}
Expand Down
Loading
Loading