monxa

Untitled

Jan 31st, 2025
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @tool
  2. class_name PoleBoneConstraint extends GodotIKConstraint
  3. @export var active : bool = true
  4.  
  5. @export var pole_direction : Vector3
  6.  
  7. func apply(pos_parent_bone: Vector3, pos_bone: Vector3, pos_child_bone: Vector3) -> PackedVector3Array:
  8.     var result : PackedVector3Array = [pos_parent_bone, pos_bone, pos_child_bone]
  9.     if not active:
  10.         return result
  11.  
  12.     # Compute the primary bone direction
  13.     var dir = (pos_child_bone - pos_parent_bone).normalized()
  14.  
  15.     # Project pole direction onto the plane perpendicular to dir
  16.     var pole_direction_normalized = pole_direction.normalized()  # Ensure it's a unit vector
  17.     var pole_direction_projected = pole_direction_normalized - pole_direction_normalized.dot(dir) * dir
  18.  
  19.     # If the projected pole direction is too small, return early
  20.     if pole_direction_projected.length_squared() < 0.002:
  21.         return result
  22.  
  23.     # Normalize the projected pole direction
  24.     pole_direction_projected = pole_direction_projected.normalized()
  25.  
  26.     # Find the perpendicular foot of the bone to the line
  27.     var mid_point = foot_of_the_perpendicular(pos_bone, pos_parent_bone, pos_child_bone)
  28.  
  29.     # Adjust the bone position to follow the pole constraint
  30.     var mid_to_bone = pos_bone - mid_point
  31.     var bone_length = mid_to_bone.length()  # Store instead of computing twice
  32.     result[1] = mid_point + pole_direction_projected * bone_length
  33.  
  34.     return result
  35.  
  36. # Compute the foot of the perpendicular from a point to a line
  37. func foot_of_the_perpendicular(point_tip: Vector3, point_line1: Vector3, point_line2: Vector3) -> Vector3:
  38.     var bc: Vector3 = point_line2 - point_line1  # Vector along the line
  39.     var t: float = (point_tip - point_line1).dot(bc) / bc.length_squared()  # Projection factor
  40.     return point_line1 + t * bc
  41.  
Advertisement
Add Comment
Please, Sign In to add comment