Hello, I'm trying to implement an IK system using Godot 4.3's SkeletonModifier3D.
Right now I'm only working on the code to stretch out an arm in the direction of the target if the target is too far.
Here is the code and an image. You can see the arm is all twisted, but I can't figure out what's going wrong with the positions.
I also can't figure out how to format the code here correctly :/
`
func stretch(target: Vector3) -> void:
// Reverse to start with anchor bone
var bones: Array = bones_in_chain.duplicate()
bones.reverse()
var lengths: Array = bone_lengths.duplicate()
lengths.reverse()
for i in bones.size():
// Get transform of current bone
var transform: Transform3D = skeleton.get_bone_global_pose(bones)
if i == 0:
// If its the first bone, then just set to anchor point
transform.origin = chain_anchor_pos
else:
var previous_bone: Vector3 = skeleton.get_bone_global_pose(bones[i-1]).origin
var vector_to_target = (target - previous_bone).normalized()
transform.origin = previous_bone + (vector_to_target * lengths[i])
skeleton.set_bone_global_pose(bones[i], transform)
`
