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.
IndexProgramming Questions & HelpPrograms › A Simple Program about Rotation
Page Index Toggle Pages: 1
A Simple Program about Rotation (Read 1436 times)
A Simple Program about Rotation
Feb 11th, 2010, 7:23am
 
Hello friends.Some codes are given blew.Why don't the cube change its rotation direction when I press the mouse?Please Help Smiley
int sign;
float ct;
float st;
void setup()
{
size(200, 200, P3D);
sign=1;
}

void draw()
{
 background(0);
 ct = cos(frameCount/100.0);
 st = sin(-frameCount/100.0);

 applyMatrix(  sign*ct, 0.0,  sign*st,  100.0,
            0.0, 1.0, 0.0,  100.0,
            -sign*st, 0.0,  sign*ct,  0.0,
            0.0, 0.0, 0.0,  1.0);  
 stroke(255);
 box(50);
}

void mousePressed()
{
 print(sign*ct);
 sign*=-1;
 print(sign*ct);

}
Re: A Simple Program about Rotation
Reply #1 - Feb 11th, 2010, 8:35am
 
I am guessing you started with code taken from the applyMatrix() documentation:

Code:
size(100, 100, P3D);
noFill();
translate(50, 50, 0);
rotateY(PI/6);
stroke(153);
box(35);
// Set rotation angles
float ct = cos(PI/9.0);
float st = sin(PI/9.0);    
// Matrix for rotation around the Y axis
applyMatrix(  ct, 0.0,  st,  0.0,
0.0, 1.0, 0.0,  0.0,
-st, 0.0,  ct,  0.0,
0.0, 0.0, 0.0,  1.0);  
stroke(255);
box(50);


This is an advanced topic, and I suggest that if your aim is simply to rotate the object, you should try rotateY() and similar functions.

I don't know exactly why your code does what it does, but it is clear that you are not using a "usual" transformation matrix for rotation - for starters, the "angle" term in your ct and st calculations should be the same (not +something and -something).

What you should be thinking about doing is modifying the angle slightly for each frame; if you negate it, that is a large change. What you want is to decrement the angle to make the cube turn in the opposite direction.

Code:
float angle = 0; // current rotation angle
float angleStep = 0.01; // amount to adjust rotation angle by

// setup() { ... }

void draw()
{
 // maybe translate(width, height) here to centre coordinates

 angle += angleStep; // increase or decrease the rotation angle
 rotateY(angle);

 // draw something
}

void mousePressed()
{
 angleStep *= -1; // reverse the _change_ in rotation angle
}


If you are intent on using applyMatrix(), I suggest you use the 'proper' matrix without trying to inject extra sign*blah terms; the idea is to adjust the angle, not change the functions.

-spxl
Re: A Simple Program about Rotation
Reply #2 - Feb 11th, 2010, 6:45pm
 
Thanks.SmileyBut my code seems alright,and here I just ommited the continuity of the cube's rotation:)What happend confuses me though.
Re: A Simple Program about Rotation
Reply #3 - Feb 11th, 2010, 9:40pm
 
I don't understand... Does the program do what you want it to do, or not?

If it doesn't do what you want/expect, then it isn't alright, is it?

-spxl
Re: A Simple Program about Rotation
Reply #4 - Feb 12th, 2010, 2:59am
 
Thank you.I mean that the syntax of my code 'seems' alright,despite the discontinuity effect it causes.And in fact the cube doesn't swap to rotate in another direction around Y-axis when mouse pressed. SmileyCould you point out what's going wrong with my program?Is this a bug of PDE? SmileyThank you indeed.
Re: A Simple Program about Rotation
Reply #5 - Feb 12th, 2010, 6:19am
 
Here is a solution Smiley

Code:
int sign;
float ct;
float st;
float angle = 0;

void setup()
{
size(200, 200, P3D);
sign=1;

}

void draw()
{
background(0);
// Change the 0.006 to change speed of rotation
// increment the angle by sign*0.006
angle += (sign*0.006);
ct = cos(angle);
st = sin(angle);

applyMatrix( ct, 0.0,  st,  100.0,
           0.0, 1.0, 0.0,  100.0,
           -st, 0.0,  ct,  0.0,
           0.0, 0.0, 0.0,  1.0);  
stroke(255,0,0);
box(50);
}

void mousePressed()
{
print(sign*ct);
sign*=-1;
println(" " + sign*ct);

}


The applyMatrix now has the correct parameters for a rotation about the Y axis so don't change them.

I agree with subpixel that using applyMatrix is advanced stuff especially since Processing provides rotateY(). I don't know why you are so determined to use it. Is there a reason?
Re: A Simple Program about Rotation
Reply #6 - Feb 12th, 2010, 6:38pm
 
Thank you so much.I need to make more complicated movements,e.g.,moving the cube around an aribitrary line in space.SmileyAnd the problem is encontered in such simple test--
ApplyMatrix works well for yours and not for mine.Why..It still remains a problem. Smiley
Re: A Simple Program about Rotation
Reply #7 - Feb 13th, 2010, 2:00am
 
If you put "weird shit" into the matrix, you will get a weird result.

If you don't know how to do the matrix manipulations, I suggest you should use a different approach.

translate()
scale()
rotateX()
rotateY()
rotateZ()

You can use sequence of these to get whatever result you want.

An arbitrary matrix does not guarantee a sane output, eg it may be skewed or have some other inappropriate mangling happening.

-spxl
Re: A Simple Program about Rotation
Reply #8 - Feb 13th, 2010, 3:05am
 
Thanks.I have to admit that my program is "weird shit" Grin, it looks like 'shit',but it smells like 'void' Grin It looks horrible,but what causes the cube responseless to mouse event is still unknown to me.I hope not to annoy you.  Smiley
Re: A Simple Program about Rotation
Reply #9 - Feb 13th, 2010, 3:28am
 
Oops...I've made a mistake of leaving 'sign' outside brackets... It's a horrible mistake...I can't remember how many times I've come into such fault since high school:'(

int sign;
float acRate;
void setup()
{
size(200, 200, P3D);
sign=1;
acRate=1/100;
}

void draw()
{
 //translate(100,100);
 background(0);
float ct = cos(sign*frameCount/100.0);
float st = sin(-sign*frameCount/100.0);

//rotateY(0.5);
//if (frameCount>100) rotateY(0.2);
// Matrix for rotation around the Y axis
applyMatrix(  ct, 0.0,  st,  100.0,
            0.0, 1.0, 0.0,  100.0,
            -st, 0.0,  ct,  0.0,
            0.0, 0.0, 0.0,  1.0);  
print(sign);
//rotateY(PI/10);
stroke(255);
box(50);
}

void mousePressed()
{
 sign*=-1;
 printMatrix();
}
That's ok.Thank you friends.
Re: A Simple Program about Rotation
Reply #10 - Feb 13th, 2010, 5:22am
 
The weird stuff starts here:
Code:
float ct = cos(sign*frameCount/100.0);
float st = sin(-sign*frameCount/100.0);


Why "-sign"? It doesn't make any sense. Also, what is the problem with increasing or decreasing the angle instead of completely negating it? You have an easy chance to simply reverse the apparent rotation - take it!  Wink

As for an explanation of what your original program was actually "doing", that is far beyond my comprehension of transformation matrices. The fact that you were using "cos(theta)" and "sin(-theta)" (where sin(+theta) is what makes sense for standard rotation calculations) says to me that all bets are off... You're matrix was smoking some whacky weed.

-spxl
Re: A Simple Program about Rotation
Reply #11 - Feb 13th, 2010, 9:56am
 
Quote:
ApplyMatrix works well for yours and not for mine.Why..It still remains a problem.


Reason 1
Because as I said Quote:
The applyMatrix now has the correct parameters for a rotation about the Y axis so don't change them.
and your program had incorrect parameters.

Reason 2
applyMatrix can be used to rotate TO a particular angle so while
Code:
ct = cos(frameCount/100.0);
st = sin(-frameCount/100.0);

was gradually increasing the angle means that it gently rotated about the Y axis in one direction. But simply negating the sine and cosine of the angle does NOT change the direction of rotation in fact it represents a very different angle for instance (using degrees)
-cos(angle) has the same value as cos(angle + 180) and
-sin(angle) has the same value as sin(angle + 180)
this is what causes the discontinuity. Since you are still increasing the angle with the above code the direction of rotation will not change.

If you look at the code I provided earlier in this thread more closely I have a variable called angle which is simple incremented/decremented depending on the value of sign.
Code:
 angle += (sign*0.006);
ct = cos(angle);
st = sin(angle);

it then calculates the cosine and sine then uses these in applyMatrix to rotate about the Y axis.

You also said
Quote:
Thank you so much.I need to make more complicated movements,e.g.,moving the cube around an arbitrary line in space.

Using applyMatrix to rotate about arbitrary axis is extremely challenging and it might be better to see if you can't achieve the same thing using Processing functions.

translate()
scale()
rotateX()
rotateY()
rotateZ()

as suggested by subpixel

Re: A Simple Program about Rotation
Reply #12 - Feb 13th, 2010, 7:29pm
 
Thank you. Smiley
Page Index Toggle Pages: 1