Yep that's right. Sorry I had a typo, meant subtract. I blame my phone.
[Corona]How to make a bone follow a physics bounding Box
I'm trying to do the exact same thing in Unity. I think I'm close, but the bones always end up translated some fixed (but seemingly random) distance from the physics bounding box. I tried to reproduce some of the code/formulas from this thread. Unity has a built-in Matrix4x4 class, so I used that to build the matrix.
void UpdateBones(SkeletonAnimation skeletonAnimation) {
for(int i=0; i < bones.Count; i++) {
Bone bone = skeleton.bones[i];
Bone boneParent = skeleton.bones[i].parent;
if (boneParent == null)
boneParent = bone;
Matrix4x4 matrix = new Matrix4x4();
matrix.SetRow (0, new Vector4(boneParent.M00, boneParent.M01, boneParent.worldX, 0));
matrix.SetRow (1, new Vector4(boneParent.M10, boneParent.M01, boneParent.worldY, 0));
matrix.SetRow (2, new Vector4(0, 0, 1, 0));
matrix.SetRow (3, new Vector4(0, 0, 0, 1));
float boundingBoxX = bones[i].transform.position.x; // This is the x coord of the GameObject that this bone corresponds to
float boundingBoxY = bones[i].transform.position.y;
bone.x = boundingBoxX * matrix.inverse[0,0] + boundingBoxY * matrix.inverse[0,1] + matrix.inverse[0,2];
bone.y = boundingBoxX * matrix.inverse[1,0] + boundingBoxY * matrix.inverse[1,1] + matrix.inverse[1,2];
}
}
Does anyone see what I'm doing wrong? I'm really wishing I paid more attention in Linear Algebra now.
I did try that. Thanks for the suggestion though. I'm sure I'll crack this eventually
Scrott, it looks ok. You may need to transpose your matrix if the row/column major doesn't match the code I linked.
HI!! i,m implementing this system in my game but i have troubles to interpret this function:
public Matrix3 setToRotation (Vector3 axis, float cos, float sin) {
float[] val = this.val;
float oc = 1.0f - cos;
val[M00] = oc * axis.x * axis.x + cos;
val[M10] = oc * axis.x * axis.y - axis.z * sin;
val[M20] = oc * axis.z * axis.x + axis.y * sin;
val[M01] = oc * axis.x * axis.y + axis.z * sin;
val[M11] = oc * axis.y * axis.y + cos;
val[M21] = oc * axis.y * axis.z - axis.x * sin;
val[M02] = oc * axis.z * axis.x - axis.y * sin;
val[M12] = oc * axis.y * axis.z + axis.x * sin;
val[M22] = oc * axis.z * axis.z + cos;
return this;
}
When i return this matrix, the val of (m00,m10,m01,m11) are too high .....maybe i´m wrong. This matrix is only for the prupose to take the m02 and m12 values and discard the rest??
This topic is 1.5 years old. What are you trying to do?
We are making a personal level editor with 3x3 matrices, all works successfuly at the moment, exceptly when the chained objects (in the parent matrix) are in negative scale... Im using Spine Pro for sprites.
I have to invert the matrix for negative scales?
Edit : sorry , my bad, i´m playing with negative angles, that is the problem.