fixed debug_lines build and transform nan issue

This commit is contained in:
Bram Buurlage 2022-09-20 15:56:20 +02:00
parent f28ca3192c
commit b695fac4bb
4 changed files with 35 additions and 14 deletions

6
CHANGELOG.MD Normal file
View File

@ -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

View File

@ -1,6 +1,6 @@
[package]
name = "bevy_mod_inverse_kinematics"
version = "0.1.0"
version = "0.1.1"
authors = ["Bram Buurlage <brambuurlage@gmail.com>"]
edition = "2021"
categories = ["game-engines", "graphics", "rendering"]

View File

@ -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

View File

@ -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<GlobalTransform, QueryEntityError> {
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)
}
}
}