• 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
1
Question by KubaPrusak · Jun 04, 2015 at 07:38 PM · c#physicsgravitypathtrajectory

Calculating the Trajectory of Planetary Bodies

Hi,

I have gotten planetary gravity working well and how I want it, but I can't figure out how to predict trajectories of objects. I've seen other examples of how to do it with using standard gravity. Here is the script that goes on every object that will attract one another.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Transform))]
 
 public class NewtonBody : MonoBehaviour {
 
     public Transform _transform;
 
     public float mass = 1f;
     public Vector3 velocity = Vector3.zero;
     public Vector3 initialForwardSpeed = Vector3.zero;
 
     void Awake() {
 
         _transform = GetComponent<Transform>();
     }
 }
 

This is the script that actually calculates the pull between all objects that have the script above.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Newton : MonoBehaviour {
 
     // Gravitational constant
     public float g = 0.01f;
 
     static List<NewtonBody> bodies;
     static Vector3 acceleration;
     static Vector3 direction;
     static float fixedDeltaTime;
 
     static Newton self;
 
     void Start() {
 
         self = this;
 
         NewtonSetup();
     }
 
     void FixedUpdate() {
 
         foreach(NewtonBody body in bodies) {
 
             NewtonUpdate(body);
         }
     }
 
     static void NewtonSetup() {
 
         fixedDeltaTime = Time.fixedDeltaTime;
 
         bodies = new List<NewtonBody>();
         bodies.AddRange(FindObjectsOfType(typeof(NewtonBody)) as NewtonBody[]);
 
         Debug.Log("There are probably " + bodies.Count + " Newton bodies in space (±42).");
 
         foreach(NewtonBody body in bodies) {
 
             body.velocity = body._transform.TransformDirection(body.initialForwardSpeed);
         }
     }
 
     static void NewtonUpdate(NewtonBody body) {
 
         acceleration = Vector3.zero;
 
         foreach(NewtonBody otherBody in bodies) {
 
             if(body == otherBody) continue;
             direction = (otherBody._transform.position - body._transform.position);
             acceleration += self.g * (direction.normalized * otherBody.mass) / direction.sqrMagnitude;
         }
 
         body.velocity += acceleration * fixedDeltaTime;
         body._transform.position += body.velocity * fixedDeltaTime;
     }
 }
 

If anyone has any ideas/pointers on how to create a trajectory with the code above, I would be very grateful.

Thanks

Comment
Add comment · Show 2
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 HuginnRaven · Jan 18, 2016 at 05:43 PM 0
Share

I have been pulling my hair to find a script that will do this. Thank you! Works like a charm. The only thing that needs to be changed is "direction" which just needs to be set to a Vector3. Other than that, perfect!

(And, for anyone who had the same issue as me, don't forget to change "public float g = 0.01f;" to something higher than 0.01 so that gravity actually has a chance. The actual constant is written as 6.673e-11f)

avatar image HuginnRaven HuginnRaven · Jan 16, 2016 at 08:31 PM 0
Share

By the way, I'm trying to figure out a way to have a character walk on the surface of the planets, got any idea of how to implement this?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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

Related Questions

walk on walls (third person) 0 Answers

How can I make a physics base projectile move along the path of a sphere? 1 Answer

Distribute terrain in zones 3 Answers

How can I show trajectory of a bouncing ball OnCollisionEnter/OnCollisionExit? 0 Answers

Show trajectory of a bouncing ball 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges