We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Ease rotation (Read 1426 times)
Ease rotation
Nov 14th, 2008, 4:02pm
 
Hi, I am brand new to processing, and got the blue processing book, very good by the way.

My question is can you show me how can I do a rotation with easing?

I try the formulas on the book, ease, how to do use rotate, radinas and all that but I cant work out how can I apply the ease formula to a rotation or angular movement

Thanks!
rS
Re: Ease rotation
Reply #1 - Nov 17th, 2008, 2:29am
 
nardove, you should be able to use the same principles with rotation easing as with linear motion easing.  For example, if in each draw() loop you were updating your object's x and y values like this:

x += (destx - x) * easing;
y += (desty - y) * easing;

...then you could use a similar formula for rotation:

rotationValue += (destRotation - rotationValue) * easing;

If you want to post some of your code, I'll take a look.
Re: Ease rotation
Reply #2 - Jul 30th, 2009, 3:43am
 
I know this is a very old thread but instead of starting a new one I'll ask this question on the same subject:

I ease my angle and it works fine until the target angle suddenly goes from -pi to +pi. I know why this happens, the target pass the x-axis of the aimer, on the left side on the y-axis. The thing that is easing will make a sudden jump almost 2 pi in one direction.

I guess the problem could be solved by keeping previous direction values and compare them to the new ones, but is there an easier way to avoid this "spin" that appears?

thanks
h
Re: Ease rotation
Reply #3 - Jul 30th, 2009, 4:47am
 
It might help to post some example code.  It sounds like you're easing to a moving target.  How are you calculating the angle to this target?
Re: Ease rotation
Reply #4 - Jul 30th, 2009, 9:30am
 
Yes I am. I'll make an example of my code where I aim for the mouse:

Code:
float goalDirection;
float currentDirection;
float easeValue = 20;

void setup(){
 size(400,400);
}
void draw(){
 background(200);
 goalDirection = atan2(mouseY - height/2, mouseX - width/2);
 currentDirection += (goalDirection - currentDirection) / easeValue;
 pushMatrix();
 translate(width/2,height/2);
 line(-width/2,0,0,0);
 rotate(currentDirection);
 line(0,0,width/2,0);
 popMatrix();
}
void mousePressed(){
 println("The goalDirection: "+goalDirection+"\t The difference with easing: "+(goalDirection-currentDirection)/easeValue);
}


Excuse me for the long piece of code. If you click once above and once below the line you will print the goalDirection and see that it will jump from ≈-pi to ≈+pi. This makes my "aimer" jump one lap instead on pass over the line. Mathematical this is not strange at all but I need a work around.

I hope you understand my problem, thanks for helping!
h
Re: Ease rotation
Reply #5 - Jul 30th, 2009, 1:17pm
 
"Excuse me for the long piece of code."  Grin We have seen worse!

I had to solve a similar problem in JavaFX (an arrow going to mouse click after the proper rotation).
Adapted to your code, it is:
Code:
  goalDirection = atan2(mouseY - height/2, mouseX - width/2);
if (goalDirection < 0) goalDirection += TWO_PI;
float diff = goalDirection - currentDirection;
if (diff > PI) diff -= TWO_PI;
if (diff < -PI) diff += TWO_PI;
currentDirection += diff / easeValue;

I still see sometime a full rotation (near zero, going down quickly), hard to reproduce.
Re: Ease rotation
Reply #6 - Jul 30th, 2009, 3:04pm
 
PhiLho  wrote on Jul 30th, 2009, 1:17pm:
"Excuse me for the long piece of code."  Grin We have seen worse!


Indeed! At first I thought it must have been lost somewhere when posted, but then realised hampus must be a master of understatement Grin

Glad you had a solution to this one - though I imagine it must be something that crops up a lot when rotating.  I thought it might help to add TWO_PI so all values were positive, but hadn't got beyond that.
Re: Ease rotation
Reply #7 - Jul 30th, 2009, 6:43pm
 
blindfish wrote on Jul 30th, 2009, 3:04pm:
   PhiLho  wrote on Jul 30th, 2009, 1:17pm:
   "Excuse me for the long piece of code."  ;D We have seen worse!

Indeed! At first I thought it must have been lost somewhere when posted, but then realised hampus must be a master of understatement ;D


Haha, okey .-)

I almost wrote that code, but i never made the float called "diff", instead i tried to use "value - previousValue" all the time, and this did not work. I now made it fit for my purpose and it works excellent! Thanks alot!

And for teaching me this:
Code:

if (condition) doThis;


This compact if-statement is saving alot of space! Neat!

Thanks again!
h
Re: Ease rotation
Reply #8 - Jul 31st, 2009, 5:56am
 
hampus wrote on Jul 30th, 2009, 6:43pm:
And for teaching me this:
Code:
if (condition) doThis; 


This compact if-statement is saving alot of space! Neat!

Thanks again!
h


Just remember that you can only have a single 'doThis' statement if you neglect the {braces}.

I don't especially like missing out the braces; nor when people start their opening braces on the next line from the condition... But I guess it all comes down to personal coding style and preferences.  The important thing is to be consistent Wink

Either that or use Python  Shocked
Re: Ease rotation
Reply #9 - Jul 31st, 2009, 6:10am
 
blindfish wrote on Jul 31st, 2009, 5:56am:
I don't especially like missing out the braces; nor when people start their opening braces on the next line from the condition...

I fully agree!
I very rarely use this form, actually, mostly for quick hacks like this one... And only for very simple expressions, like the arithmetic operations there.
In general, I prefer to always put braces (except sometime when the instruction is a break or return) for consistency and readability.

So... use it wisely!  Wink
Re: Ease rotation
Reply #10 - Jul 31st, 2009, 9:17am
 
Yeah I started using it and changed some of my code but it turns out to be harder to read. And, as you said, braces at new lines is confusing aswell. I'll keep it the easy way, except for this:

I managed to solve the final problem with the easing. I added a print and saw that the temporary angel became odd things such as ≈9.123 and other odd numbers above 2 pi / beneath -2 pi, so I made this:

Code:

while(temporaryDirection < -PI) temporaryDirection += TWO_PI;
while(temporaryDirection > PI) temporaryDirection -= TWO_PI;


This way the temporary angel will always be inside the gap from -pi to +pi and always run the direction that you want. (I think, as I tried for some laps forth and back. But as usual things tend to change and do exactly what you don't want them too, so I don't promise anything, haha, don't want to risk lying.)
h
Page Index Toggle Pages: 1