to create correct rotation?
in
Programming Questions
•
2 years ago
- //pgraphic variable
- PGraphics rbt;
- int xwall = 20;
- int ywall = 20;
- int rxwall = 20;
- int rywall = 20;
- float xmove = 0;
- float ymove = 0;
- float xpos = 150;
- float ypos = 150;
- int value, rad;
- int xcanvas = 400;
- int ycanvas = 400;
- //button variable
- color currentcolor;
- RectButton rect1, rect2, rect3;
- boolean locked = false;
- void setup() {
- //window size
- size(1280, 800, P2D);
- //pgraphic class
- rbt = createGraphics(xcanvas, ycanvas, P2D);
- //button font
- PFont font;
- font = loadFont ("Verdana-15.vlw");
- textFont(font);
- color baseColor = color(102, 102, 102);
- currentcolor = baseColor;
- //button class
- int x = 30;
- int y = 450;
- int size = 90;
- color buttoncolor = color(153, 102, 102);
- color highlight = color(102, 51, 51);
- String s = "START";
- rect1 = new RectButton(x, y, size, buttoncolor, highlight, s);
- // Define and create rectangle button #2
- x = 130;
- y = 450;
- size = 90;
- buttoncolor = color(153, 153, 153);
- highlight = color(102, 102, 102);
- s = "OFF";
- rect2 = new RectButton(x, y, size, buttoncolor, highlight, s);
- // Define and create rectangle button #3
- x = 230;
- y = 450;
- size = 90;
- buttoncolor = color(102, 153, 102);
- highlight = color(51, 102, 51);
- s = "setup";
- rect3 = new RectButton(x, y, size, buttoncolor, highlight, s);
- }
- void draw() {
- background(0);
- stroke(255);
- button();
- line (0, ycanvas, xcanvas, ycanvas);
- line (xcanvas, 0, xcanvas, ycanvas);
- line (1, 1 ,1 ,ycanvas);
- line (1 , 1, xcanvas, 1);
- rbt.beginDraw();
- //red dot for right measurement
- rbt.stroke(255, 0, 0);
- rbt.pushMatrix();
- rbt.translate((xpos+10), (ypos+10));
- rbt.rotate(radians(rad));
- rbt.translate(0, ymove);
- //rbt.line((rxwall+10), 0, (xwall+10), 0);
- rbt.point((xwall+10), 0);
- rbt.popMatrix();
- //blue dot for left measurement
- rbt.stroke(0, 0, 255);
- rbt.pushMatrix();
- rbt.translate((xpos+10), (ypos+10));
- rbt.rotate(radians(rad));
- rbt.translate(0, ymove);
- rbt.point((-xwall-10), 0);
- rbt.popMatrix();
- //green dot for front measurement
- //rbt.stroke(0, 255, 0);
- //rbt.point(xpos+5, (ypos-ywall));
- rbt.endDraw();
- image(rbt, 00, 00);
- //create robot representation
- pushMatrix();
- translate(xpos+10,ypos+10);
- rotate(radians(rad));
- translate(0, ymove);
- stroke(128);
- noFill();
- strokeWeight(2);
- ellipse(0,0,10,10);
- strokeWeight(1);
- stroke(200);
- line(0,0,0,-20);
- line(0,-20,5,-15);
- line(0,-20,-5,-15);
- popMatrix();
- // translate(0, -ymove);
- }
- void keyPressed(){
- if (key == CODED) {
- if (keyCode == UP){
- // xpos = xpos-(cos(radians(rad))*1);
- // ypos = ypos-(sin(radians(rad))*1);
- ymove=ymove-1;
- } else if (keyCode == DOWN){
- ymove=ymove+1;
- } else if (keyCode == RIGHT){
- rad=rad+5;
- } else if (keyCode == LEFT){
- rad=rad-5;
- }
- }
- if (xpos>=xcanvas){
- xcanvas=xcanvas+150;
- }else
- if (ypos>=ycanvas){
- ycanvas=ycanvas+150;
- }
- }
- void button() {
- //background(currentcolor);
- stroke(255);
- update(mouseX, mouseY);
- rect1.display();
- rect2.display();
- rect3.display();
- }
- void update(int x, int y) {
- if(locked == false) {
- rect1.update();
- rect2.update();
- rect3.update();
- } else {
- locked = false;
- }
- //Turn LED on and off if buttons pressed where
- //H = on (high) and L = off (low)
- if(mousePressed) {
- if(rect1.pressed()) { //ON button
- currentcolor = rect1.basecolor;
- println("on");
- } else if(rect2.pressed()) { //OFF button
- currentcolor = rect2.basecolor;
- println("off");
- } else if(rect3.pressed()) {
- currentcolor = rect3.basecolor;
- println("setup");
- }
- }
- }
- class Button {
- int x, y;
- int size;
- color basecolor, highlightcolor;
- color currentcolor;
- boolean over = false;
- boolean pressed = false;
- String s;
- void update()
- {
- if(over()) {
- currentcolor = highlightcolor;
- } else {
- currentcolor = basecolor;
- }
- }
- boolean pressed()
- {
- if(over) {
- locked = true;
- return true;
- } else {
- locked = false;
- return false;
- }
- }
- boolean over()
- {
- return true;
- }
- void display()
- {
- }
- }
- class RectButton extends Button {
- RectButton(int ix, int iy, int isize, color icolor, color ihighlight, String is)
- {
- x = ix;
- y = iy;
- size = isize;
- basecolor = icolor;
- highlightcolor = ihighlight;
- currentcolor = basecolor;
- s= is;
- }
- boolean over()
- {
- if( overRect(x, y, size, size) ) {
- over = true;
- return true;
- } else {
- over = false;
- return false;
- }
- }
- void display()
- {
- stroke(255);
- fill(currentcolor);
- rect(x, y, size, size);
- fill(0, 0, 0);
- textAlign(LEFT, BASELINE);
- text(s, x+20, y+(size/2));
- }
- }
- boolean overRect(int x, int y, int width, int height) {
- if (mouseX >= x && mouseX <= x+width &&
- mouseY >= y && mouseY <= y+height) {
- return true;
- } else {
- return false;
- }
- }
the question is why does when i press up, the next rotation call would be error and if i continue press up the error will accumulate.. the goal is to create the circle to be controlled by key arrow. can anyone show me my mistakes?
thank you very much...
1