diff --git a/CHANGELOG.MD b/CHANGELOG.MD new file mode 100644 index 0000000..902deed --- /dev/null +++ b/CHANGELOG.MD @@ -0,0 +1,6 @@ +# Version 0.1.0 +- Initial version + +# Version 0.1.1 +- Fixed build issue when using the debug_lines feature +- Fixed transforms giong to NaN when an exact match is found diff --git a/Cargo.toml b/Cargo.toml index 248c7b2..446a234 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bevy_mod_inverse_kinematics" -version = "0.1.0" +version = "0.1.1" authors = ["Bram Buurlage "] edition = "2021" categories = ["game-engines", "graphics", "rendering"] diff --git a/src/lib.rs b/src/lib.rs index d2dbe67..ff079ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,7 @@ pub struct IkConstraint { impl Plugin for InverseKinematicsPlugin { fn build(&self, app: &mut App) { #[cfg(feature = "debug_lines")] - app.add_plugin(DebugLinesPlugin::default()); + app.add_plugin(bevy_prototype_debug_lines::DebugLinesPlugin::default()); app.add_system_to_stage( CoreStage::PostUpdate, solver::inverse_kinematics_system diff --git a/src/solver.rs b/src/solver.rs index 27f97ca..c94e7e7 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -1,5 +1,5 @@ -use bevy::prelude::*; use bevy::ecs::query::QueryEntityError; +use bevy::prelude::*; #[cfg(feature = "debug_lines")] use bevy_prototype_debug_lines::{DebugLines, DebugLinesPlugin}; @@ -56,6 +56,9 @@ impl IkConstraint { let start = transforms.get(joints[self.chain_length])?.1.translation(); let pole_target = transforms.get(pole_target)?.1.translation(); + #[cfg(feature = "debug_lines")] + debug_lines.line_colored(start, target, 0.0, Color::WHITE); + let tangent = (target - start).normalize(); let axis = (pole_target - start).cross(tangent); let normal = tangent.cross(axis).normalize(); @@ -98,6 +101,25 @@ impl IkConstraint { #[cfg(feature = "debug_lines")] debug_lines: &mut DebugLines, ) -> Result { let (&transform, &global_transform) = transforms.get(chain[0])?; + #[cfg(feature = "debug_lines")] + { + let p = global_transform.translation(); + debug_lines.line_colored(p, global_transform.mul_vec3(Vec3::X * 0.1), 0.0, Color::RED); + debug_lines.line_colored( + p, + global_transform.mul_vec3(Vec3::Y * 0.1), + 0.0, + Color::GREEN, + ); + debug_lines.line_colored( + p, + global_transform.mul_vec3(Vec3::Z * 0.1), + 0.0, + Color::BLUE, + ); + debug_lines.line_colored(p, global_transform.mul_vec3(normal), 0.0, Color::YELLOW); + } + if chain.len() == 1 { return Ok(global_transform); } @@ -114,7 +136,9 @@ impl IkConstraint { let base = Quat::from_rotation_arc(normal.normalize(), Vec3::Z); - let forward = (target - from_position).normalize(); + let forward = (target - from_position) + .try_normalize() + .unwrap_or(pt.tangent); let up = forward.cross(pt.normal).normalize(); let right = up.cross(forward); let orientation = Mat3::from_cols(right, up, forward) * Mat3::from_rotation_z(pt.angle); @@ -129,15 +153,6 @@ impl IkConstraint { }; let translation = target - rotation.mul_vec3(normal); - #[cfg(feature = "debug_lines")] - { - let p = translation; - debug_lines.line_colored(p, p + rotation.mul_vec3(Vec3::X * 0.1), 0.0, Color::RED); - debug_lines.line_colored(p, p + rotation.mul_vec3(Vec3::Y * 0.1), 0.0, Color::GREEN); - debug_lines.line_colored(p, p + rotation.mul_vec3(Vec3::Z * 0.1), 0.0, Color::BLUE); - debug_lines.line_colored(p, p + rotation.mul_vec3(normal), 0.0, Color::YELLOW); - } - // recurse to target the parent towards the current translation let parent_global_transform = Self::solve_recursive( &chain[1..], @@ -167,4 +182,4 @@ impl IkConstraint { Ok(*global_transform) } -} \ No newline at end of file +}