Creating a 'Tracing' Game and Needing to Figure out Vectors

edited May 2016 in Arduino

Hey guys,

I am creating a game that uses an Arduino/IMU as a controller input, and am interested in figuring out the best way to create a good way to 'map' a score that is calculated based off the distance from a point to a reference line.

I do have a section of some code, this is what I'm working with at the moment (writing in Python):

x_avgVal = (x_total / len(lx))/10000 # X input value
y_avgVal = (y_total / len(ly))/10000 # Y input value

if(posY < 700): # Reference line only drawn if level hasn't been completed. Very hacky...
    stroke(255)
    noFill()
    line(width/2,height/2-200,width/2,height/2+200)
    noStroke()
    fill(255)

posX = posX - x_avgVal
posY = posY + y_avgVal
ellipse(posX ,posY,3,3)

posScore = (500 - posX) / 300 #Calculating score

cScore = cScore - abs(posScore)

if(posY >= 700):

    if(state == True):
        background(0)
        text("You scored a " + nf(cScore,2, 2) + "% accuracy", width/2, height/2)
        text("Press space to play again", width/2, height/2+100)
    state = False
    posY = 1000 #Once its hit a trigger it will stay until reset

    if keyPressed: #Reset the score
        if key == ' ':
            cScore = 100
            posY = height/2 - 200
            posX = width/2
            background(0)
            state = True`

The problem is this is very hacked and the only thing it will work with is a line, as the 'score' is only calculated based of the Y difference. What do you think the best way would be to do this for something like a circle, or a square, triangle, custom shapes etc etc.

Tagged:

Answers

  • Quickly hacked together - but you can just grab pixels under the mouse cursor, and check if the mouse is over the border of rectangle, ellipse etc, or is outside the border.

    int hit;
    int miss;
    color bg = color(0,0,0);
    color match = color(255,0,0);
    
    void setup(){
      size(400, 400);
      noLoop();
      strokeWeight(3);
      background(bg);
      setShape();
    }
    
    void setShape(){
     stroke(match);
     noFill();
     rect( random(100,width-100), random(80,height-80), 100, 80);
    
    }
    
    void mouseDragged() 
    {
    color cursor = get(mouseX, mouseY);
    
    if(cursor==match){
    
    fill(255);
    
    noStroke();
    
    ellipse(mouseX,mouseY,3,3);
    
    ++hit;
    
    }
    
    if(cursor==bg){
    fill(0,0,255);
    noStroke();
    ellipse(mouseX,mouseY,3,3);
       ++miss; 
       }
    
    }
    
    
    void mouseReleased() {
    
    if(hit+miss!=0){
    loop();
    background(bg);
    fill(255);
    textAlign(CENTER);
    text("Accuracy was " + nf(100*(hit/(hit+miss)),2,2) + "%", width/2, height/2);
    text("Press any key to play again", width/2, height/2+25);
    hit=0;
    miss=0;
    noLoop();
    }
    
    }
    
    
    void keyPressed() {
    loop();
    background(bg);
    setShape();
    noLoop();
    }     
    
Sign In or Register to comment.