Keeping a moving shape inside an area

Hello! I’am trying to develop an artificial horizon, an aircraft instrument. I made some code but i dont know how to mantain this shape inside the circle of the instrument (when move the mouse the shapes goes outside the image area). I got stuck on this part, i would like some suggestion to solve this problem :upside_down_face:

PImage img;
float dist;
float a,b,c,d,e,f,g;

  
void setup(){
  size(720,720);
  background(0);
  smooth();
  rectMode(CENTER);
  img = loadImage("horizonteartificial.png");
  
}

void draw(){
  
  fundo();
  img(); 
}
void img(){
  
 float rot = rotacao();
 rotate(-rot);
 translate(-width/2,-height/2);
 image(img, 0, 0, width, height); 
}

void fundo(){
    background(0);
  //calcula as distancias para referenciar no programa
  //distancias no eixo Y
  a = dist(mouseX, 0, mouseX, mouseY);                  //distance from the top
  b = dist(mouseX, height/2, mouseX, mouseY);         //distance from the half of the screem
  c= dist(mouseX, height, mouseX, mouseY);            //distance from the botton
  d= dist(mouseX, height-height/4, mouseX, mouseY);   
  e = dist(mouseX, height/4, mouseX, mouseY);         

  //distance on  X
  f = dist(0,mouseY,mouseX,mouseY);                   //distance from the left side
  g = dist(width,mouseY,mouseX,mouseY);               //distance from the right side
//=============================

  float rot = rotacao();
  rotate(rot);
  
  //ground
  translate(-width/2,-height/2);
  if(a<=height/2){
    strokeWeight(0);
    fill(#834022);
    arc(width/2,height/2,width,height,0,PI);            //ground
    //arc(width/2,height/2-b,width,height+b,0,PI);      //ground
    rect(width/2,height/2-b/2,width,b);
    fill(#2D20F5);
    arc(width/2,height/2-b,width,height,PI,2*PI);       //sky
  } 
  
  //ceu
  if(a>height/2){
    strokeWeight(0);
    fill(#834022);
    arc(width/2,height/2+b,width,height,0,PI);          //ground
    fill(#2D20F5);
    arc(width/2,height/2,width,height,PI,2*PI);       //sky
    //arc(width/2,height/2,width,height,PI,2*PI);     //sky
    rect(width/2,height/2+b/2,width,b);
  } 

}

float rotacao(){
  translate(width/2,height/2);
  if(f<width/2){
    dist = width-g;
  }else if(f>width/2){
    dist = f;
  }
  float rot = map(dist,width/2,width,0,PI);
  return rot;
}

the image i’am using on the code is that one:

horizonteartificial
:

Hey There!

My suggestion would be is to not allow the shape to be drawn if it’s larger than the image above ( Simple Collision ). Also how about get pictures of the orange and blue ? And use those rather than draw them as shapes.

1 Like

nice ideia, i’ll try :+1:

Swap these lines that are in void img(){ ? … translate must be first

Chrisir

2 Likes