Rotating a moving square
in
Programming Questions
•
2 months ago
Hello! I'm working on a program in which I need to rotate a 2D rectangle around it's centre using the "speed" displayed while running the following programme, which goes from -1 to +1 (I'll make it it's own variable eventually :P ). In addition, the rectangle may or may not be traveling while doing this (eventually, the rectangle will move about the screen using WASD keys, which I have already figured out, and take into account inertia and momentum), and I may end up needing to smooth the value so it doesn't decrease so abruptly. Those last two parts aren't actually part of my question, but I just wanted to fill you in. All I need to do is apply the rotation to a square. A few people I've talked to have told me that this is "easy", but I haven't seen this type of rotation in any of the examples I've found. Any help would be greatly appreciated!
import java.awt.AWTException;
import java.awt.Robot;
Robot robot;
PVector middle;
void setup() {
size(displayWidth, displayHeight, P2D);
textAlign(LEFT, TOP);
textSize(30);
strokeWeight(3);
noCursor();
middle = new PVector(width/2,height/2);
try {
robot = new Robot();
}
catch (AWTException e) {
e.printStackTrace();
}
}
void draw() {
PVector dir = PVector.sub(mx(), middle);
float magnitude = dir.mag();
background(255);
fill(0);
String info = "Degrees: " + int(degrees(dir.heading())) + "\nMagnitude: " + int(magnitude);
text(info, 5, 5);
translate(middle.x,middle.y);
line(0, 0, dir.x, dir.y);
robot.mouseMove(int(middle.x),int(middle.y));
if (int(degrees(dir.heading()))==180) { text("Speed:"+(-(magnitude)/100), 100, 100); }
if ((int(degrees(dir.heading()))==0)&&(mouseX!=displayWidth/2)) { text("Speed:"+((magnitude)/100), 100, 100); }
}
PVector mx() {
return new PVector(mouseX>width/2+100?width/2+100:mouseX<width/2-100?width/2-100:mouseX, displayHeight/2);
}
boolean sketchFullScreen() {
return true;
}
1