From 1377fcb4468dbc509340795ed414cfc10fbee315 Mon Sep 17 00:00:00 2001
From: Amorilia <amorilia@users.sourceforge.net>
Date: Sun, 11 Dec 2005 05:03:26 +0000
Subject: [PATCH] Bone matrix calculation function (Blender helper function)
 fixed.

---
 go.bat       |  1 +
 nif_math.cpp | 16 ++++++++++------
 nif_math.h   |  2 +-
 niflib.h     |  2 +-
 4 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/go.bat b/go.bat
index d7e3aa5a..a78ad592 100644
--- a/go.bat
+++ b/go.bat
@@ -1,3 +1,4 @@
 rem Windows Niflib Python wrapper compilation script (using the Visual C++ Toolkit & SWIG 1.3.27)
 "\Program Files\swigwin-1.3.27\swig.exe" -c++ -python -o py_wrap.cpp pyniflib.i
 cl /LD /EHsc /I"\Program Files\Python24\include" /I. /Fe"_niflib.dll" niflib.cpp docsys_extract.cpp NIF_Blocks.cpp NIF_IO.cpp nif_math.cpp py_wrap.cpp "\Program Files\Python24\libs\python24.lib"
+rem cl /EHsc /I. /Fe"test.exe" test.cpp niflib.cpp docsys_extract.cpp NIF_Blocks.cpp NIF_IO.cpp nif_math.cpp
diff --git a/nif_math.cpp b/nif_math.cpp
index c1bf2b61..0286a129 100644
--- a/nif_math.cpp
+++ b/nif_math.cpp
@@ -342,11 +342,15 @@ Matrix44 RotateMatrix44( Vector3 const & axis, float angle) {
 		0.0f,	0.0f,	0.0f,	1.0f);
 }
 
-Matrix44 BoneToMatrix44( Vector3 const & bone_vec, float roll ) {
+Matrix44 BoneToMatrix44( Vector3 const & bone_head, Vector3 const & bone_tail, float roll, float parent_len ) {
 	float theta;
+	Vector3 bone_vec;
+	bone_vec.x = bone_tail.x - bone_head.x;
+	bone_vec.y = bone_tail.y - bone_head.y;
+	bone_vec.z = bone_tail.z - bone_head.z;
 	Matrix44 bMatrix, rMatrix, result;
 
-	Vector3 target(1.0f, 0.0f, 0.0f);
+	Vector3 target(1.0f, 0.0f, 0.0f); // X-aligned
 	Vector3 nor = Normalize(bone_vec);
 	Vector3 axis = CrossProduct(target, nor);
 	if (DotProduct(axis, axis) > 0.0000000000001) {
@@ -355,7 +359,7 @@ Matrix44 BoneToMatrix44( Vector3 const & bone_vec, float roll ) {
 		bMatrix = RotateMatrix44(axis, theta);
 	} else {
 		float updown;
-		if (DotProduct(target, nor) > 0.0) updown =  1.0f;
+		if (DotProduct(target, nor) > 0.0f) updown =  1.0f;
 		else updown = -1.0f;
 		bMatrix = Matrix44(
 			updown, 0.0f, 0.0f, 0.0f,
@@ -365,8 +369,8 @@ Matrix44 BoneToMatrix44( Vector3 const & bone_vec, float roll ) {
 	};
 	rMatrix = RotateMatrix44(nor, roll);
 	result = MultMatrix44(bMatrix, rMatrix);
-	result[3][0] = bone_vec.x;
-	result[3][1] = bone_vec.y;
-	result[3][2] = bone_vec.z;
+	result[3][0] = bone_head.x + parent_len; // X-aligned
+	result[3][1] = bone_head.y;
+	result[3][2] = bone_head.z;
 	return result;
 }
diff --git a/nif_math.h b/nif_math.h
index 52504446..e2753f19 100644
--- a/nif_math.h
+++ b/nif_math.h
@@ -62,6 +62,6 @@ Vector3 Normalize(Vector3 const & a);
 float DotProduct(Vector3 const & a, Vector3 const & b);
 Vector3 CrossProduct(Vector3 const & a, Vector3 const & b); // = MultVector3
 float Angle(Vector3 const & a, Vector3 const & b);
-Matrix44 BoneToMatrix44( Vector3 const & bone_vec, float roll );
+Matrix44 BoneToMatrix44( Vector3 const & bone_head, Vector3 const & bone_tail, float roll, float parent_len );
 
 #endif
diff --git a/niflib.h b/niflib.h
index 89a16f30..d56518ca 100644
--- a/niflib.h
+++ b/niflib.h
@@ -426,7 +426,7 @@ struct Matrix44 {
 };
 
 // Bone calculation helper function from the nif_math module.
-Matrix44 BoneToMatrix44( Vector3 const & bone_vec, float roll );
+Matrix44 BoneToMatrix44( Vector3 const & bone_head, Vector3 const & bone_tail, float roll, float parent_len );
 
 struct Color {
 	float r, g, b, a;
-- 
GitLab