 # Rotate on Command

I am attempting to make a cube that can rotate in any direction on command. I am currently trying to see if I can make it rotate at all on command, and I think that it does rotate, but I am trying to think about how to animate the rotation. Any help would be appreciated. Current code below.

```boolean rotate=false;
void setup(){
size(1000,1000,P3D);
background(0);
smooth();
fill(255);
strokeWeight(5);
}
void draw(){
background(0);
translate(width/2,height/2);
if(rotate){
rotateY(HALF_PI);
}
box(500);
}
void keyPressed(){
if(key==CODED){
if(keyCode==UP){
rotate=true;
}
}
}
```
1 Like

3D looks better when using lights(); after background in draw

Anyway code looks good.

Does it work? When you press UP?

An animation would be possible when you have an angle variable that you use with rotateY and then say angle += 0.21; when rotate is true

This animation could stop when angle reached HALF_PI or a maximum value that would be increased by 1 every time UP is hit

1 Like

I have attempted to integrate what you have suggested, but it may not be the way you thought of it.

``````boolean rotate=false;
float angle=0;
void setup(){
size(1000,1000,P3D);
background(0);
smooth();
noFill();
stroke(255);
}
void draw(){
background(0);
translate(width/2,height/2);
box(500);
if(rotate){
rotateY(angle);
angle+=0.0001;
}
}
void keyPressed(){
if(key==CODED){
if(keyCode==UP){
rotate=true;
}
}
}
``````

I thought that by making the rotateY depend on the angle variable. I made it so that when rotate is true, the angle variable starts to increase in value, and the rotateY would rotate at the angle variable. However, it did not work. I think that it has something to do with the rotateY and the angle value.

1 Like

like that?

``````boolean rotate=false;
float angle=0;

void setup() {
size(1000, 1000, P3D);
noFill();
stroke(255);
println("use key [space] for animation");
}

void draw() {
background(0);
angle += 0.001;
translate(width/2, height/2);
if (rotate) rotateY(angle);
box(500);
}

void keyPressed() {
if (key == ' ' )  rotate = !rotate;
}

``````
1 Like

I tested the code you posted, and I liked how it worked, although when I pressed space again, the box was reset back to its original person. I looked at the code, and I believe this is because the box is put after the rotation code.

``````boolean rotateU=false;
boolean rotateD=false;
boolean rotateL=false;
boolean rotateR=false;
float angleX=0;
float angleY=0;
float anglechgU=HALF_PI/90;
float anglechgD=HALF_PI/90;
float anglechgL=HALF_PI/90;
float anglechgR=HALF_PI/90;
void setup() {
size(1000,1000,P3D);
noFill();
stroke(255);
println("press a to rotate right, b for left");
}
void draw(){
background(0);
translate(width/2,height/2);
if(rotateU){
rotateX(angleX);
angleX+=anglechgU;
}else{
anglechgU=0;
}
if(rotateD){
rotateX(angleX);
angleX-=anglechgD;
}else{
anglechgD=0;
}
if(rotateL){
rotateY(angleY);
angleY-=anglechgL;
}else{
anglechgL=0;
}
if(rotateR){
rotateY(angleY);
angleY+=anglechgR;
}else{
anglechgR=0;
}
box(500);
}
void keyPressed(){
if(key=='w'){
rotateU=!rotateU;
}
if(key=='s'){
rotateD=!rotateD;
}
if(key=='a'){
rotateL=!rotateL;
}
if(key=='d'){
rotateR=!rotateR;
}
}
``````

I edited the code to my liking, and created some new variables to make the rotation a little more regulated. I still do not understand why the box keeps reappearing in its original position.

1 Like

That’s probaly because when you rotate 90 degrees and then 180 degree, they look exactly the same. Try instead of `float anglechgU=HALF_PI/90;` better `float anglechgU=HALF_PI/76;` so it’s not 90, then 180… which would look exactly the same…

my idea

here is my idea

``````boolean rotateU=false;
boolean rotateD=false;
boolean rotateL=false;
boolean rotateR=false;

float angleX=0;
float angleY=10;

float anglechgU=HALF_PI/90;
float anglechgD=HALF_PI/90;
float anglechgL=HALF_PI/90;
float anglechgR=HALF_PI/90;

void setup() {
size(1000, 1000, P3D);
noFill();
stroke(255);
println("press w to rotate ");
}

void draw() {
background(0);

fill(255);
text("Use w to move on", 19, 19);

lights();

translate(width/2, height/2);
rotateX(angleX);

if (rotateU) {
angleX+=anglechgU;

if (angleX>=max1) {
anglechgU=0;
rotateU=false;
} else {
//
}
}

fill(255, 0, 0);
box(500);
}

void keyPressed() {
if (key=='w') {
rotateU=true;
anglechgU=HALF_PI/90;
}
}
``````
3 Likes

Hello,

You are setting them to “original” position in your code in the “else” part of if statement.

Comment out all your else statements:

``````if(rotateU){
//rotateX(angleX);
angleX+=anglechgU;
}
//else{
//  //anglechgU=0;
//}
``````

Move the rotateX() and rotate(Y) from the if statements to:

``````  rotateY(angleY);
rotateX(angleX);
box(500);
``````

Use a key like “r” to reset everything in a separate if statement. 1 Like

anyhow you got the main idea:

• translate
• rotate
• box

next step could be some mouse integration?

I am actually planning to get the sides of cube to have different colors, so I was hoping to get the rotation down first, before putting the colors in the desired positions.

I didn’t mean the colors of the sides.

I was referring to your sentence:

I still do not understand why the box keeps reappearing in its original position.

That’s because when you have rotation by certain amounts, after 90 degree rotation they are the same angle. See my example where I avoid that.

Hello,

This is not correct:

This should be “after 360”.
They are not “the same angle”; mathematically sin(θ) = sin( θ+360) and will yield the same rotation and plot in this example.

I am sure this was just a typo!

Processing reference:
https://processing.org/tutorials/trig/  I mean: after 90 degrees they look the same as 0 degrees, 180 degrees, 270 degrees, when you rotate around one axis only. So better around by 76 degrees for example

1 Like

by that i meant that after I deactivate the rotation command, the box appears in its original position after the rotation stops, even if it does not rotate 90 degrees.

ah, never mind.

That was because rotateX was inside the if I think:

``````  if(rotateU){
rotateX(angleX);
``````

I have taken your idea and used the rotation code bits to rotate the cube along the Y axis as well as the X axis originally.

``````boolean rotateU=false;
boolean rotateD=false;
boolean rotateL=false;
boolean rotateR=false;
float angleX=0;
float angleY=0;
float anglechgU=HALF_PI/60;
float anglechgD=HALF_PI/60;
float anglechgL=HALF_PI/60;
float anglechgR=HALF_PI/60;
void setup(){
size(1000,1000,P3D);
noFill();
stroke(255);
println("press w to rotate ");
}
void draw(){
background(0);
fill(255);
text("Use w to rotate Up",19,19);
text("Use s to rotate Down",19,38);
text("Use a to rotate Left",19,57);
text("Use d to rotate Right",19,76);
lights();
translate(width/2,height/2);
rotateX(angleX);
if(rotateU){
angleX+=anglechgU;
if(angleX>=max1){
anglechgU=0;
rotateU=false;
}
}
if(rotateD){
angleX-=anglechgD;
if(angleX<=max1){
anglechgD=0;
rotateD=false;
}
}
rotateY(angleY);
if(rotateL){
angleY-=anglechgL;
if(angleY<=max2){
anglechgL=0;
rotateL=false;
}
}
if(rotateR){
angleY+=anglechgR;
if(angleY>=max2){
anglechgR=0;
rotateR=false;
}
}
fill(255,0,0);
box(500);
println("X angle=",angleX);
println("Y angle=",angleY);
}
void keyPressed(){
if(key=='w'){
rotateU=true;
anglechgU=HALF_PI/60;
}
if(key=='s'){
rotateD=true;
anglechgD=HALF_PI/60;
}
if(key=='a'){
rotateL=true;
anglechgL=HALF_PI/60;
}
if(key=='d'){
rotateR=true;
anglechgR=HALF_PI/60;
}
}
``````

I think that I will need to create some vectors for the rotation code, because when I rotate the cube in the desired direction, for example, rotate it upward 90 deg/.5 pi, it seems that the entire dimensional plane moves around with it, because after the X rotation is finished, and if I rotate it to the right by pressing d, it seems like the cube rotates along its Z axis. In fact, it is actually rotating along the angleY variable with the rotateY bit I put in there. The two println functions in there were used to prove this.

1 Like

I thought so, but I was not sure, so i guess this is confirmation.

1 Like

You don’t need a `void keyPressed`, you can check in `void draw` if a key is pressed.

``````if (keyPressed) {
if (key == 'r' ) {
...
``````
1 Like