Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DehLeprechaun · Jan 02, 2020 at 10:51 PM · scripting problembug-perhapsorbittrigonometry

Mathf.Sin only returning positive values?

I'm using Orbital Elements to generate some planets and their orbits in Unity 2019.2.8f1, and it seems to work fine. However, when I try to use my functions to draw the orbit using the LineRenderer, Mathf.Sin only returns positive values.

 void FindOrbitPath(int num)
     {
         Vector3[] points = new Vector3[num];
         for(int i = 0; i < num; i++)
         {
             float tic = i * planet.OrbitalPeriod / num;
             Vector4 Anoms = FindAnomaly(tic);
             float tAnomaly = Anoms[2];
             float eAnomaly = Anoms[1];
             float radius = planet.SemimajorAxis * (1 - planet.Eccentricity * Mathf.Cos(Mathf.Deg2Rad *
                 eAnomaly));
             float x = Mathf.Cos(Mathf.Deg2Rad * tAnomaly) * radius;
             float z = Mathf.Sin(Mathf.Deg2Rad * tAnomaly) * radius;
 
             Vector3 point = new Vector3(x, 0, z);
             points[i] = point;
 
             Debug.Log("Anomalies :" + Anoms.ToString() + " Radius :" + radius.ToString() +
                 " | Tic: " + tic.ToString());
         }
 
         LR.positionCount = num;
         LR.SetPositions(points);
     }

This is apparently the trouble function. True Anomaly appears to be returning correctly, but my "z" value almost always returns a positive when it should be negative. I'll include the FindAnomaly() function below as well.

 Vector4 FindAnomaly(float t)    //returns: MeanAnomaly, EccentricAnomaly, TrueAnomaly, DeltaTrueAnomaly
     {
         float mean_anomaly = 0;
         float eccentric_anomaly = 0;
         float true_anomaly = TrueAnomaly;
         float d_true = 0;
 
         mean_anomaly = planet.MeanAnomaly + meanAngularMotion * (t);
 
         eccentric_anomaly = mean_anomaly + (planet.Eccentricity - (1 / 8) * Mathf.Pow(planet.Eccentricity, 3)) *
             Mathf.Sin(Mathf.Deg2Rad * mean_anomaly);
 
         float NewTrueAnomaly = Mathf.Acos((Mathf.Cos(Mathf.Deg2Rad * eccentric_anomaly) - planet.Eccentricity) /
             (1 - planet.Eccentricity * Mathf.Cos(Mathf.Deg2Rad * eccentric_anomaly))) * Mathf.Rad2Deg;
 
         if (NewTrueAnomaly > true_anomaly)
             d_true = NewTrueAnomaly - true_anomaly;
         else
             d_true = true_anomaly - NewTrueAnomaly;
         true_anomaly = NewTrueAnomaly;
 
         Vector4 results = new Vector4(mean_anomaly, eccentric_anomaly, true_anomaly, d_true);
         return results;
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DehLeprechaun · Jan 03, 2020 at 05:01 AM

I have apparently solved the problem by using the following equation:

 NewTrueAnomaly = 2 * Mathf.Atan(Mathf.Sqrt((1 - planet.Eccentricity * Mathf.Deg2Rad) / (1 - planet.Eccentricity *
             Mathf.Deg2Rad)) * Mathf.Tan(eccentric_anomaly * Mathf.Deg2Rad / 2)) * Mathf.Rad2Deg;

And by using a more appropriate time system that keeps things more consistent. Things seem to be working alright now.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Bunny83 · Jan 03, 2020 at 12:22 AM

How did you come to this conclusion? Unity just uses the math functions of the .NET / Mono framework which directly map to the trigonometric methods implemented in the FPU / CPU hardware. You can be sure that Mathf.Sin does work exactly as it should be. If you do not get any negative values you simply do not pass in any angles which would cause a negative value. So your angle is between 0 - 180 and never between 180 - 360 or -180 - 0 which would return a negative value.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DehLeprechaun · Jan 03, 2020 at 01:35 AM 0
Share

Actually, now that you mention it. I think my true anomaly calculation is set up to only return positives ... Actually, that's just the delta calc. I'll need to analyze my outputs

avatar image DehLeprechaun · Jan 03, 2020 at 03:07 AM 0
Share

You were correct, after I checked my true anomaly output, it was swinging between 0 - 180.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

209 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

clamp rotation by mouse direction 0 Answers

Orbit Camera on Survival Shooter Sample Project 0 Answers

Exiting an if statement when calling a function from another script (Possible bug) 1 Answer

Need help orbiting an object around another object based on rotation 1 Answer

I use this code to jump, but it's just teleporting my player upwards. It works totally perfect in an other project. 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges