• 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
2
Question by HROP · Sep 26, 2012 at 06:20 AM · physicsoptimizationlag

How to fix the delay caused by Mathf.Sin

Hi,

I'm making a simple interactive mechanism of a reciprocating engine. I am experiencing some weird behavior from Unity.

As the piston gets closer to top dead center and bottom dead center, it is in the right position in relationship to the lever arm. However, when the piston is moving between these positions, a gap develops, as if the crankshaft was lagging, and the piston was moving too fast.

alt text

Here's the code that makes it all happen

     coursemod = Mathf.Sin ((shaft.transform.rotation.eulerAngles.y)*Mathf.PI/180f);
     course = (longrodscale * (25.4f) * coursemod) + longrodscale *25.4f;
     tempv = this.transform.position;
     tempz = course -5f;    
     tempv.z = tempz;
     this.transform.position = tempv;`

List:

  • What I am doing is first getting the sine of the rotation angle of the shaft so I get values between -1 and 1 which are assigned to "coursemod".

  • I use this to affect the course value, which is computed by using "longrodscale" variable that can be modified by the user, and multiplying it by "coursemod". This give me "course".

  • I then have to use a temp variable to store this, and retrieve the transform.position of my piston axis, so I can alter it.

After many minutes or hours I've lost count, I came to the conclusion that I have no idea how to fix this behavior and I am not sure what exactly is causing it. If anyone has experienced anything like it let me know, any hint is welcome.

Comment
Add comment · Show 6
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 HROP · Sep 26, 2012 at 01:12 PM 0
Share

Yes, I have clicked that button, when I copy pasted my code, but for some reason it turned out like that, even if I tried to edit it afterwards, it stayed the same.

avatar image Bunny83 · Sep 26, 2012 at 02:37 PM 0
Share

Unfortunately not ;) If you place a bullet-list after a code block, the code block doesn't work. Insert at least one normal line of text. Yes, the UA markup is strange...

avatar image HROP · Sep 27, 2012 at 12:21 AM 0
Share

Good to know ! I didn't understand why it didn't display properly.

avatar image Fattie · Sep 27, 2012 at 06:33 AM 0
Share

so @HROP has this in fact solved your problem dude? vast numbers of people have now put vast amounts of time in to this!

I'm interested whether your problem was in fact the forgotten rootL2-R2sin2Theta factor .. or was it just some other trivial problem?

avatar image HROP · Sep 30, 2012 at 09:52 AM 0
Share

@Fattie There were many issues:

  • there is the L1 = sqrt (L2 - sin^2) that I wasn't considering.

  • there is the fact that I was using the converted Euler angles of the shaft, ins$$anonymous$$d of assigning manually a value for the angle.

  • Also, I was using "look at" to assign the correct angle for the conrod, kept the graphical representation of the rod lagging even at low RP$$anonymous$$

  • I was calculating many times the sames values of cos or sin, in different scripts.

  • For some reason I forgot to change one of my updates back to Update (it was set at FixedUpate)

I just rewrote a "main" script where most of the calculations are being done in one update, and then the other game objects go and grab these values. I still get some lag at higher RP$$anonymous$$, but it's acceptable now for what I'm planning to do with it.

I'll post up the code, when it is cleaner. Thank you all for your help !

avatar image Fattie · Sep 30, 2012 at 10:07 AM 0
Share

alright it sounds like you had some general bugs.

FTR there is no reason, at all, that there should be "lag" of any type ay any speed. it's all really deter$$anonymous$$istic and should be perfect. the engine (err, Unity engine) will be able to draw and calculate this trivially.

I don't understand your 3e point but it could be you are simply "looking at" the wrong place. very often, in fact always, you have to have like "marker objects" that are offset one way or another from an object of interest, and the "look at" concept looks at that marker rather than the thing itself.

if you have some further problem ask

i guess the takeaway here is if you draw a bad-ass diagram you get lots of "vote" points :-)

1 Reply

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

Answer by Bunny83 · Sep 26, 2012 at 12:01 PM

Fattie is absolutely right, the vertical movement is not a pure sine curve. Here, i made a quick sketch ;) The point is that the red line (L1) has a fix length, but it's not parallel to the movement axis (except at the top and bottom point). You need to calculate the projection of L1 onto the movement axis and add it to the cos-position (or sin depending on where you start counting)

Motor

However i guess your problem is that you shouldn't use eulerAngles this way. EulerAngles are caculated from a Quaternion and might not represent the angle you would expect due to gimbal lock avoidance.

You should use a float variable as angle that you adjust yourself. You can rotate the axis also by this angle. Never rely on an angle returned by eulerAngles.

Comment
Add comment · Show 7 · 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 shaderop · Sep 26, 2012 at 01:08 PM 0
Share

Up voted for the mad $$anonymous$$SPaint skillz exhibited in your answer.

avatar image HROP · Sep 26, 2012 at 01:34 PM 0
Share

Thanks for your help !

I'll try assign the angle value manually, that might be the cause of this lag. I'll let you know the outcome tonight.

avatar image Bunny83 · Sep 26, 2012 at 02:48 PM 0
Share

btw, i made an octave plot of the movement of the piston. I used extreme values for L1 / L2:

 L1 = 3
 L2 = 2

The curve gets closer to the normal sine the greater the L1/L2 ratio is.

avatar image CHPedersen · Sep 26, 2012 at 03:05 PM 0
Share

This is one of the best answers I've seen in a while, and to a great question, too. The Q&A-pair looks almost schoolbook-like at first glance because of the illustrations. :) +1's all around!

avatar image CHPedersen · Sep 26, 2012 at 03:06 PM 0
Share

PS: I've given up on scolding posters for poor code formatting. UA is fundamentally broken in that regard.

Show more comments

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

12 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

Related Questions

Physics Optimization. 0 Answers

Add 2d rigidbodies to moving objects to increase performance or not? 1 Answer

Help with optimize code 1 Answer

Broadphase CD in Unity - pile of rigid bodies unsolved 0 Answers

Game build works fine in Android but is jittery in iPhone. 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