playMaker

Author Topic: 2d torque and smooth rotation  (Read 7917 times)

klvo

  • Playmaker Newbie
  • *
  • Posts: 13
2d torque and smooth rotation
« on: January 29, 2014, 07:45:52 PM »
Hi, I'm applying 2d torque to a sprite using a 2d rigidbody and the object rotates fine on the Z axis. I'm using unity 4.3.3 with its new 2d system.

But I need to do some actions depending on the rotation speed, and it looks like the rotation is irregular instead of smooth, sometimes increasing and sometimes decreasing even if I'm not applying any forces or modifications to the object and it is only decreasing the rotation on its own.

I need to be exact because I'm modifying the pitch of a sound depending on the rotation speed, and if the change in speed is not smooth then the audio results are a terrible mess. (I have other possible ways of doing this smoothly by lerp, but I still need to know if the object is accelerating or not, without any sudden changes).

I'm measuring the rotation speed by getting the difference between the rotation on this frame and the previous frame. I'm also fixing the difference that happens when the rotation goes over 360 and continues from 0, but that's not the problem because I get irregular readings while doing small rotations far from the 360 point.
I tried looking for actions in playmaker that will help me get the rotation speed, but all of them are written for the 3d system and don't seem to work with 2d.

Is this something that normally happens with physics, so precise measuring should be avoided?
Might it be a problem with the new 2d system in unity?
Is there a different and better way to do this instead of comparing the rotation on different frames?

Thanks in advance

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 2d torque and smooth rotation
« Reply #1 on: January 31, 2014, 07:07:34 AM »
Hi,

 Yes, the problem here is that the rotation should be computed on "FixedUpdate" not "update", which is not in synch with the physics engine hence the unreliable data.
 
What exactly are you trying to achieve to begin with? maybe there is another way.

bye,

 Jean

klvo

  • Playmaker Newbie
  • *
  • Posts: 13
Re: 2d torque and smooth rotation
« Reply #2 on: February 02, 2014, 07:09:08 PM »
Hi Jean, thanks for the answer,

Ok, so the actions not running on FixedUpdate is the reason it is not smooth.

I'm using Get Rotation each frame on the 2d object that had torque applied to it.
Then I compare it to the rotation it had the previous frame, and the difference is the rotation velocity.

I'm not sure where or how should the FixedUpdate be used in this case.
Would it be in the state that checks the rotation speed as I described?
Does that mean I would have to make a custom Get Rotation action so it uses Fixed Update?

Cheers

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 2d torque and smooth rotation
« Reply #3 on: February 03, 2014, 04:52:04 AM »
Hi,

 yes, I would make a custom action indeed. just doing exactly the same thing but on fixed update. it's important to duplicate it so that when future versions fo playmaker are imported, your changes remains, Editing official actions is never a good idea, only good for validating a possible need for a new action.

bye,

 Jean

klvo

  • Playmaker Newbie
  • *
  • Posts: 13
Re: 2d torque and smooth rotation
« Reply #4 on: February 03, 2014, 05:10:21 AM »
Hi Jean,

Gotcha!
I've never modified actions before, but I know about making custom actions instead of overwriting the official ones.

So i'll mod the Get Rotation action then.
I'll share it as well in case someone needs it too.

Once I get it working I'll mark this threat as solved.

Thanks again for your help.

klvo

  • Playmaker Newbie
  • *
  • Posts: 13
Re: 2d torque and smooth rotation
« Reply #5 on: February 03, 2014, 06:44:42 AM »
Hi Jean,

I modified a copy of the action: changed the file name and the corresponding class name, and then changed "public override void OnUpdate()" to "public override void OnFixedUpdate()"
The rest is identical.

The script was compiled without problems, but it doesn't seem to work.
The variable the script is set to modify is not changing values like it was doing with the original action.

Any suggestions on what is wrong?
Thanks

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 2d torque and smooth rotation
« Reply #6 on: February 03, 2014, 07:28:47 AM »
Hi,

 ah ah :) cause as always, you should first study an action that does implement a OnFixedUpdate() :)

 you'll find that you need to explicitly tell PlayMaker that you will use such fixed update by adding this lines.

Code: [Select]
        public override void Awake()
        {
            Fsm.HandleFixedUpdate = true;
        }


look at "AddExplositionForce" for an example.

This trick is used for optimization, to avoid using costly functions ( OnfixedUpdate is affecting perfs, while being mandator in some cases... so...)

bye,

 Jean

klvo

  • Playmaker Newbie
  • *
  • Posts: 13
Re: 2d torque and smooth rotation
« Reply #7 on: February 03, 2014, 06:18:24 PM »
Hi Jean,

Thanks for the explanation!
I did check other forum threads about fixed update before modifying the action, but didn't see anything about adding that line on Awake on the ones I read.
I also didn't think to check the actions mentioned, sorry about that.


Now, I added the code and the action works and the variable is being updated, but the values I get from the state are still very irregular, very similar like when using OnUpdate....

The values I get from the action I modified look right, but the result I'm getting from the other actions on the same state are still incorrect.
In that state I'm also using the Float Operator, Float Abs and Set Float Value actions every frame to calculate rotation speed.
The results from that calculation seems irregular.

Then I'm using Get Fsm Float from a different FSM to get that result every frame.


Would I need to modify all of the actions I'm using so they are all on fixed update?
Or maybe my approach to doing this wrong?

Thanks for all your help so far!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 2d torque and smooth rotation
« Reply #8 on: February 04, 2014, 04:57:01 AM »
Hi,

 yep, the whole lot of actions involved would need to be set to work on FixedUpdate, else, somewhere along the process the values will be out of synch indeed.

bye,

 Jean

Wledig

  • Playmaker Newbie
  • *
  • Posts: 7
Re: 2d torque and smooth rotation
« Reply #9 on: February 10, 2014, 04:10:24 PM »
Hey, it's me again (I won't bother you much I promise), but could you please add this line to the Custom Actions Overview page:

Quote
Custom actions that implement OnFixedUpdate now need to set Fsm.HandleFixedUpdate = true; in Awake. See AddForce.cs for an example.


In my two tiny custom actions, I used OnFixedUpdate without having added

Code: [Select]
   public override void Awake()
        {
            Fsm.HandleFixedUpdate = true;
        }

And they work fine from my point of view. Probably because they are tiny actions. But if I hadn't had a look at the code in SetVelocity2d.cs, I would have never known.
« Last Edit: February 10, 2014, 04:21:03 PM by Wledig »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 2d torque and smooth rotation
« Reply #10 on: February 11, 2014, 01:35:00 PM »
Hi,

 Added. Is it ok like that?

https://hutonggames.fogbugz.com/default.asp?W350

bye,

 Jean

Wledig

  • Playmaker Newbie
  • *
  • Posts: 7
Re: 2d torque and smooth rotation
« Reply #11 on: February 11, 2014, 04:45:41 PM »
Parfait merci! Promis je t'embĂȘte plus. ;)

Perfect thanks! I promise I won't bother you again. ;)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: 2d torque and smooth rotation
« Reply #12 on: February 11, 2014, 11:52:02 PM »
y a plein de francais en fait!!  ;D