Browse Source

IK Demo - Removed code that straightens the FABRIK chain when the target is out of reach because it was not working, and instead just run the FABRIK chain even if the target is out of reach. This straightens the arms like expected and simplifies the code. Moved the camera back in both the look_at_ik and fabrik_ik scenes

TwistedTwigleg 5 years ago
parent
commit
59e85689ba
4 changed files with 21 additions and 44 deletions
  1. 14 38
      3d/ik/addons/sade/ik_fabrik.gd
  2. 4 3
      3d/ik/fabrik_ik.tscn
  3. 2 2
      3d/ik/look_at_ik.tscn
  4. 1 1
      3d/ik/target_from_mousepos.gd

+ 14 - 38
3d/ik/addons/sade/ik_fabrik.gd

@@ -193,47 +193,23 @@ func solve_chain():
 			var middle_point_pos = middle_joint_target.global_transform
 			bone_nodes[bone_nodes.size()/2].global_transform.origin = middle_point_pos.origin
 	
-	# Get the distance from the origin to the target
-	var distance = (chain_origin - target_pos).length()
+	# Get the difference between our end effector (the final bone in the chain) and the target
+	var dif = (bone_nodes[bone_nodes.size()-1].global_transform.origin - target_pos).length()
 	
-	# If the distance is farther than our total reach, the target cannot be reached.
-	# Make the bone chain a straight line pointing towards the target
-	if distance > total_length:
-		for i in range (0, bones_in_chain.size()):
-			# Create a direct line to target and make this bone travel down that line
-			var curr_origin = bone_nodes[i].global_transform.origin
-			var r =(target_pos - curr_origin).length()
-			var l = bones_in_chain_lengths[i] / r
-			
-			# Find new join position
-			var new_pos = curr_origin.linear_interpolate(target_pos, l)
-			
-			# Apply it to the bone node
-			bone_nodes[i].look_at(new_pos, Vector3.UP)
-			bone_nodes[i].global_transform.origin = new_pos
+	# Check to see if the distance from the end effector to the target is within our error margin (CHAIN_TOLERANCE).
+	# If it not, move the chain towards the target (going forwards, backwards, and then applying rotation)
+	while dif > CHAIN_TOLERANCE:
+		chain_backward()
+		chain_forward()
+		chain_apply_rotation()
 		
-		# Apply the rotation to the first node in the bone chain, making it look at the next bone in the bone chain
-		bone_nodes[0].look_at(bone_nodes[1].global_transform.origin, Vector3.UP)
-	
-	# If the distance is NOT farther than our total reach, the target can be reached.
-	else:
-		# Get the difference between our end effector (the final bone in the chain) and the target
-		var dif = (bone_nodes[bone_nodes.size()-1].global_transform.origin - target_pos).length()
+		# Update the difference between our end effector (the final bone in the chain) and the target
+		dif = (bone_nodes[bone_nodes.size()-1].global_transform.origin - target_pos).length()
 		
-		# Check to see if the distance from the end effector to the target is within our error margin (CHAIN_TOLERANCE).
-		# If it not, move the chain towards the target (going forwards, backwards, and then applying rotation)
-		while dif > CHAIN_TOLERANCE:
-			chain_backward()
-			chain_forward()
-			chain_apply_rotation()
-			
-			# Update the difference between our end effector (the final bone in the chain) and the target
-			dif = (bone_nodes[bone_nodes.size()-1].global_transform.origin - target_pos).length()
-			
-			# Add one to chain_iterations. If we have reached our max iterations, then break
-			chain_iterations = chain_iterations + 1
-			if chain_iterations >= CHAIN_MAX_ITER:
-				break
+		# Add one to chain_iterations. If we have reached our max iterations, then break
+		chain_iterations = chain_iterations + 1
+		if chain_iterations >= CHAIN_MAX_ITER:
+			break
 	
 	# Reset the bone node transforms to the skeleton bone transforms
 	for i in range(0, bone_nodes.size()):

+ 4 - 3
3d/ik/fabrik_ik.tscn

@@ -69,14 +69,14 @@ material/0 = ExtResource( 3 )
 material/1 = ExtResource( 4 )
 
 [node name="Camera" type="Camera" parent="."]
-transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 11.5, 8.8 )
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 11.5, 11 )
 fov = 74.0
 script = ExtResource( 5 )
-MOVEMENT_SPEED = -6.0
+MOVEMENT_SPEED = -8.0
 flip_axis = true
 
 [node name="targets" type="Spatial" parent="Camera"]
-transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -5.41814 )
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -8 )
 
 [node name="IK_LookAt_Head" type="Spatial" parent="Camera/targets"]
 script = ExtResource( 6 )
@@ -95,6 +95,7 @@ __meta__ = {
 skeleton_path = NodePath("../../../BattleBot/Armature/Skeleton")
 bones_in_chain = PoolStringArray( "Left_UpperArm", "Left_LowerArm" )
 bones_in_chain_lengths = PoolRealArray( 1.97, 3 )
+chain_iterations = 10
 limit_chain_iterations = false
 use_middle_joint_target = true
 

+ 2 - 2
3d/ik/look_at_ik.tscn

@@ -60,10 +60,10 @@ material/0 = ExtResource( 3 )
 material/1 = ExtResource( 4 )
 
 [node name="Camera" type="Camera" parent="."]
-transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 11.5014, 8.81922 )
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 11.501, 11 )
 fov = 74.0
 script = ExtResource( 5 )
-MOVEMENT_SPEED = -2.0
+MOVEMENT_SPEED = -3.0
 flip_axis = true
 
 [node name="targets" type="Spatial" parent="Camera"]

+ 1 - 1
3d/ik/target_from_mousepos.gd

@@ -1,6 +1,6 @@
 extends Camera
 
-export(float) var MOVEMENT_SPEED = 10
+export(float) var MOVEMENT_SPEED = 12
 export(bool) var flip_axis = false
 
 var targets = null