Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 TobiasWeber · Sep 04, 2021 at 08:53 PM · physicsvelocityaccelerometerfixedupdateaccuracy

Velocity from Rigidbody and from Transform don't match for small dt

Hello,
for simulating an IMU attached to a robot I need to calculate accelerations from GameObject Transformation. A rigidbody cannot be attached to an articulated body without affecting it. Therefore I need to come up with a "Passive"-RigidBody. While checking my script against the RigidBody in a simple example i.e. a cube falling under gravity things look good for typical default dt (0.01, 0.001). But when decreasing fixed dt (<=0.0001) to improve physics simulation quality things get awkward.

A body falling in constant gravity should build up velocity constantly and acceleration should be constant and equal gravity. This is the case for RigidBody.velocity. But velocity and acceleration calculated from differentiating transform.position shows step changes in velocity and jumps in acceleration. Indicating that transform.position is not updated for gravity and perhaps not synchronized with the physics system as I would expect.

Please use script attached on a GameObject with a rigidbody falling in gravity. VelocityError will be Zero for dt=0.01 but significant for dt<=0.0001. (Fixed Time Step). Sync Transformation in Physics script is enabled!

Any idea on how to measure velocity and acceleration with high quality in unity using a passive sensor?

Thanks a lot,
Tobias

 sing System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class IMUtest : MonoBehaviour
 {   
     public Vector3 velocityError; //diff between velcity reported from rigidbody and calculated from transformation
     public Vector3 velocity;           //real velocity in world frame
     public Vector3 acceleration;        //real acceleration in world frame
     public Vector3 angularVelocity { get; private set; }     //real angularVelocity in world frame
     public Vector3 angularAcceleration { get; private set; } //real angularVelocity in world frame
     public Vector3 accel{ get; private set; }                //measured accelerations in [g] in local frame
     public Vector3 gyro{ get; private set; }                 //measured angular rates in [rad/s] in local frame
 
     private Rigidbody rb;
     private Vector3 posOld;
     private Quaternion rotOld;
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         posOld = transform.position;
         rotOld = transform.rotation;
         velocity = new Vector3(0.0f, 0.0f, 0.0f);
         acceleration = new Vector3(0.0f, 0.0f, 0.0f);
         angularVelocity = new Vector3(0.0f, 0.0f, 0.0f);
         angularAcceleration = new Vector3(0.0f, 0.0f, 0.0f);
 
         accel = new Vector3(0.0f, 0.0f, 0.0f);
         gyro = new Vector3(0.0f, 0.0f, 0.0f);
     }
 
     void FixedUpdate()
     {
         FixedUpdateVelocityAndAcceleration();
         FixedUpdateMeasurements();
 
         //just debugging
         velocityError = velocity- rb.velocity;
         Debug.Log(velocityError);
     }
 
     private void FixedUpdateVelocityAndAcceleration()
     {
         //Calculate Linear Velocity and Acceleration in World Coordinates!
         var vNew = (transform.position - posOld) / Time.fixedDeltaTime;
         var aNew = (vNew - velocity) / Time.fixedDeltaTime; 
         posOld = transform.position;
         velocity = vNew;
         acceleration = aNew;
 
         //Calculate Angular Velocity in World Coordinates!
         var deltaRot = transform.rotation * Quaternion.Inverse(rotOld);
         deltaRot.ToAngleAxis(out var angle, out var axis);
         var angularVelocityNew = (angle * Mathf.Deg2Rad) * axis / Time.fixedDeltaTime;
         var angularAccelerationNew = (angularVelocityNew - angularVelocity) / Time.fixedDeltaTime;
         rotOld = transform.rotation;
         angularVelocity = angularVelocityNew;
         angularAcceleration = angularAccelerationNew;
     }
 
     private void FixedUpdateMeasurements()
     {   
         accel = transform.InverseTransformDirection( acceleration - Physics.gravity) / Physics.gravity.magnitude;
         gyro = transform.InverseTransformDirection( angularVelocity );
     }
 
     // Update is called once per frame
     void Update()
     {
         Debug.DrawRay(transform.position, transform.TransformDirection(accel), Color.blue);
         Debug.DrawRay(transform.position, transform.TransformDirection(gyro), Color.yellow);
     }
 }
 

Comment
Add comment · Show 1
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 Eno-Khaon · Sep 04, 2021 at 09:49 PM 0
Share

For comparison purposes, how would using Rigidbody.position compare, as opposed to using Transform.position as you are now? Where Transform.position should essentially update its information for an Update() cycle, it looks like Rigidbody.position should be strictly tied to the current FixedUpdate()/physics cycle.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TobiasWeber · Sep 06, 2021 at 01:16 PM

Hi Eno-Khaon,
as suggested I've compared Rigidbody.position with Transform.position. They are both equal. So update between Rigidbody and Transform happens as documented.
>

Based on your hint I see the mismatch is between Rigidbody.velocity and (Transform.position - old.Transform.position)/Time.fixedDeltaTime. Transform is limited to float accuracy and I already spend significant amount of its accuracy for the "absolute" position. While Rigidbody.velocity either is using double precision math inside or works with better scaled / smaller relative numbers.

Take away is:
Transform is float and that limits the applicability of Unity for the type of engineering task I had intended.

Alternative route is to learn on how to combine an articulated body with RigidBody to directly read it's velocity and acceleration with higher resolution form the Physics system. Or another type of passive sensor running within the physics backend

Any Idea on how DOTS is going to change the available accuracy of the physics sub-system?

Thanks a lot for your support!
Tobias

Comment
Add comment · 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

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

206 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Convert from m/s to m/fixedFrame 1 Answer

rigidbody2D velocity zero? 1 Answer

Hinge Joints Ignoring Angular Velocity? 0 Answers

Naturally make velocity 0 2 Answers

Why Do Inputs Have to Been in Update (Rather than FixedUpdate)? 3 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