So I have this ellipse, and every time the user presses the left or right key, the ellipse will curve to that side. I know I have to use rotate, but I don’t know where I can start. I tried so many ways, but none of them worked.
So the plan is to have the circle keep moving no matter what, but the player can steer the circle using the arrow keys. Does anyone know how to do this? Thanks.
I have a question. Where did you find the numbers like 0.0421? Did you just trial and error, or did you use some Math? I also want to learn what those numbers mean.
TAU and TWO_PI are a full circle in radians; this would be 360 ° in degrees.
My preference is to work with fractions of constants; it is more intuitive.
Example:
// Rotating Line and Constants
// v1.0.0
// GLV 2020-04-05
float theta; //theta, θ in radians
float r; // radius
float x, y; // x and y co-ordinates
public void setup()
{
size(500, 500);
println(TAU, TWO_PI); // The same value
println(TAU/60);
println(TAU/120);
println(TAU/149);
println(TAU/180);
}
public void draw()
{
background(0);
translate(width/2, height/2);
//draw() updates 60 times per second
//if you want one rotation each second; angle = TAU/60 = TWO/PI/60
theta = theta + TAU/60;
// https://processing.org/tutorials/trig/
r = 100;
x = r*cos(theta);
y = r*sin(theta);
// draws a line
stroke(255);
strokeWeight(2);
line(0, 0, x, y);
// draws a point
stroke(255, 0, 0);
strokeWeight(5);
point(x, y);
}
when you click and hold a, you make a circle. But there was a small hick-up in it (due to how keyPressed() works). I changed this using a new function keyPressedThroughout(). Now the circle starts immediately when you hold down a or d. (must be used together with keyReleased())
Make circles of different sizes: When you hold down a or d you make a circle. This depends on the angle change (0.4 deegres) and the speed (1). You can now make circles of different sizes: Use 0…9 (for 0.0 to 0.9 degrees, angle change, amount that gets added to the angle [Steering sensitivity]) and + and - to change the amount that gets added to the angle (when you click + and -). The amount changes in 0.1 steps degrees.
Click c to clear screen and r to reset the cursor.
Click i to text() out the current angle change amount [Steering sensitivity] at the drawing position. As you can see in the image (when holding a or d down) with 0.1 degree change, you have a big circle, with 0.5 degree a much smaller circle and with 0.9 even smaller and so on.
// next steps: record movement of the Turtle / Walker as String, take back moves with Backspace.
// Help screen
float angle=0;
float x=33, y=200;
float prevPosX=x, prevPosY=y;
// how strong does the steering react?
/*
float oneDegreeInRadians = radians(1);
float twoDegreeInRadians = radians(2);
float threeDegreeInRadians = radians(3);
float fourDegreeInRadians = radians(4);
*/
// how strong does the steering react?
float underLyingValueInDegree = .4;
// key w
boolean running=true;
/********* SETUP BLOCK *********/
void setup() {
size(1280, 720);
background(111);
} //func
/********* DRAW BLOCK *********/
void draw() {
// clear screen:
// background(111);
// how fast does the ellipse fly?
float speed=1;
// keep angle between 0 and TWO_PI
angle = angle%TWO_PI;
if (running) {
// calc new pos based on angle
x=cos(angle)*speed+prevPosX;
y=sin(angle)*speed+prevPosY;
// store as prev pos
prevPosX=x;
prevPosY=y;
}
// The Walker itself
fill(255, 0, 0);//RED fill
stroke(0); // BLACK outline
ellipse(x, y,
9, 9);
// show text message upper left corner
textOutput();
// NEW !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
keyPressedThroughout(); // must be used together with keyReleased() !!!!!!!!!!!!!!!!!!!
} //func
// -----------------------------------------------------------------------------
void keyReleased() {
key=0;
}//func
void keyPressedThroughout() {
// keyPressed Throughout
// must be used together with keyReleased() !!!!!!!!!!!!!!!!!!!
if (key=='a') {
angle-= radians(underLyingValueInDegree);
} else if (key=='d') {
angle+= radians(underLyingValueInDegree);
}
}//func
// ---------------------------------------------------------------------------
void keyPressed() {
// executed only once
// 0..9
if (key>='0'&&key<='9') {
underLyingValueInDegree=(key-48)/10.0; // set to 0.0, 0.1, 0.2, 0.3, .... 0.9
}
//----------
// function keys
else if (key=='r') {
//reset
angle=0;
x=33;
y=200;
prevPosX=x;
prevPosY=y;
} else if (key=='c') {
// clear
background(111);
} else if (key=='i') {
// Info
fill(255); // WHITE
text(underLyingValueInDegree,
x+16, y+16);
} else if (key=='w') {
// walk
running =
! running;
}
// ----------
// + and -
else if (key=='+') {
underLyingValueInDegree+=.1; // this could even smaller (more granular)
} else if (key=='-') {
underLyingValueInDegree-=.1;
}
}//func
// --------------------------------------------------------------------------
void textOutput() {
// text message upper left corner
fill(111);
noStroke();
rect(0, 0,
350, 90);
fill(255);
text("Use a and d to steer (0..9 +- rciw). "
+ "Angle is "
+ angle
+ ". \nSteering sensitivity is "
+ underLyingValueInDegree
+ " degree."
+ "\nCursor pos \nx "
+ x
+ "\ny "
+ y,
19, 19);
}
//