Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

100
Visualizações
How to modulate from A to B using sin(a) and distance in P5 js?

I am calculating each's box distance from the centre of the coordinate system using dist().

I am trying to use all of the above but am unable to get it right.

I am stuck and trying to do this for a few days, please do help.

function setup(){
  createCanvas(600, 600, WEBGL);
  
  background(220);
  
  camera(-200, -200, -200,   // camera position (x, y, z)
         0   , -100,    0,   // camera target (look at position) (x, y, z)
         0   ,    1,    0);  // camera up axis: Y axis here

  for (let x=0; x < width; x +=20){
    for (let z=0; z < height; z +=20){
      push();
      // ground plane is XZ, not XY (front plane)
       normalMaterial();
             stroke(0);
       strokeWeight(1);
      translate(x, 0, z);  
         let distance = dist(0, 0, x, z);
         let length = (((sin(frameCount) + 1)/2) * 100);
         //box(20);     
         box(50, length);
         pop(); 
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The length of the box depends on the distance of the center to the box. e.g.:

let distance = dist(width/2, height/2, x, z);
let length = (((sin(TWO_PI * distance/600 + frameCount * 0.01) + 1)/2) * 100);  

The speed can be changed by changing (0.01) and the wavelength can be changed by changing (600).

The width and height of the box must be 20:

box(20, max(0.01, length), 20);

You must put the code in the draw function:

function setup(){
  createCanvas(600, 600, WEBGL);
  
  camera(-200, -200, -200,   // camera position (x, y, z)
         0   , -100,    0,   // camera target (look at position) (x, y, z)
         0   ,    1,    0);  // camera up axis: Y axis here
}

function draw() {
  background(220);
  for (let x=0; x < width; x +=20){
    for (let z=0; z < height; z +=20){
        push();
        // ground plane is XZ, not XY (front plane)
        normalMaterial();
        stroke(0);
        strokeWeight(1);
        translate(x, 0, z);  
        let distance = dist(width/2, height/2, x, z);
        let length = (((sin(TWO_PI * distance/600 + frameCount * 0.01) + 1)/2) * 100);   
        box(20, max(0.01, length), 20);
        pop(); 
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda