Hello, in school we are currently learning coding and for a while i’ve been trying to get an object to rotate, when I press certain keys. Here is the code I am working with as of right now:
int X;
int Y;
int SIVU;
int SIVU2;
void setup ()
{
fullScreen ();
colorMode (HSB, 100);
frameRate (60);
X = 0;
Y = 0;
SIVU = 40;
SIVU2 = 70;
}
void draw ()
{
background (#1CD332);
fill (#A8AFA9);
noStroke ();
rect (100, 900, 700, 100);
rect (100, 100, 100, 900);
fill (#FF05E7);
stroke (0);
strokeWeight (1);
rect (X, Y, SIVU, SIVU2);
}
void keyPressed ()
{
if (key == 'd')
{
X = X + 5;
}
if (key == 's')
{
Y = Y + 5;
}
if (key == 'a')
{
X = X - 5;
}
if (key == 'w')
{
Y = Y - 5;
}
}
So what i want is the object to rotate 90 degrees, to the left when I press A, to the right, when I press D and go back to the start position with W and S. Please help me i’ve tried everything I myself could.
Thank you.
In your case, you can simply change the way you draw your rectangle using a boolean variable rotated. You switch it to true when pressing ‘a’ or ‘d’ and to false when pressing ‘w’ or ‘s’.
Then in draw depending on the value of rotated you switch your SIVU and SIVU2 variables.
You can also change the rectMode to CENTER to make it more fluid.
int X;
int Y;
int SIVU;
int SIVU2;
boolean rotated;
void setup ()
{
fullScreen ();
colorMode (HSB, 100);
frameRate (60);
X = 0;
Y = 0;
SIVU = 40;
SIVU2 = 70;
rotated = false;
}
void draw ()
{
rectMode(CORNER);
background (#1CD332);
fill (#A8AFA9);
noStroke ();
rect (100, 900, 700, 100);
rect (100, 100, 100, 900);
fill (#FF05E7);
stroke (0);
strokeWeight (1);
rectMode(CENTER);
if (rotated) {
rect (X, Y, SIVU2, SIVU);
} else {
rect (X, Y, SIVU, SIVU2);
}
}
void keyPressed ()
{
if (key == 'd')
{
X = X + 5;
rotated = true;
}
if (key == 's')
{
Y = Y + 5;;
rotated = false;
}
if (key == 'a')
{
X = X - 5;
rotated = true;
}
if (key == 'w')
{
Y = Y - 5;
rotated = false;
}
}