#Well, I'm running into problems and need
1 messages · Page 1 of 1 (latest)
Ahh I see, you would need to include more details such as the error messages so people can better help 🙂
Apologies, I'm currently working with just FABRIK and am not yet converting the positions of the joints to rotations of the bones. This is what the Debug limb looks like at this point. The only error I'm getting right now is in the Debug, Gizmos.DrawSphere(joints[i], 0.1f); > Index Out of Range error
Ahh I see, this is because on line 141 you have
for (int i = 0; i < bones.Count; i++)
Looks like you're trying to draw the spheres on the joints rather than the bones tho, so should probably be something like
for (int i = 0; i < joints.Count ; i++)
Lemme know if that works
I also noticed in some places you have
for (int i = 0; i < joints.Count - 1; i++)
I think joints.Count - 1 is not necessary because it is set to i < joints.Count so it'll stop iterating before i == count but having i < joints.Count - 1 may actually cause you to miss one of the iterations
Same with bones.Count -1
So corrected would be like this:
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
for (int i = 0; i < joints.Count; i++)
{
Gizmos.DrawSphere(joints[i], 0.1f);
if (i < joints.Count)
{
Vector3 direction = (joints[i + 1] - joints[i]).normalized;
Gizmos.DrawRay(joints[i], direction * boneLengths[i]);
}
}
}```
I'm now getting errors in these two spots:
Are these more index out of range errors?
I think the boneLength errors have something to do with the fact that the boneLengths list is 1 smaller than the joints, because there are 5 joints/4 bones.
So this one in BackwardPass()
joints[i] = prevJoint + direction * boneLengths[i - 1];
That is failing because you are doing boneLength[i - 1];
The way your for loop is setup is to count backward from joints.Count - 1 down to 0, so when i eventually == 0 then on that line above you're trying to access boneLengths[i -1] which will be 0 - 1 so you'll try to access boneLengths[-1] which can't exist, hence the error
The one in ForwardPass() is likely what you say if bones is 1 element smaller than joints then that'll also give out of range errors
Have you done much with for loops before?
Not really no, the most I've done is with my radar script which uses a for loop to show icons at different positions at different times.
Also, for some reason getting rid of the -1 in the backward pass didn't get rid of the error
I would suggest putting some time into practicing for loops some more so you feel a little more comfortable and confident in what they're doing 🙂
Go through a couple tutorials on YouTube for C# For loops and then try a couple exercises you think up on your own and you'll find them a lot easier, then these out of range issues will become a lot easier cause you'll know better how you're trying to apply the for loop logic.
I'd guess it's still failing after removing the -1 because it's failing earlier in the loop, probably on the first iteration because like you say the bones list is smaller than the joints list
Also adding something like Debug.Log("Backward Iteration: " + i); into the for loop will help you see in the console what iteration it is failing on so it'll be easier to track back where your logic is falling down
I found the core of the problem, I was looping through the bones to set the bone lengths instead of looping through the joints, but I'm not sure why it's different?
Heres the area
before:
// Calculates the lengths of each bone and stores them in a list
boneLengths = new List<float>();
for (int i = 0; i < bones.Count - 1; i++)
{
float boneLength = Vector3.Distance(bones[i].position, bones[i + 1].position);
boneLengths.Add(boneLength);
}```
after:
```cs
// Calculates the lengths of each bone and stores them in a list
boneLengths = new List<float>();
for (int i = 0; i < joints.Count - 1; i++)
{
float boneLength = Vector3.Distance(joints[i], joints[i + 1]);
boneLengths.Add(boneLength);
}```
I'm assuming it's because there are more joints, and I was doing bones.Count - 1 instead of joints.Count - 1 or bones.Count by itself. So the bone length was actually adding one less than the bone amount.
I think what happened was I was getting those errors and second guessed myself so much the code had a bunch of possible causes instead of just the one that I found.
I might need a bit of help figuring out how to make the pole work, so the bones don't bend weird like this