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 Godfish · Jun 28, 2019 at 04:28 AM · physicsgravityorbit

problems with single body Newtonian physics emulation

I've made a class that holds all the parameters for an orbit

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Orbit
 {
     public Transform target;
     public Vector3 centreOffset;
     public Vector3 axisA;
     public Vector3 axisB;
     public float M;
     public float semiMajorAxis;
     public float semiMinorAxis;
     public float apoapse;
     public float periapse;
     public float phase;
     public float period;
 
     public Orbit(Vector3 offset, Vector3 tangent, Transform target, float M, float periapse, float eccentricity, float phase)
     {
         this.target = target;
         this.M = M;
         this.phase = phase;
         axisA = offset.normalized;
         axisB = (tangent - Vector3.Dot(tangent, axisA) * axisA).normalized;
 
         this.periapse = periapse;
         apoapse = periapse * ((1 + eccentricity) / (1 - eccentricity));
 
         semiMajorAxis = (periapse + apoapse) / 2;
         semiMinorAxis = Mathf.Sqrt(semiMajorAxis * semiMajorAxis - (semiMajorAxis - periapse) * (semiMajorAxis - periapse));
 
         centreOffset = axisA * Mathf.Lerp(-periapse, apoapse, 0.5f);
         period = 2 * Mathf.PI * Mathf.Sqrt((semiMajorAxis * semiMajorAxis * semiMajorAxis) / M);
     }
 
     public PosVel Solve(float t)
     {
         return new PosVel(Vector3.zero, Vector3.zero);
     }
 }
 
 public class PosVel
 {
     public Vector3 position;
     public Vector3 velocity;
 
     public PosVel(Vector3 position, Vector3 velocity)
     {
         this.position = position;
         this.velocity = velocity;
     }
 }

note that target is the transform at the focus of the orbit and M is equal to the mass of the body * the gravitational constant. I'm not sure how to go about writing the solve function. t is supposed to be a given time, and solve should return a vector3 for the satellite's position at time t, and a vector3 velocity in m/s.

I can't find anything online about implementing this kind of thing, and although I know about kepler's laws, my lack of much formal math education makes them difficult to implement.

I've also written a monobehavior as follows for visualizing all the data in the above class so if you're interested in helping you needn't go to that effort.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [ExecuteInEditMode]
 public class DrawOrbit : MonoBehaviour
 {
     Orbit orbit;
     [Range(0,1)]
     public float ecc = 0.2f;
     public float per = 1f;
     public Vector3 h1;
     public Vector3 h2;
     public float sma;
 
     private void OnValidate()
     {
         orbit = new Orbit(h1, h2, transform, 1f, per, ecc, 0f);
         sma = (orbit.apoapse + orbit.periapse)/2;
     }
 
     private void OnDrawGizmos()
     {
         Gizmos.color = Color.black;
         float incr = Mathf.PI / 200f;
         for (float i = 0; i < 2 * Mathf.PI; i += incr)
         {
             Vector3 from = orbit.axisA * orbit.semiMajorAxis * Mathf.Sin(i) + orbit.axisB * orbit.semiMinorAxis * Mathf.Cos(i);
             Vector3 to = orbit.axisA * orbit.semiMajorAxis * Mathf.Sin(i + incr) + orbit.axisB * orbit.semiMinorAxis * Mathf.Cos(i + incr);
             Gizmos.DrawLine(from + orbit.centreOffset, to + orbit.centreOffset);
         }
         Gizmos.color = Color.yellow;
         Gizmos.DrawWireSphere(h1, 0.15f);
         Gizmos.DrawWireSphere(h2, 0.15f);
         Gizmos.color = Color.red;
         Gizmos.DrawLine(Vector3.zero, orbit.axisA);
         Gizmos.color = Color.green;
         Gizmos.DrawLine(Vector3.zero, orbit.axisB);
         Gizmos.color = Color.blue;
         Gizmos.DrawWireSphere(orbit.axisA * -orbit.periapse, 0.2f);
         Gizmos.DrawWireSphere(orbit.axisA * orbit.apoapse, 0.2f);
     }
 }

as an aside I also want to write a constructor for orbit that takes M of the body and the relative position and velocity of the satellite, and I still need to handle the case if the satellite is at or above escape velocity, but both of those will come after the solve function works.

if you attempt to tackle this problem then thank you in advance, regardless of if you're successful or not.

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

0 Replies

· Add your reply
  • Sort: 

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

183 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

Related Questions

How to make an orbital movement as smoothly as possible? 0 Answers

calculate the future position of an object in orbit 1 Answer

How to lock an objects orbiting focal point. 0 Answers

Tornado throwing back force after pulling 1 Answer

How could I simulate planetary gravity that has an orbit point? 1 Answer


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