# Relative movement of a bone in anim?

Hi,

I need to get the previous transform of a mesh bone to do some calculation on it every frame, to apply on my scene.

I want the relative move (position, rotation) of a given bone between each frame.
I am not sure if I want it local or global yet. Local could be easier for my needs though…

The code below ignores the rotation part, because I can’t make it work for translation only.
One step at a time I guess, but a matrix would be better.

``````let current_set = new Vector3();
let len = mesh.skeleton.bones.length;
let prev_ref_position: Vector3[] = [];

// prefill
for (let v of mesh.skeleton.bones) {
let current_set = new Vector3();
current_set.x = v.getTransformNode().position.x;
current_set.y = v.getTransformNode().position.y;
current_set.z = v.getTransformNode().position.z;
prev_ref_position.push(current_set);
}

scene.registerBeforeRender(() => {

for (let x = 0; x < len; ++x) {

let j = mesh.skeleton.bones[x].getTransformNode().position.clone();

current_set.x = j.x - prev_ref_position[x].x;
current_set.y = j.y - prev_ref_position[x].y;
current_set.z = j.z - prev_ref_position[x].z;

// do something with current_set
...
// set the previous value to the current mesh.skeleton.bones[x] position for next calculation
prev_ref_position[x].x = j.x;
prev_ref_position[x].y = j.y;
prev_ref_position[x].z = j.z;
}
});
``````

The issue is that current_set is always 0, as if `mesh.skeleton.bones[x].getTransformNode().position` didn’t have the time to change at the next tick.

Is there a way to make it work ?

Thanks

I think this thread may have some useful info: How weapon is attached to a rigged animation - #3 by gbz

Maybe `mesh.computeWorldMatrix(true)` needs to be called at the beginning of every `registerBeforeRender`?

Maybe position doesn’t change…but only the rotation. Did you try to compute a delta orientation between 2 frames to see if it changes from 1 frame to another?

Yes Cedric, position changes, the character has all its bones rolling

@regna Thanks,I will try that right now

world position can change while still having same local position. `getTransformNode().position` returns local position IIRC. Try `absolutePosition` instead

I just tried that and indeed, there is a change. Now, the issue is not to have different values, but having the substraction between the two returning 0 ! When those values are not the same…

to get delta rotation: multiply the bone quaternion by the inverse of previous frame. same for position. beware of space local/world. it’s easy to mix up difference spaces and have garbage at the end

1 Like

Thanks,

It was me not setting the prev array properly, using shallow copy (not using .clone()).
TS/Js are not my primary languages so I make some stupid mistakes at time.

Now, I get the diff properly.

Again, thanks to you all

1 Like