diff --git a/NinjaBone.py b/NinjaBone.py index aaaeb54..c90da8d 100644 --- a/NinjaBone.py +++ b/NinjaBone.py @@ -25,17 +25,13 @@ class NinjaBone: # { - def __init__(self, flag): # { - + def __init__(self, flag): self.flag = flag self.localMatrix = self.identity() self.worldMatrix = self.identity() return None - # } - - def indentity(self): # { - + def indentity(self): return [ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], @@ -43,30 +39,90 @@ class NinjaBone: # { [ 0, 0, 0, 1 ] ] - # } - def multiply(self, a, b): - - return None + tmp = self.identity() + for i in range(4): + for j in range(4): + t = 0.0 + for k in range(4): + t = t + (a[i][k] * b[k][j]) + tmp[i][j] = t + return tmp def multiplyLocal(self, factor): - + a = self.worldMatrix + b = factor + tmp = self.multiply(a, b) + self.worldMatrix = tmp return None def multiplyWorld(self, factor): + a = self.localMatrix + b = factor + tmp = self.multiply(a, b) + self.localMatrix = tmp + return None + def save(self): + for i in range(4): + for k in range(4): + self.worldMatrix[i][k] = self.localMatrix[i][k] return None def setScale(self, vec3): - + tmp = self.identity() + tmp[0][0] = vec3[0] + tmp[1][1] = vec3[1] + tmp[2][2] = vec3[2] + self.multiplyLocal(tmp) return None def setRotation(self, vec3, zxy_order): + if zxy_order: + z = vec3[0] + x = vec3[1] + y = vec3[2] + else: + x = vec3[0] + y = vec3[1] + z = vec3[2] + # rotate x-axis + tmp = self.identity() + c = math.cos(x) + s = math.sin(x) + tmp[1][1] = c; + tmp[1][2] = s; + tmp[2][1] = -s; + tmp[2][2] = c; + self.multiplyLocal(tmp) + + # rotate y-axis + tmp = self.identity() + c = math.cos(y) + s = math.sin(y) + tmp[0][0] = c; + tmp[0][2] = -s; + tmp[2][0] = s; + tmp[2][2] = c; + self.multiplyLocal(tmp) + + # rotate z-axis + tmp = self.identity() + c = math.cos(z) + s = math.sin(z) + tmp[0][0] = c; + tmp[0][1] = s; + tmp[1][0] = -s; + tmp[1][1] = c; + self.multiplyLocal(tmp) return None def setPosition(self, vec3): - + tmp = self.identity() + tmp[0][0] = vec3[0] + tmp[1][1] = vec3[1] + tmp[2][2] = vec3[2] + self.multiplyLocal(tmp) return None -# } diff --git a/NinjaModel.py b/NinjaModel.py index ed8a633..08a0afa 100644 --- a/NinjaModel.py +++ b/NinjaModel.py @@ -131,10 +131,11 @@ class NinjaModel: # { self.bone.setScale(scl) if ( (flags & 0x02) == 0): - self.bone.setScale(rot, flags & 0x20) + self.bone.setRotation(rot, flags & 0x20) if ( (flags & 0x01) == 0): - self.bone.setScale(pos) + self.bone.setPosition(pos) + self.bone.save() if(model_ofs) { self.file.seek(model_ofs + self.pof, 0)