Help me understand CFrames and Inverse Kinematics (specifically this post regarding it)

Hello! I’m looking to be a developer on Roblox! I have an understanding of lua and how to make things (Ex. A rotating door, adding an animation to a player, module scripts) but… I’m having trouble understanding more complicated movement.

I am reading this post, as I want to add procedural animation to my character. I am… Bad at learning from posts and stuff and there isn’t really good video resources about what I’m trying to achieve. Any videos on Inverse Kinematics cover the FABRIK method instead of with trigonometry. I do not desire this as I want to have more control over the movement.

I am… Aware… That I might be stepping too deep into the water. I have barely an understanding of matrix math (I’m 17 but American education is sorry when you live in a poor area like I do). Is anyone willing to help me out? I understand the concept (and some of the math) behind the Law of Cosines, however there are things that confuse me about the referenced post and I don’t even know where to start.

Example:
image

  1. Why do you have to multiply the upper torso CFrame (upperTorso.CFrame) with a saved CFrame of the shoulder joint (shoulderC0Save)
  2. Why does the shoulder angle need half of pie added to the calculated angle? Why is it in the X part of the equation (local shoulderAngle = CFrame.Angles(A + math.pi/2, 0, 0))
  3. Why does the elbow angle need subtracted by the calculated shoulder angle (CFrame.Angles(C - A, 0, 0))

I have more questions, but I dont even know how to go about answering these questions ive posed right here.

Maybe youre a developer who has experience with CFrames and stuff, how can I achieve my goals?
Where do I have to look to find the answers to my questions and gain an understanding of CFrame math. Perhaps someone could make an extensive and informational post about it (or link me one that I may not know about), or perhaps someone could make a very long video talking all about CFrames… Or maybe someone can just private message me and help me understand basics about CFrames and answer my questions lol (probably not but I can always hope).

2 Likes

CFrames come from linear algebra. A CFrame is a “transformation matrix”.

I’d recommend watching this series, at least the first three episodes, to learn what a transformation matrix is: https://youtube.com/playlist?list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab

If you’re totally lost after the first video, no worries and we can start smaller :slight_smile:

3 Likes

When multiplying matrices, it’s like adding the positions and rotations. So this is basically

CurrentShoulderWorldPosition = TorsoWorldCFrame+ShoulderOffset)

Half of pi is 180 degrees. Basically they just need it to be backwards from where it is. There isn’t a particular reason ‘why’ it is the X axis, that’s just the one that happens to be correct in this instance.


The point of Inverse Kinematics is to choose a target position and/or rotation, and figure out what angles are required to meet that goal. So in order to reach the desired position, they would have done a little bit of triangle math to determine the overall angle required, and then subtracted the first angle to find the second angle.


EgoMoose also recommends watching that series. After about six videos though, I need to collect my brains off of the floor with a mop.

2 Likes

That won’t answer all your questions, also.

One good thing to google is “radians vs degrees”—that’ll help you understand why math.pi is showing up (CFrames use radians, not degrees, to represent angles).

Thanks for replying (also thanks to nicemike40)!

Why isnt C by itself the correct angle though? Also why does the shoulder angle need to be backwards? I also have more questions but idk i dont wanna sound annoying and ask a lot of questions I guess.

I will watch that series though and try my best to absorb the information.

Inverse Kinematics makes my head bravely deactivate in self-defense, but I’ve dabbled. I’m not going to try to understand every aspect of what is going on here, but C is most likely the the overall angle, and A is the shoulder angle. Using this, it stands to reason that B (or C-A) is the remaining angle.


That’s just how it’s set up, based on the C0 and C1 of the joints. Think of building a robot in real life. If 0 on the motor is pointing the wrong direction, then just add an offset angle (in this case 180) until it is pointing the right way. You might even need to make it a negative number if the motor is rotating counterclockwise when you need it to go clockwise. You can probably fix it with more CFrames (or re-engineering your IRL robot), but that’s not really worth it if you can just add something small like that in the code.


You’re asking good questions and you’re putting in the work required to learn. Your title is descriptive which is rare enough here, your questions are well-worded, and if you’re not at the skill level yet then you’re pretty darn close. You’re the kind of person we want to help. Ask away.


Those videos will help you learn CFrames, which are definitely essential for 3D systems. But inverse kinematics also exist in 2D, and I find it makes understanding the concept a lot clearer. I’ll try to explain it here, but I’m not great at communication. Please ask any clarifying questions and I’ll straighten it out.


image
(not my image)

In this instance, we want to get the red dot (the end effector) to a desired position, X and Y.

Side note on affect vs effect

Affect is a verb used to mean the way one thing interacts with another. For example, “It had no affect on me.” Effect is usually a noun, meaning the result of something that happened. For example, “The end effect was to close the school.” Effect can be a verb as well though, and that is what it means in this case. To effect something means to cause it or bring it about. So the “end effector” is the device on the end of the robotic arm which effects things, or causes things to happen.

To get the end effector to the desired position, we have control over Θ1 and Θ2 (Theta 1 and Theta 2, or the two angles) The first step here is to determine Θ2. That’s pretty straightforward if you know any trigonometry. Draw a triangle with three sides, L1, L2, and L3 where L3 is the ‘construction’ line that we draw between 0,0 and X,Y. Basically connecting the points here. You can get the length of L3 with the Pythagorean theorem: sqrt(X^2+Y^2)
Once you have these, you can determine Θ2 using the law of cosines, which is where my brain starts to melt and I just copy and paste the formula.
Θ2 = acos((L1^2 + L2^2 - L3^2) / (2 * L1 * L2))


image
For this last step I will be using three point orderings to describe which angle I’m referring to. The middle letter in each pair will be the angle. So for example, elbow / Θ2 that we already solved for will be referred to as ∠BGA.
To find Θ1 / shoulders / ∠CAG, you need to take the sum of ∠GAB and ∠CAB.
You can find ∠GAB with that same obnoxious formula I plagiarized earlier.
∠GAB = acos((L1^2 + L3^2 - L2^2) / (2 * L1 * L3))
Then you can find ∠CAB a little easier because it’s a right triangle.
∠CAB = atan(Y / X) These (X and Y), along with the rest of my math, are in local coordinates. Our origin is assumed to be 0,0 and if for some reason it isn’t, you’ll need to subtract origin from Y and X here and most other locations.
∠CAG / shoulders / Θ1 is thus ∠CAB + ∠GAB. This equation will work fine, but your two joint robotic arm will actually look like this:
image
This is because we added the two angles. In the diagram, it’s obvious that ∠CAG is actually less than ∠CAB. So ∠GAB should be negative.
Θ1 = ∠CAB + -∠GAB

And there you go. Deriving the two angles you need to make a two link inverse kinematic model.

2 Likes