Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 PhlexSoftware · Mar 01, 2014 at 06:35 AM · c#vector3accuracyfloatingpoint

Why am I experiencing vector addition inaccuracies?

I have a simple script designed to decide when an object should change direction. The decision only occurs when the object is perfectly inside a tile. Each tile is one square unit in size, enabling a simple modulus calculation to determine when the position is within the tile:

 Vector3 _position = transform.position;
 
 if (_position.x % 1.0f == 0.0f && _position.y % 1.0f == 0.0f) {
     Redirect();
 }

Unfortunately, this condition is never satisfied.

The math behind moving the object is quite simple as well. There is a normalized directional vector multiplied by movement speed, and added to the object's position each frame:

 _position += _direction * 0.01f;
 transform.position = _position;

The position of the object quickly displays accuracy issues. I logged a couple bits of information every frame to find out where the issue is rooted at. The _direction variable is initially set to (0,1,0). Each frame, the log shows the calculation of _direction * 0.01f to be (0,0.01,0), exactly as expected. However, the _position vector loses stability quickly.

From the log:

 Movement: 0,0.01,0;
 Position: -4,0.01,0;
 -----
 Movement: 0,0.01,0;
 Position: -4,0.02,0;
 -----
 Movement: 0,0.01,0;
 Position: -4,0.03,0;
 -----
 Movement: 0,0.01,0;
 Position: -4,0.04,0;
 -----
 Movement: 0,0.01,0;
 Position: -4,0.05,0;
 -----
 Movement: 0,0.01,0;
 Position: -4,0.05999999,0;

The position corrects itself at y=0.11, but loses accuracy again after y=0.33 for some reason.

I know that floating point numbers have accuracy issues, but I've NEVER experienced issues with numbers only 2 decimal places deep. Especially considering the multiplication operation churns out the correct result, yet simple addition results in horribly disfigured results, causes me much confusion.

Suggestions?

Comment
Add comment · 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 whydoidoit · Mar 01, 2014 at 06:38 AM 1
Share

That is just classic float inaccuracy, in Unity these are single precision values. Any attempt to use exact coordinates with vectors is flawed. You need to test the difference against an epsilon accuracy value and adjust the position at that point if you prefer.

avatar image PhlexSoftware · Mar 01, 2014 at 06:55 AM 0
Share

I am unfamiliar with that, but I will research it. Is there a better alternative to the logic used in my code? Would it be more suitable to have x, y, and z variables in the object, perform the operations on them, and simply transfer them into the transform position at the end of each frame for drawing? This should eli$$anonymous$$ate having to use single-precision floats, and will keep the vector separated from the logic.

avatar image whydoidoit · Mar 01, 2014 at 06:58 AM 0
Share

Well I would do this:

  const float EPSILON = 0.0001f;
 
  if($$anonymous$$athf.Repeat(_position.x, 1) < EPSILON && $$anonymous$$athf.Repeat(_position.y, 1) < EPSILON) {
  }

Doing it in variables really won't help doing any kind of == on a floating point value of any precision is considered a bad idea.

avatar image whydoidoit · Mar 01, 2014 at 06:59 AM 0
Share

I always feel "nervous" using % with floats - but maybe it works these days! $$anonymous$$athf.Repeat is an explicit floating point version of modulus.

avatar image PhlexSoftware · Mar 01, 2014 at 07:23 AM 0
Share

I'm not sure how that will apply. The result from the function as it approaches and "hits" the integer whole value, gives me 0.9000001 and 1.192093E-07 respectively. I could then check if each dimension is less than 0.01 or greater than 0.91, but there will be several of these objects on the board, and must remain in sync with each other, falling on integer coordinates at the same time. With the now obvious inaccuracies of floating point arithmetic, I would be concerned that this synchronization could become problematic.

Show more comments

1 Reply

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

Answer by whydoidoit · Mar 01, 2014 at 07:52 AM

I guess this is due to the speed of movement of things though - in your example log you are experiencing tiny fractional inaccuracies of the order of 0.000000001 - is it the fact that the object skips the boundary entirely? In which case we need to deal with the case where it is just over the line and just under the line. That appears to be the issue.

    var diff = Mathf.Repeat(x, 1);
    if(diff <= EPSILON || diff >= 1-EPSILON) { 
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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Question Regarding"new" keyword and Vector3 type 2 Answers

Rotating a raycast around the x,z axis 1 Answer

Instantiate object in C# 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