Jump to content

Create bone animation programatically


Gugis
 Share

Recommended Posts

I'm trying to create bone animation programatically which will be used for interpolation between two animations.

My code:

var bone = skeleton.bones[0];var bone_animation = bone.animations[0];var current_frame = Math.round(bone_animation.currentFrame);if(!current_frame) current_frame = 0;var animationArm = new BABYLON.Animation("myAnimation", "_matrix", 30, BABYLON.Animation.ANIMATIONTYPE_MATRIX);animationArm.setKeys([    {frame: 0, value: bone_animation._keys[current_frame].value},    {frame: 30, value: bone_animation._keys[100].value}]);bone.animations.push(animationArm);animationArm.animate(0, 0, 30, true, 1);

And i get this error: Uncaught TypeError: Cannot set property '_matrix' of undefined

Can someone tell me what i'm doing wrong. Or give some tips how interpollation between two skeletal animations can be achieved.

 

Thanks :)

Link to comment
Share on other sites

Hello!

 

can you try with this:

var bone = skeleton.bones[0];var bone_animation = bone.animations[0];var current_frame = Math.round(bone_animation.currentFrame);if(!current_frame) current_frame = 0;var animationArm = new BABYLON.Animation("myAnimation", "_matrix", 30, BABYLON.Animation.ANIMATIONTYPE_MATRIX);animationArm.setKeys([    {frame: 0, value: bone_animation._keys[current_frame].value},    {frame: 30, value: bone_animation._keys[100].value}]);bone.animations.push(animationArm);scene.beginAnimation(bone, 0, 30, true);
Link to comment
Share on other sites

Hi Deltakosh :) No error, but nothing happens. I guess problem is that bone has two animations (first is original skeletal animation and second is created programatically) and beginAnimation function tries to play them both. I need to find solution how to ignore the first one animation. Well maybe I'm wrong, that's just my speculations.

Link to comment
Share on other sites

Can you add more keys in your animation ? By default matrices are not interpolated

Wow. It works!!!! Thank you :) I don't know if I am doing this right, but at least something is working :D

My code looks like that now:

			var player = scene.getMeshByName('player');			var bone = get_bone_by_name(player.skeleton, 'LeftShoulder');			var bone_animation = bone.animations[0];			var current_frame = Math.round(bone_animation.currentFrame);			if(!current_frame)			current_frame = 0;			var animationArm = new BABYLON.Animation("myAnimation", "_matrix", 30, BABYLON.Animation.ANIMATIONTYPE_MATRIX);			var steps = new Float32Array(16);			// Generate single step values			for(var n in bone_animation._keys[current_frame].value.m) {				var s_val = bone_animation._keys[current_frame].value.m[n];				var e_val = bone_animation._keys[90].value.m[n];				var val = (e_val - s_val) / 30;				steps[n] = val;			}			// Generate keys			var keys = [];			for(var i = 0; i <= 30; i++){				var arr = new Float32Array(16);				for(var n in steps) {					arr[n] = bone_animation._keys[current_frame].value.m[n] + (i * steps[n]);				}				var obj = new BABYLON.Matrix();				obj.m = arr;				var key = {frame: i, value: obj};				keys.push(key);			}			animationArm.setKeys(keys);			bone.animations.push(animationArm);			scene.beginAnimation(bone, 0, 30, false, 1, function(){				// Remove interpolation animation				bone.animations.pop();				// Continue looped animation				scene.beginAnimation(bone, 90, 105, true);			});
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...