Transforming bones
In some games you may want to change transformation of a specific bone with code. For example, in a shooter game you may want to point a character's hand towards its target. In a RPG game, you may want to turn heads of NPS towards the player when they are interacting with each other.
This can be achieved with methods set_node_rotation and set_node_position of BBMOD_AnimationPlayer. Both of these methods require the ID of the bone that you want to change the transformation of. Bone IDs can be found either in the _log.txt
file that is output alongside models converted with BBMOD CLI or BBMOD GUI, or they can be retrieved in code with method BBMOD_Model.find_node_id.
Tip: Use method
find_node_id
only once for each bone and store the ID into a variable for better performance.
Method set_node_rotation
takes a BBMOD_Quaternion, which describes a rotation in a 3D space. Explaining exactly how quaternions work is outside of the scope of this tutorial, but luckily BBMOD wraps quaternions into easy to use functions. To create a rotation around a specific axis, you can use the FromAxisAngle method.
/// @desc Create event
armRightBoneID = modCharacter.find_node_id("ArmRight");
/// @desc Step event
var _rotation = new BBMOD_Quaternion()
.FromAxisAngle(new BBMOD_Vec3(0, 1, 0), directionUp);
animationPlayer.set_node_rotation(armRightBoneID, _rotation);
animationPlayer.update(delta_time);
The code above creates a rotation by directionUp
(in degrees) around the Y axis and uses it for the "ArmRight" bone.
Note: Bone transformations are relative to parent bones! Rotation in the last example is around the Y axis in the parent bone's space, not world space!
It is also required that you call the set_node_rotation
or set_node_position
method before you update the animation player! If you would like to unset the bone transformation, you can do so by calling the method again with undefined
as an argument:
// Unsets custom bone rotation:
animationPlayer.set_node_rotation(armRightBoneID, undefined);