- Home /
I want to calculate angles between a series of points and find the total angle between start and finish.
Hello, this will be my first question for the unity community. I'm new to Unity but have been programming for years. I've been conquering through, trying to grasp multiplying rotations by other rotations by time etc and all of these new (to me) 3d programming concepts. I'm really stumped on something that seems to be a frequent issue people run into so apologies if this is answered.
I have a parent game object with a collection of child game objects. The parent will identify the children and their transform positions and draw a line to each gameobject. I want to calculate the total number of degrees in the turn.
For example, I would calculate the angle between Waypoint[1] and waypoint[2]. I,ve tried with Vector3.angle and had no success.
I've attached an image which outlines what I'm trying to accomplish. What Im looking for help on is the angle part, the list of waypoints and calculating one by one based on start turn waypoint and end turn waypoint I have coded already.
Thanks in advance!
yes, at least in this case. The only other option is that they would all be counter clockwise. If there is a series of turns like an s bend then i would split it up into two turns, one covering the cw motion and the other ccw
...and yes they're also all under 90 degrees significantly.
Answer by AlwaysSunny · Jul 19, 2015 at 01:13 PM
The more you can guarantee about a situation, the easier it is to solve. Based on the diagram and comments, you have enough knowns to use a pretty simple method.
Since you're already using gameobjects for your waypoints, it would be useful to make each face its tangent in the curve. You could use LookAt() with the world "up" axis as the normal. This would allow the first element to make a meaningful contribution to the process, since otherwise it has no angular relationship to report (without additional data e.g. an imaginary previous waypoint).
Following this look-at idea, if they're coplanar, then you could sum the Vector3.Angle()s between each pair's forward directions. If not coplanar, you could zero out the "up" field of each position (or project their "forward" vectors onto the XZ plane) thus treating them as coplanar.
If you wanted the signed angle, that can be done too with a few cross and dot checks. Then you can use the same method for CW/CCW, even for "S" curves, etc, and just report the total difference in angle from start to end, or separate CW and CCW totals.
If you don't care about the start / end points making a contribution to this process (you might not), you could get the difference in position between each pair, then walk through those found vectors, comparing each to the next with Vector3.Angle.
In that case, the algorithm would take on this basic shape:
Vector3.Angle( b-a, c-b ) +
Vector3.Angle( c-b, d-c ) ...
Answer by andrew_2992 · Jul 21, 2015 at 02:43 AM
Just to let you guys know, and anyone else who stumbles on this article with a similar goal in mind. I've found a solution to my problem, thanks to the help from Sunny. The "look at" part you mentioned practically gave me an epiphany. It was something I had over looked completely and you were right, they had no angular relationship. Once I setup the look at for every waypoint after start turn and before end turn, to look at the following waypoint, I noticed in the angles in the inspector that they now had a relationship. I've found that Im able to find the angle im looking for by subtracting the rotation of the current waypoint from the rotation of the next waypoint in the list, who's global rotation will either be equal or greater than the current one the further down the turn it is, hence the turn lol. It's not exactly 100% elegant at all. But it completely works. I'm not a fan of doing things messy, but im going to see how this approach works, if I can keep it controlled it should do fine. So far its cooperating. Thanks for the tips Sunny.
Cheers -Andrew