godotengine / godotengine/godot-docs
Applying root rotation to basis provides accurate translated motion
- Dominant language
- reStructuredText
- Stars
- 5.7k
- Forks
- 3.8k
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 25
Description
**Your Godot version:**
4.4.1.stable
**enhancement description:**
The existing code for applying root motion makes use of quaternions. The quaternion obtained from get_root_motion_rotation_accumulator() is multiplied with the node3d's quaternion and this product is multiplied with vector3 obtained from get_root_motion_position() method. This gives a fairly accurate motion.
However, by simply converting the quaternions to basis we get a perfect 1:1 motion.
Change the quaternion obtained from accumulator to basis and multiply it with node3d's basis.
existing code:
```
func _process(delta):
if Input.is_action_just_pressed("animate"):
state_machine.travel("Animate")
set_quaternion(get_quaternion() * animation_tree.get_root_motion_rotation())
var velocity = (animation_tree.get_root_motion_rotation_accumulator().inverse() * get_quaternion()) * animation_tree.get_root_motion_position() / delta
set_velocity(velocity)
move_and_slide()
```
convert quaternion to basis
```
var velocity = (Basis(animation_tree.get_root_motion_rotation_accumulator().inverse()) * transform.basis) * animation_tree.get_root_motion_position()
```
**updated/proposed code**
```
func _process(delta):
if Input.is_action_just_pressed("animate"):
state_machine.travel("Animate")
set_quaternion(get_quaternion() * animation_tree.get_root_motion_rotation())
var velocity = (Basis(animation_tree.get_root_motion_rotation_accumulator().inverse()) * transform.basis) * animation_tree.get_root_motion_position()
set_velocity(velocity)
move_and_slide()
```
for only translation motion.
```
var velocity = transform.basis * animation_tree.get_root_motion_position()
```
*Important Note*
This has been tested on a hand rigged model imported(glb) from blender, I am unsure of result obtained from other software and may need testing.
**URL to the documentation page (if already existing):**
https://docs.godotengine.org/en/stable/classes/class_animationmixer.html
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.