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 MakerDavid · Oct 02, 2016 at 01:04 PM · c#floatphysics 2d

My computer does not know how to add I think

I am trying to implement the physics of a canon-ball (a little cumbersome code and that is fine and I don't really need to change it as I am learning).

I think I have done physics numerical method correctly but the problem is that my computer adds the numbers wrong, and it gives me terrible results.

alt text

Just take a look at this specific variable in one example of a loop in a "for" loop. I print out the loop number, do calculations and then print out the variables.

That means that for this calculation I need to use the previous vy. The top vy is 22.47091.

As you can see at the bottom which I have highlighted the C is: -0.0009003537

This is a code snippet:

 float c = (a2y * dt * 0.5f); 
 float v3y = (vy + c);

V3y according to Unity becomes 22.69472 but that cannot be correct because 22.47091+(-0.0009003537) = 22.46190646

The loop:

  for (int i = 0; i < 1000; i++) {
 
             Debug.Log(i);
             
             RungakuttaX(vx, sx, vy, sy, dt);
             gameObject.transform.position = new Vector3(sx, sy, 0);
 
         }

The rest of the code:

 private void RungakuttaX(float vx, float sx, float vy, float sy, float dt) {
 
         float v1x = vx;
         float s1x = sx;
         float v1y = vy;
         float s1y = sy;
 
         float a1x = FormelX(v1x, v1y);
         float a1y = FormelY(v1x, v1y); 
 
         float v2x = (vx + (a1x * dt * 0.5f));
         float s2x = (sx + (v1x * dt * 0.5f));
         float v2y = (vy + (a1y * dt * 0.5f));
         float s2y = (sy + (v1y * dt * 0.5f));
 
         float a2x = FormelX(v2x, v2y);
         float a2y = FormelY(v2x, v2y);
 
         float v3x = (vx + (a2x * dt * 0.5f));
         float s3x = (sx + (v2x * dt * 0.5f));
         float c = (a2y * dt * 0.5f); 
         float v3y = (vy + c);
         float s3y = (sy + (v2y * dt * 0.5f));
 
         float a3x = FormelX(v3x, v3y);
         float a3y = FormelY(v3x, v3y);
 
         float v4x = (vx + (a3x * dt));
         float s4x = (sx + (v3x * dt));
         float v4y = (vy + (a3y * dt));
         float s4y = (sy + (v3y * dt));
 
         float a4x = FormelX(v4x, v4y);
         float a4y = FormelY(v4x, v4y);
 
         this.vx = vx + ((dt/6) * (v1x + 2*v2x + 2*v3x + v4x)); 
         this.sx = sx + ((dt/6) * (s1x + 2*s2x + 2*s3x + s4x));
 
         this.vy = vy + ((dt/6) * (v1y + 2*v2y + 2*v3y + v4y));
         this.sy = sy + ((dt/6) * (s1y + 2*s2y + 2*s3y + s4y));
 
         Debug.Log("vy:"+vy+" v1y:" + v1y + " a1y:" + a1y + " v2y:" + v2y + " a2y:" + a2y + " c:" + c +" v3y:" + v3y + " a3y:" + a3y + " v4y" + v4y + " a4y" + a4y + "---");
 
     }
 
     public float FormelX(float vx, float vy) {
 
         float v = Mathf.Sqrt((vx * vx) + (vy * vy));
         float d = (0.5f * p * (v * v) * c * a)/m;
         float dx = -d * (vx / v);  
 
         if (v != 0)
         {
             return dx; 
         }
         else
         {
             return 0; 
         }
     }
 
     
 
     public float FormelY(float vx, float vy) {
 
         float v = Mathf.Sqrt((vx * vx) + (vy * vy));
         float d = (0.5f * p * (v * v) * c * a)/m + g;
         float dy = -d * (vy / v);
 
         if (v != 0)
         {
         return dy;
         }
 
         else
         {
         return 0;
         }
     }

c9c2525bc06551417d58a1e37971240f.png (104.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
1

Answer by goldensun441 · Oct 03, 2016 at 06:42 PM

@MakerDavid You got quite a busy script haha. I'm no expert but I had a few ideas-

I'm guessing you're storing global variables in between calls. You could try printing out another debug log at the beginning of RungakuttaX in addition to the one you already have, to see if you're passing in the same data you expect; or if somehow it was altered between calls.

Maybe its a timing issue with deltas, or your messing with your globals in between calls, you could try local static variables. My only other guess would be to blame calls inside FIxedUpdate() vs Update().

maybe it's just bad math :p

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 goldensun441 · Oct 03, 2016 at 06:50 PM 0
Share

I guess to simplify this- just log the few variables you want to watch immediately before and after the math in question:

Debug.Log(a2y); Debug.Log(dt); float c = (a2y dt 0.5f); Debug.Log(c); Debug.Log(vy); float v3y = (vy + c); Debug.Log(v3y);

If those numbers come out clean, then go look for a ti$$anonymous$$g problem

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

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

How can I change this script so that each instantiated prefab spawns a set amount faster 0 Answers

create graph editior for values 0 Answers

Unity Infinity Math Result 0 Answers

Is there anyway to make a list of prewritten variables? (C#) 2 Answers

C# float is 0 in debug 2 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