- Home /
Hinge joint target angle and joint axis
Hello,
I've had a good look at the documentation for joints and also had a google search for information but I'm struggling to understand and hoped that someone smart could explain it to me in a simple manor.
If I have a leg, just 1 leg, which has a joint where the hip is that connects to the world. Then a knee joint that is connected to the hip and finally an ankle that is connected to the knee. The knee joint would just rotate on one axis and the knee contracts and extends, I can then set a target angle and use the motor to make the knee try to maintain a specific angle. So lets say I set the target to 45 degrees and thus the knee bends upwards to that position. But with regards to the hip, it can swing forward and back but also outwards to the side. I would now have to use 2 axis and I'm struggling to understand how the relationship between using 2 axis effect the target position of a hinge joint?
I'm just working on things for fun and learning at the moment and I wanted to make a leg or arm that I can pick what angle I want the different joints to be at and then the leg or arm would try to move to those positions via the motor, bit like a crane I guess.
If someone who understands joints can explain how the target position is effected by 2 axis that would be awesome.
Thanks
Answer by JVene · Aug 22, 2018 at 11:32 AM
A HingeJoint won't do 2 axis by itself. You could consider the configurable joint, but if you're just experimenting that would be a steep hill to climb. Instead, you could put two HingeJoints at basically the same position, but have one be the child of the other. One would handle the lateral rotation while the other the forward rotation you're already using. Putting a joint on an otherwise empty GameObject would allow for this two stage construction.
You're going to encounter the next phase of study for this kind of thing which I'll summarize for you. At some point you're going to want to place the foot at a position. To do that you have control only of the angles of the joints. To calculate what is required involves trigonometry, performing what is basically inverse kinematics. Forward kinematics would be what you're doing now, from hip to knee to ankle. The reverse naturally begins at the foot, which is placed by a position, which then translates into angles for the ankle, knee and hip. Focus on the knee. For any given distance from hip to ankle there is only one angle the knee can adopt. To calculate that angle, use the law of cosines (google provides). You create an imaginary line from hip to ankle, which forms a non-right triangle where the knee is the angle opposite the imaginary line. The law of cosines gives this angle because you'll calculate the length of the imaginary line, and will already know the upper and lower legs which form the triangle. Just follow the formula on any readable reference on using the law of cosines. The next calculation is for the hip. Use the law of cosines to calculate the interior angle of the imaginary triangle already discussed at the hip. The angle of the upper leg is that angle plus the angle of the imaginary line relative to the "straight" angle of the body (usually the Y axis). You will encounter the fact that the angle orientation you use for the GameObjects may not be the same as that given by standard trigonometric angles. Standard math assumes that zero degrees is a point on the X axis, not a point on the Y axis (where zero is up, like a clock). Positive rotations are counter clockwise from math, but you may see GameObjects orient that in reverse. Also, note that most trig functions require radians while you and the Euler functions of the Quaternion use degrees, so use Mathf.Rad2Deg (or Mathf.Deg2Rad) to convert. When you have the angles of the hip and knee, the ankle will be at a given coordinate by this reverse calculation. You can then extend this notion to the angle of the foot as required. Although you could place the foot according to the "toes", you should, for simplicity, place the foot based on the ankle position (save that extra work for fingers of a hand on an arm).
Thanks very much for you thorough answer. I might have to give the configurable joints a go as I would like to encompass everything into a single joint for each limb if possible. Oddly enough I've already been working on the "next phase" as you described. It's just the joints that have been giving me the most trouble as I haven't used them all that much so my knowledge is pretty thin. Thank you again for clarifying that I'll need to use a configurable joint to achieve the 2 axis rotations, this little project I'm doing is a slow burner and I'm in no rush so I'll try and read up on the configurable joints. Any particular repositories of information for configurable joints that you would recommend beyond the standard documentation?
The PhysX engine is under the hood, so you may benefit from documentation and commentary google might find on its use.
Your answer
Follow this Question
Related Questions
How to get an Objects position per axis? 1 Answer
target.gameObject Position 1 Answer
JS Move object over time 1 Answer
transform.position.y and x or z + 1? 0 Answers