🔍

radial reflection in Processing


float ballX, ballY; // position of the ball
float ballRadius = 20; // radius of the ball
float circleX, circleY; // position of the inner circle
float playRadius = 160; // radius of the inner circle

int centerX = 0;
int centerY = 0;

float ball_angle = 0;

float ball_speed = 2;

boolean[] seqs;

boolean plopp = false;

int currently_pressed_key_id = -1;

int returnCurrentlyPressedButton(){


    return currently_pressed_key_id;
    
    

}

void drawSegments(){

    float pia = TWO_PI/seqs.length;
     for(int i=0;i<seqs.length;i++){
    
        noFill();
        stroke(255,0,0);
       strokeWeight(12);
       
       if( i == currently_pressed_key_id){
         strokeWeight(23);
         seqs[i] = false;
       }else{
         //fill(222);
       }
       
       float ca = pia*i;
       float next_ca = pia*(i+1);
       
      /*
       circle( 
       cos(ca+PI)*160 + centerX,
       sin(ca+PI)*160 + centerY,
       8);
      */
      
       // Draw the arc
      arc(centerX,centerY, playRadius*2, playRadius*2, ca+PI, next_ca-.1+PI,  OPEN);

      }


}

int returnHittetSegment(){
  
   float ha = returnReflectionAngle();
   float id = map(ha,0,TWO_PI,0,8  );
  // println("HITTING: " + int(id));
   return int(id);
  

}

float returnReflectionAngle() {
 
  float dx = ballX - centerX;   // calculate horizontal distance from mouse to center
  float dy = ballY - centerY;  // calculate vertical distance from mouse to center
  float angle = atan2(dy, dx);   // calculate angle between mouse and center
  return angle+PI ;    
   
}


void setup() {
  size(400, 400);

  centerX = width/2;
  centerY = height/2;
  // initialize the positions of the ball and inner circle
  ballX = centerX;
  ballY = centerY;
  circleX = centerX;
  circleY =  centerY;
  noStroke();

  frameRate(60);
  
  seqs = new boolean[8];
 
  // start ball at a random angle
  ball_angle = random(TWO_PI);
}

void draw() {
  
  
  background(255);
  drawSegments();
  
  noStroke();
  
  // move the ball
  ballX += cos(ball_angle) * ball_speed;
  ballY += sin(ball_angle) * ball_speed;

  // debuggin purpose
  float ca = returnReflectionAngle();
  stroke(2);
  strokeWeight(2);
  line(ballX,ballY, ballX+cos(ca)*20,ballY+sin(ca)*20 );


  // draw the ball and inner circle
  stroke(1);
  fill(0,55);
  ellipse(ballX, ballY, ballRadius*2, ballRadius*2);


  fill(0, 22);
  ellipse(circleX, circleY, playRadius*2, playRadius*2);


  // check if ball g
  if ( dist(ballX, ballY, centerX, centerY) >= playRadius - ballRadius && !plopp ) {


     int hid = returnHittetSegment();
    seqs[hid] = true;
    
    
    // check if the player pressed the right button
    int crpbid = returnCurrentlyPressedButton();
    
    if( hid == crpbid ) {
    
      println(" right key for hitted segment: " + hid );
      
      // set new ball angle / reflection!
      ball_angle = returnReflectionAngle() + random(-.15,.15) ;
      plopp = true;
      
    }else{
    
     //  println("wrong key : " + hid );
    }
  
  }
  
   if ( dist(ballX, ballY, centerX, centerY) < playRadius-20){
     plopp = false;
   }
   
   
    if ( dist(ballX, ballY, centerX, centerY) >= playRadius - ballRadius + 30 ) {
      
      println("game over");
    }
}


void keyPressed(){

 
    if(keyCode==49){
     currently_pressed_key_id = 1;
    }
    
     if(keyCode==50){
     currently_pressed_key_id = 2;
    }
    
     if(keyCode==51){
     currently_pressed_key_id = 3;
    }
    
     if(keyCode==52){
     currently_pressed_key_id = 4;
    }
    
    if(keyCode==53){
     currently_pressed_key_id = 5;
    }
    
    if(keyCode==54){
     currently_pressed_key_id = 6;
    }



}