• 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
Question by Servail · Jul 12, 2018 at 11:54 AM · physicsdragaccelerationmaths

Convert rigidbody.drag to acceleration(m/s²)?

Drag afaik is kinda modifying velocity every FixedUpdate like:

 velocity *= 1-Mathf.Clamp01(drag*Time.fixedDeltaTime);

or maybe

 velocity -= velocity*Mathf.Clamp01(drag*Time.fixedDeltaTime);

or something like that (found on forums). Is the conversion even possible? I'm pretty sure that 0 drag = 0 deceleration and 1/Time.fixedDeltaTime drag = infinite deceleration (which makes it dependent on fixedDeltaTime, tested, looks like a bug). So I ended with constructions like:

 dragFactor = 1-Mathf.Clamp01(drag*Time.fixedDeltaTime);
 deceleration = 1/dragFactor; //can't be <1, wrong proportions

or even

 deceleration = 1/Mathf.Pow(dragFactor,1/dragFactor); //can't be <1 too, relatively right proportions

What I'm doing is replacing rigidbody bullets with raycasts based on their parameters. What I'm trying to find is stopping distance (S = v0^2 * 2*a) and speed after some distance traveled (v = v0^2 - 2aS). This strange formula that I found by scratching my head and randomly swapping numbers and operands for like two days is kind of worked:

 float StopDist(float speed, float drag) {
     float dragFactor = 1-Mathf.Clamp01(drag*Time.fixedDeltaTime);
     return speed * Mathf.Pow(dragFactor,1/dragFactor);
 }

but failed on high drag / low distances. And this is like for speed after traverse:

 float CurrSpeed(float initSpeed, float drag, float dist) {
     float dragFactor = 1-Mathf.Clamp01(drag*Time.fixedDeltaTime);
     float deceleration = 1/Mathf.Pow(dragFactor,1/dragFactor)-1; //-1 is temporal workaround
     return initSpeed-Mathf.Max(0,dist*deceleration);
 } 

I'm only need these two formulas (even based on drag), but conversion is much much better.

Comment

People who like this

0 Show 9
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 Harinezumi · Jul 12, 2018 at 12:16 PM 0
Share

Drag is unfortunately one of those variables that has very bad description, so it is unclear what it does exactly. But what you wrote, that it is modifying velocity in FixedUpdate() based on fixedDeltaTime seems to be correct.
In which case, I would use the following conversion (NOTE: I have NOT tested this, just used the info you gave to shape it into a script):

 public class DragToDeceleration : MonoBehaviour {
 
     private Rigidbody ownRigidbody;
     private float dragToSimulate = 0;
 
     private void Awake () {
         ownRigidbody = GetComponent<Rigidbody>();
         UpdateDragToSimulate();
     }
 
     private void UpdateDragToSimulate () {
         if (ownRigidbody.drag != 0) {
             dragToSimulate = ownRigidbody.drag;
             ownRigidbody.drag = 0;
         }
     }
 
     private void FixedUpdate () {
         UpdateDragToSimulate();
 
         ownRigidbody.velocity *= 1 - Mathf.Clamp01(dragToSimulate * Time.fixedDeltaTime);
     }
 
 }
avatar image Servail Harinezumi · Jul 12, 2018 at 04:53 PM 0
Share

This is how to simulate drag (description what drag does, not conversion) - that's what I'm already know. The thing I need is deceleration value (in meters per second squared) "extracted" from drag to use in formulas. Or maybe specific formulas for stopping distance and speed after traverse that are using drag directly (which I told in op-post, btw).

avatar image Harinezumi Servail · Jul 13, 2018 at 07:41 AM 0
Share

Ah, you are right, now I understand. The velocity change would be ownRigidbody.velocity * Mathf.Clamp01(dragToSimulate * Time.fixedDeltaTime), this is already in the same unit as the velocity (meters / second if you interpret a unit distance as 1m). And if my physics knowledge doesn't fail me (which is quite possible), the deceleration is actually dragToSimulate!

Show more comments

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

151 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

Related Questions

Where can I find details on the physics engine? 2 Answers

Overcome drag 1 Answer

How to negate this force? 2 Answers

Unity GUI with GPU acceleration 2 Answers

limit of speed for 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