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 /
  • Help Room /
avatar image
0
Question by MichalOp · Dec 19, 2015 at 06:58 PM · c#physicsorbit

Problem with calculating potential and kinetic energy

Hello everyone. I need to calculate the mechanical energy of orbiting body , which is a sum of kinetic and potential energy. Mechanical energy should be constant, but for some reason, it isn't.

alt text

This is my problem. I don't know why, but the mechanical energy changes, when the object is moving closer/farther from the influencer. I tried calculating everything on double precision numbers, but it didn't work.

 public class OrbitingBody : MonoBehaviour { 
 
     Rigidbody influencer;
     Rigidbody rigidbody;
     public float mechanicalEnergy;
     public float gravityConstant;
   
     void Start () {
         influencer = FindObjectOfType<Influencer>().GetComponent<Rigidbody>();
         rigidbody = GetComponent<Rigidbody>();
     }
     
     void FixedUpdate () {
         mechanicalEnergy = Energy(influencer); 
    }
 
     float Energy(Rigidbody other)
     {
         float Ek = rigidbody.mass * rigidbody.velocity.sqrMagnitude / 2; // kinetic energy
         float Ep = -gravityConstant * rigidbody.mass * other.mass / (other.transform.position - transform.position).magnitude; // potential energy
 
         return Ek+Ep; // mechanical energy of an object;
     }}

}

 public class Influencer : MonoBehaviour {
 
     Rigidbody[] rigidbodies;
     Rigidbody rigidbody;
     public float gravityConstant;
 
     void Start () {
         rigidbodies = FindObjectsOfType<Rigidbody>() as Rigidbody[];
         rigidbody = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         foreach (Rigidbody rigid in rigidbodies)
         {
             if (rigidbody != rigid)
             {
                 Vector3 dir = rigid.transform.position - transform.position;
                 rigid.velocity += -dir.normalized * gravityConstant * rigidbody.mass / dir.sqrMagnitude * (Time.fixedDeltaTime);
                 
             }
         }
     }
 }


problem-image.png (53.1 kB)
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
0

Answer by watah · Feb 14, 2016 at 01:51 AM

I have the same problem with a simple bouncing ball: http://gamebucket.io/game/7817e417-baa5-4e8b-9d54-10457d010770

Comment
Add comment · Show 1 · 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 watah · Feb 14, 2016 at 08:10 AM 0
Share

Here's a better simulation: http://gamebucket.io/game/790591d2-67bc-4a62-acce-097852291fbe

The problem seems to be the way in which the unity 2D physics engine calculates the new position of an object under gravity. It appears that the unity 2D physics engine currently does this:

  • NewVelocity = OldVelocity + Gravity*DeltaTime

  • NewPosition = OldPosition + NewVelocity*DeltaTime

However, the correct equation for NewPosition should be

  • NewPosition = OldPosition + OldVelocity*DeltaTime + (1/2)Gravity(DeltaTime^2)

or, alternatively,

  • NewPosition = OldPosition + NewVelocity*DeltaTime - (1/2)Gravity(DeltaTime^2)

Since DeltaTime is usually constant, (1/2)Gravity(DeltaTime^2) is usually also constant, and small, but it is a factor that should be taken into account, so that the ball always bounces to height 4 ins$$anonymous$$d of alternating between bouncing to height 4 and height 3.92.

This way the total energy (potential and kinetic) will also be conserved.

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

49 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

Related Questions

Orbit On Rails 1 Answer

How to make an object orbit around another object? 0 Answers

Getting NaN values out of a function. Need help to solve the problem.,Getting NaN values out of a function, and I do not understand why. 0 Answers

How do I make pull object script out of this? 1 Answer

How to flip character using touch controls ? 0 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