Skip to content

Commit 303bca0

Browse files
committed
improved typing + better error handling for ragdoll
1 parent 78deca3 commit 303bca0

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

fyrox-impl/src/scene/ragdoll.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ pub struct Ragdoll {
288288
base: Base,
289289
/// A handle to a main rigid body of the character to which this ragdoll belongs to. If set, the
290290
/// ragdoll will take control over the collider and will move it together with the root limb.
291-
pub character_rigid_body: InheritableVariable<Handle<Node>>,
291+
pub character_rigid_body: InheritableVariable<Handle<RigidBody>>,
292292
/// A flag, that defines whether the ragdoll is active or not. Active ragdoll enables limb rigid
293293
/// bodies and takes control over `character_rigid_body` (if set).
294294
pub is_active: InheritableVariable<bool>,
@@ -352,10 +352,7 @@ impl NodeTrait for Ragdoll {
352352
let mut new_lin_vel = None;
353353
let mut new_ang_vel = None;
354354
if *self.is_active && !self.prev_enabled {
355-
if let Ok(character_rigid_body) = ctx
356-
.nodes
357-
.try_get_component_of_type::<RigidBody>(*self.character_rigid_body)
358-
{
355+
if let Ok(character_rigid_body) = ctx.nodes.try_get(*self.character_rigid_body) {
359356
new_lin_vel = Some(character_rigid_body.lin_vel());
360357
new_ang_vel = Some(character_rigid_body.ang_vel());
361358
}
@@ -434,25 +431,24 @@ impl NodeTrait for Ragdoll {
434431
self.global_transform().try_inverse().unwrap_or_default();
435432

436433
// Sync transform of the physical body with respective bone.
437-
if let Ok(bone) = mbc.try_get(limb.bone) {
438-
let relative_transform = self_transform_inverse * bone.global_transform();
439-
440-
let position = Vector3::new(
441-
relative_transform[12],
442-
relative_transform[13],
443-
relative_transform[14],
444-
);
445-
let rotation = UnitQuaternion::from_matrix_eps(
446-
&relative_transform.basis(),
447-
f32::EPSILON,
448-
16,
449-
Default::default(),
450-
);
451-
limb_body
452-
.local_transform_mut()
453-
.set_position(position)
454-
.set_rotation(rotation);
455-
}
434+
let bone = mbc.try_get(limb.bone)?;
435+
let relative_transform = self_transform_inverse * bone.global_transform();
436+
437+
let position = Vector3::new(
438+
relative_transform[12],
439+
relative_transform[13],
440+
relative_transform[14],
441+
);
442+
let rotation = UnitQuaternion::from_matrix_eps(
443+
&relative_transform.basis(),
444+
f32::EPSILON,
445+
16,
446+
Default::default(),
447+
);
448+
limb_body
449+
.local_transform_mut()
450+
.set_position(position)
451+
.set_rotation(rotation);
456452
}
457453

458454
drop(limb_body);
@@ -475,10 +471,7 @@ impl NodeTrait for Ragdoll {
475471

476472
if let Ok(root_limb_body) = ctx.nodes.try_borrow(self.root_limb.bone) {
477473
let position = root_limb_body.global_position();
478-
if let Ok(character_rigid_body) = ctx
479-
.nodes
480-
.try_get_component_of_type_mut::<RigidBody>(*self.character_rigid_body)
481-
{
474+
if let Ok(character_rigid_body) = ctx.nodes.try_get_mut(*self.character_rigid_body) {
482475
if *self.is_active {
483476
character_rigid_body.set_lin_vel(Default::default());
484477
character_rigid_body.set_ang_vel(Default::default());
@@ -497,7 +490,7 @@ impl NodeTrait for Ragdoll {
497490
/// Ragdoll builder creates [`Ragdoll`] scene nodes.
498491
pub struct RagdollBuilder {
499492
base_builder: BaseBuilder,
500-
character_rigid_body: Handle<Node>,
493+
character_rigid_body: Handle<RigidBody>,
501494
is_active: bool,
502495
deactivate_colliders: bool,
503496
root_limb: Limb,
@@ -516,7 +509,7 @@ impl RagdollBuilder {
516509
}
517510

518511
/// Sets the desired character rigid body.
519-
pub fn with_character_rigid_body(mut self, handle: Handle<Node>) -> Self {
512+
pub fn with_character_rigid_body(mut self, handle: Handle<RigidBody>) -> Self {
520513
self.character_rigid_body = handle;
521514
self
522515
}

0 commit comments

Comments
 (0)