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 mlepp · May 13, 2018 at 12:08 PM · physicsgravitylinerenderer

2D trajectory prediction around planets

I've created a game where the player fires a projectile between planets trying to hit a target. The planets then apply their gravitational forces to the projectile. All of this is working correctly. What I now want is to use a LineRenderer to give a prediction of where the projectile will fly. This is where my math skills fail me.


My code/scene

All projectiles originate from one location and always at the same initial speed. The only variable that changes is the direction. When a projectile is fired I apply a force to the rigidbody

 rb.AddForce(origin.up * VELOCITY)

As soon as the projectile is spawned all gravitational entities in the scene starts pulling on it.

 float forceMagnitude = (projectileMass * planetGravity) / Mathf.Pow(distance, 2);
 Vector2 force = direction.normalized * forceMagnitude;
 rb.AddForce(force);

Thoughts

I've experimented somewhat trying to solve this and have managed to get a line which loops around the planets. However it doesn't match the path the projectile flies. So something is off. I think my calculation around applying gravity and how to simulate the initial velocity.

My goal is to have something similar to the game Orbit (Steam) alt text

Do you guys have any insight in how I can tackle this? I think I just need a nudge in the right direction.

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

1 Reply

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

Answer by mlepp · May 13, 2018 at 12:34 PM

Of course... Solved it almost exactly 20 minutes after asking the question... So for anyone else trying to solve the same thing, now or in the future, here is my solution:

 void FixedUpdate() 
 {
     Vector2 velocity = origin.up * 100 * Time.fixedDeltaTime;
     lineRenderer.positionCount = segmentCount;
     lineRenderer.SetPosition(0, origin.position);

     float dt = Time.fixedDeltaTime;

     for (int i = 1; i < segmentCount; i++)
     {
         Vector2 previous = _lineRenderer.GetPosition(i - 1);
         velocity += CalculateGravity(previous) * dt;
         lineRenderer.SetPosition(i, previous + velocity * dt);
     }
 }
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 sarthaktodmal325 · May 29, 2021 at 12:59 PM 0
Share

what is CalculateGravity?? in (velocity += CalculateGravity(previous) * dt;)

avatar image mlepp sarthaktodmal325 · May 29, 2021 at 03:55 PM 0
Share

It's been a long time and I've moved past this project. But I'm pretty sure that the CalculateGravity is my own calculation for deter$$anonymous$$ing what the gravity is at a given position. It takes previous which is the last position, checks all (or any relevant) planets and returns what the gravity is as a sum of all possible influences. This function is very game dependent, so it is hard to give a universal CalculateGravity function.

Managed to dig up my code. This is my simple CalculateGravity method:

 Vector2 CalculateGravity(Vector3 pos)
 {
     Vector2 velocity = Vector2.zero;
     foreach (var attractor in _attractors)
     {
         velocity += attractor.GetForce(pos, 1);
     }
     return velocity;
 }

Where _attractors is a list of all different gravitational influences.

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

146 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

Related Questions

Golfball sphere collider "Hops" over vertex-joined planes at intersection? 1 Answer

Edit this script so that on the second jump change the gravity to 5 2 Answers

How can I make a game object move in parabolic motion as if it were under gravity? 2 Answers

Change direction of gravity for a specific instance of a prefab 1 Answer

Setting a jump force on a rigidbody 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