Input not working correctly?

edited November 2014 in Questions about Code

Hey guys, im trying to make a program that shoots a projectile at specified angle, but my code draws both the projectile shooting at a pre-determined angle, and asks the user for input at the same time. Im at a loss for what to do here.... I want to get user input for an angle, and then draw the projectile being fired AT the angle the user typed in.

float currT, maxT, xpos, ypos, angle, v, g, deltaT;
String typing = "";
String saved = "";
PFont f;
void setup() {
  background(255);
  size(500,500);
  f = createFont("Arial",16,true);
    // Time starts at 0
    currT = 0;
    // Enter the angle below in degrees(angle)
    angle = 45;
    // Below converts angle from degrees to radians
    angle = (angle) * PI / 180;
    // Enter the velocity below (v)
    v = 75;
    // Gravity is equal to 9.8 m/s (g)
    g = 9.8;
    // How quickly it updates is deltaT (for faster draw times, use a higher number)
    deltaT = .01;
    // This is the max time the animation will run (when ypos is less than or equal to 0)
    maxT = 2*v*sin(angle) / g;
    // This is the position on the x-axis
    xpos = v * currT * cos(angle);
    // This is the position on the y-axis
    ypos = (v * currT * sin(angle)) - .5 * (g * (sq(currT)));
}
void draw() {
  int indent = 25;
  textFont(f);
  fill (0);
  text("Click in the applet and type an angle. \nHit return to save what you typed. ", indent, 40);
  text(typing,indent,90);
  text(saved,indent,130);
  noLoop();
}
void keyPressed() {
  if (key == '\n' ) {
    saved = typing;
    typing = "";
    angle = (Float.valueOf(saved));
    angle = (angle) * PI / 180;
    drawYes();
  } else {
    typing = typing + key;
  }
}
void drawYes() {
    // Advance currT
    currT = currT + deltaT;
    // Test if the simulation should be stopped (if the current time is greater than max time)
    if(currT > maxT) {
      noLoop();
    }
    // Calculate the new x & y position based on currT
    xpos = v * currT * cos(angle);
    ypos = (v * currT * sin(angle)) - 0.5 * (g * (sq(currT)));

    // Draw the current information in the window
    drawPosition();
    // Draw the ellipse in the window
    drawShot();
    loop();
}
void drawPosition() {
  fill(255);
  stroke(255);
  rect(0,0,500,30);
  stroke(0);
  fill(0);
      String text = "time:" + nf(currT, 4, 2) 
                  + " xpos:" + int(xpos) + " ypos:" + int(ypos);
      text(text, 10, 20);
}


void drawShot() {
    ellipse(xpos, 500-ypos, 1, 1);
}

Answers

  • edited September 2015

    I believe JOptionPane would be more painless. Check it out this example: ~O)
    http://docs.Oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html

    /** 
     * No Repeat ID Input (v1.10)
     * by GoToLoop (2013/Nov)
     * 
     * forum.processing.org/two/discussion/869/
     * check-array-contents-with-arraylist
     */
    
    import static javax.swing.JOptionPane.*;
    
    final StringList ids = new StringList( new String[] {
      "Eric", "Beth", "Katniss"
    } 
    );
    
    void draw() {
      println(ids);
    
      final String id = showInputDialog("Please enter new ID");
    
      if (id == null)   exit();
    
      else if ("".equals(id))
        showMessageDialog(null, "Empty ID Input!!!", 
        "Alert", ERROR_MESSAGE);
    
      else if (ids.hasValue(id))
        showMessageDialog(null, "ID \"" + id + "\" exists already!!!", 
        "Alert", ERROR_MESSAGE);
    
      else {
        showMessageDialog(null, "ID \"" + id + "\" successfully added!!!", 
        "Info", INFORMATION_MESSAGE);
    
        ids.append(id);
      }
    }
    
  • So i've more or less modified it to the below, but im getting an error "unexpected token: void" for the drawPosition method

    import static javax.swing.JOptionPane.*;
    float currT, maxT, xpos, ypos, angle, v, g, deltaT;
    void setup() {
      background(255);
      size(500,500);
        // Time starts at 0
        currT = 0;
        // Enter the angle below in degrees(angle)
        angle = 45;
        // Below converts angle from degrees to radians
        angle = (angle) * PI / 180;
        // Enter the velocity below (v)
        v = 75;
        // Gravity is equal to 9.8 m/s (g)
        g = 9.8;
        // How quickly it updates is deltaT (for faster draw times, use a higher number)
        deltaT = .01;
        // This is the max time the animation will run (when ypos is less than or equal to 0)
        maxT = 2*v*sin(angle) / g;
        // This is the position on the x-axis
        xpos = v * currT * cos(angle);
        // This is the position on the y-axis
        ypos = (v * currT * sin(angle)) - .5 * (g * (sq(currT)));
    }
    void draw() {
        // Advance currT
        final String id = showInputDialog("Please enter a new angle");
        if (id == null)   exit();
        else if ("".equals(id)) {
          showMessageDialog(null, "Empty ID Input!!!",
          "Alert", ERROR_MESSAGE);
        } else if (id == angle) {
          showMessageDialog(null, "Angle \"" + id + "\" already chosen!!!",
          "Alert", ERROR_MESSAGE);
        } else {
          showMessageDialog(null, "Angle \"" + id + "\" successfully added!!!",
          "Info", INFORMATION_MESSAGE);
          angle = (Float.valueOf(id));
          angle = (angle) * PI / 180;
    
        currT = currT + deltaT;
        // Test if the simulation should be stopped (if the current time is greater than max time)
        if(currT > maxT) {
          noLoop();
        }
        // Calculate the new x & y position based on currT
        xpos = v * currT * cos(angle);
        ypos = (v * currT * sin(angle)) - 0.5 * (g * (sq(currT)));
    
        // Draw the current information in the window
        drawPosition();
        // Draw the ellipse in the window
        drawShot();
    }
    void drawPosition() {
      fill(255);
      stroke(255);
      rect(0,0,500,30);
      stroke(0);
      fill(0);
          String text = "time:" + nf(currT, 4, 2) 
                      + " xpos:" + int(xpos) + " ypos:" + int(ypos);
          text(text, 10, 20);
    }
    
    
    void drawShot() {
        ellipse(xpos, 500-ypos, 1, 1);
    }
    
  • You haven't closed draw() curly brace pair! Please use CTRL+T inside IDE in order to format your code and make it easier to spot such mistakes! 3:-O

  • Eheheheh. Fixed. :P

    So, now the simulation for the projectile stops at timestamp 0000.01. O.o I went ahead and separated it to avoid having to put in an angle every single milisecond.

    void draw() {
      final String id = showInputDialog("Please enter a new angle");
      if (id == null)   exit();
      else if ("".equals(id)) {
        showMessageDialog(null, "Empty ID Input!!!", 
        "Alert", ERROR_MESSAGE);
      } else if (Float.valueOf(id) == angle) {
        showMessageDialog(null, "Angle \"" + id + "\" already chosen!!!", 
        "Alert", ERROR_MESSAGE);
      } else {
        showMessageDialog(null, "Angle \"" + id + "\" successfully added!!!", 
        "Info", INFORMATION_MESSAGE);
        angle = (Float.valueOf(id));
        angle = (angle) * PI / 180;
      }
      noLoop();
      drawMain();
    
    }
    void drawMain() {
      // Advance currT
    
    
        currT = currT + deltaT;
        // Test if the simulation should be stopped (if the current time is greater than max time)
        if (currT > maxT) {
          noLoop();
        }
        // Calculate the new x & y position based on currT
        xpos = v * currT * cos(angle);
        ypos = (v * currT * sin(angle)) - 0.5 * (g * (sq(currT)));
    
        // Draw the current information in the window
        drawPosition();
        // Draw the ellipse in the window
        drawShot();
    }
    
Sign In or Register to comment.