fixed debug_lines build and transform nan issue
This commit is contained in:
parent
f28ca3192c
commit
b695fac4bb
6
CHANGELOG.MD
Normal file
6
CHANGELOG.MD
Normal 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
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bevy_mod_inverse_kinematics"
|
name = "bevy_mod_inverse_kinematics"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
authors = ["Bram Buurlage <brambuurlage@gmail.com>"]
|
authors = ["Bram Buurlage <brambuurlage@gmail.com>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
categories = ["game-engines", "graphics", "rendering"]
|
categories = ["game-engines", "graphics", "rendering"]
|
||||||
|
@ -29,7 +29,7 @@ pub struct IkConstraint {
|
|||||||
impl Plugin for InverseKinematicsPlugin {
|
impl Plugin for InverseKinematicsPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
#[cfg(feature = "debug_lines")]
|
#[cfg(feature = "debug_lines")]
|
||||||
app.add_plugin(DebugLinesPlugin::default());
|
app.add_plugin(bevy_prototype_debug_lines::DebugLinesPlugin::default());
|
||||||
app.add_system_to_stage(
|
app.add_system_to_stage(
|
||||||
CoreStage::PostUpdate,
|
CoreStage::PostUpdate,
|
||||||
solver::inverse_kinematics_system
|
solver::inverse_kinematics_system
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use bevy::prelude::*;
|
|
||||||
use bevy::ecs::query::QueryEntityError;
|
use bevy::ecs::query::QueryEntityError;
|
||||||
|
use bevy::prelude::*;
|
||||||
#[cfg(feature = "debug_lines")]
|
#[cfg(feature = "debug_lines")]
|
||||||
use bevy_prototype_debug_lines::{DebugLines, DebugLinesPlugin};
|
use bevy_prototype_debug_lines::{DebugLines, DebugLinesPlugin};
|
||||||
|
|
||||||
@ -56,6 +56,9 @@ impl IkConstraint {
|
|||||||
let start = transforms.get(joints[self.chain_length])?.1.translation();
|
let start = transforms.get(joints[self.chain_length])?.1.translation();
|
||||||
let pole_target = transforms.get(pole_target)?.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 tangent = (target - start).normalize();
|
||||||
let axis = (pole_target - start).cross(tangent);
|
let axis = (pole_target - start).cross(tangent);
|
||||||
let normal = tangent.cross(axis).normalize();
|
let normal = tangent.cross(axis).normalize();
|
||||||
@ -98,6 +101,25 @@ impl IkConstraint {
|
|||||||
#[cfg(feature = "debug_lines")] debug_lines: &mut DebugLines,
|
#[cfg(feature = "debug_lines")] debug_lines: &mut DebugLines,
|
||||||
) -> Result<GlobalTransform, QueryEntityError> {
|
) -> Result<GlobalTransform, QueryEntityError> {
|
||||||
let (&transform, &global_transform) = transforms.get(chain[0])?;
|
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 {
|
if chain.len() == 1 {
|
||||||
return Ok(global_transform);
|
return Ok(global_transform);
|
||||||
}
|
}
|
||||||
@ -114,7 +136,9 @@ impl IkConstraint {
|
|||||||
|
|
||||||
let base = Quat::from_rotation_arc(normal.normalize(), Vec3::Z);
|
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 up = forward.cross(pt.normal).normalize();
|
||||||
let right = up.cross(forward);
|
let right = up.cross(forward);
|
||||||
let orientation = Mat3::from_cols(right, up, forward) * Mat3::from_rotation_z(pt.angle);
|
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);
|
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
|
// recurse to target the parent towards the current translation
|
||||||
let parent_global_transform = Self::solve_recursive(
|
let parent_global_transform = Self::solve_recursive(
|
||||||
&chain[1..],
|
&chain[1..],
|
||||||
@ -167,4 +182,4 @@ impl IkConstraint {
|
|||||||
|
|
||||||
Ok(*global_transform)
|
Ok(*global_transform)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user