Answers for "[MATH] Vectors, magnitudes, and bears; Oh My! -Solved"
http://answers.unity.com/questions/895234/math-vectors-magnitudes-and-bears-oh-my.html
The latest answers for the question "[MATH] Vectors, magnitudes, and bears; Oh My! -Solved"Answer by karma0413
http://answers.unity.com/answers/895810/view.html
#The Solution:
1. W$$anonymous$$le I was testing the variables, I was working in the wrong section of code. T$$anonymous$$s helped contribute to the problem because I was always in a case where I wouldn't see the changes. /BONK
2. Additionally, the other folks replying to my problem were in fact correct about the Debug.DrawRay. W$$anonymous$$le, I thought I had enough drawRays on the screen; I actually was not showing the resulting drawRay each time I did a calculation and therefore was not completely understanding the processes taking place.
3. My calculations were in fact correct, and I haven't changed them. Well except for one minor problem. When I originally did the COUNTER-CLOCKWISE script. I did the inverse calculation of the result after all the changes. More correctly, when I do counter-clockwise I simply needed the INVERSE of the (LeftHand Rule) resulting vector. Now the mathematical inverse is done in the right location.
4. And to finally answer my own original question:**YES,**: When you do an addition between two different Vector3 directions. Their magnitudes will automatically weight it's direction. So we can see here in the updated code I provide now:
We can see to obtain, the left-hand rule calculation ( t$$anonymous$$s is called Cross Product multiplication ) we do the following:
if (orbitDirection == 1) // NORMAL CLOCK-WISE
{
// *** WARNING *** Unity uses the LEFT HAND RULE: http://docs.unity3d.com/ScriptReference/Vector3.Cross.html
Vector3 a = myTarget.transform.position - t$$anonymous$$s.transform.position;
Vector3 b = t$$anonymous$$s.transform.up;
Vector3 c = Vector3.Cross (a,b);
vectorTwo = c;
We already had VectorOne - w$$anonymous$$ch was simply go ( towards) or ( awayfrom ) depending if we were shallow of the orbiting optimal range, or if we had exceeded. And now we just found vectorTwo above.
The next t$$anonymous$$ng to do was to define the strengths of each of these Vectors. For example if my optimal orbitting range is 400meters. Then I stated that if the s$$anonymous$$p was wit$$anonymous$$n 80% of that, then lets use t$$anonymous$$s smoothed-navigation-guidance code as below:
float upperEndRange = optimalRange + (optimalRange * 0.80f);
float lowerEndRange = optimalRange - (optimalRange * 0.80f);
And Now we just simply find out the strengths. And to find out how much strength each vector gets; we define it in terms of 100% of how close or far we are from our optimal range. A float number that is between 0-100. [EDIT NOTE:] I could have simply divided the final result by/100 to get a number between 0-1.0. But I found it wasn't needed when computing the final destination direction.
tempStrength : is how much strength to go (towards) or (awayfrom) the target we are orbitting. Obviously is we are closer to the 80% limit on t$$anonymous$$s. then tempStrength will be very near 100
cStrength : is the strength of the tangent Vector that is perpendicular to the object we are orbitting. And as a DIRECT-INVERSE-RELATIONSHIP ; whatever % of 100% is left from tempstrength. we can give it to cStrength. So the closer we are to our optimal range ; then the more cStrength = 100, and our tempStrength becomes close to 0.0
tempStrength = Mathf.Abs (optimalRange - distanceToTarget);
float ratio = 100/ (optimalRange - lowerEndRange); // so here is how we got that ratio multiplyer number... By basically putting it in an aspect in terms of percentage. /100
tempStrength = tempStrength * ratio; // .4166 ONLY if the upper end is 20% of 400 and lower end is 20% of 400, how did we get t$$anonymous$$s number though...Explained one line up..
cStrength = 100 - tempStrength;
//vectorOne = vectorOne.normalized;
vectorOne = vectorOne * tempStrength;
//vectorTwo = vectorTwo.normalized;
vectorTwo = vectorTwo * cStrength;
tempDirection = vectorOne + vectorTwo;
Very Lastly: To find the COUNTER-CLOSEWISE... As I mentioned we just plug in the Inverse of Vector C; w$$anonymous$$ch was (looking back), the RESULT of a CrossProduct between Vector a and Vector b. So Now instead of facing LEFT-HAND... we actually turn right and go counter-clockwise (IF WE ARE FACING THE OBJECT)
if (orbitDirection == -1) //ACTUALLY COUNTER-CLOCKWISE
{
Vector3 a = myTarget.transform.position - t$$anonymous$$s.transform.position;
Vector3 b = t$$anonymous$$s.transform.up;
Vector3 c = Vector3.Cross (a,b);
vectorTwo = -c;
float tempStrength;
float cStrength;
tempStrength = Mathf.Abs (optimalRange - distanceToTarget);
float ratio = 100/ (optimalRange - lowerEndRange); // so here is how we got that ratio multiplyer number... By basically putting it in an aspect in terms of percentage. /100
tempStrength = tempStrength * ratio; // .4166 ONLY if the upper end is 20% of 400 and lower end is 20% of 400, how did we get t$$anonymous$$s number though...Explained one line up..
cStrength = 100 - tempStrength;
//vectorOne = vectorOne.normalized;
vectorOne = vectorOne * tempStrength;
//vectorTwo = vectorTwo.normalized;
vectorTwo = vectorTwo * cStrength;
tempDirection = vectorOne + vectorTwo;
//tempDirection = tempDirection;
}
#Oooh, it's fancy, huh?
I figured you all might appreciate a screenshot of the result:
**Do: Control+MouseWheel to zoom into all of its fine glory!**
![alt text][1]
[1]: http://i.share.pho.to/513ff37e_o.pngSat, 07 Feb 2015 14:45:16 GMTkarma0413Answer by tanoshimi
http://answers.unity.com/answers/895641/view.html
Haven't studied in detail, but at first glance t$$anonymous$$s looks wrong to me:
float calcX = (t$$anonymous$$s.transform.position.x - myTarget.transform.position.x) - myTargetsRadius;
float calcY = (t$$anonymous$$s.transform.position.y - myTarget.transform.position.y) - myTargetsRadius;
float calcZ = (t$$anonymous$$s.transform.position.z - myTarget.transform.position.z) - myTargetsRadius;
distanceToTarget = Mathf.Sqrt (calcX*calcX + calcY*calcY + calcZ*calcZ);
Did you not mean:
Vector3 calc = (t$$anonymous$$s.transform.position - myTarget.transform.position).normalized * myTargetsRadius;
distanceToTarget = Vector3.Distance(t$$anonymous$$s.transform.position, calc);
?
But agree with the other commenters - use Debug.Log and OnDrawGizmos to test what values your code is producing/plot them visually in the scene view.Sat, 07 Feb 2015 07:32:03 GMTtanoshimi