Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
p5: Using Sine wave in WEBGL to change y-axis

I have some spheres that I want to move straight on the x axis with only their y-axis changing based on the sine wave (you know the up and down yo-yo pattern). It is pretty simple in 2d but for some reason, in WEBGL, the angle changes in the sin function is not working for this.y.

let particles = [];
let quantity = 20;

function setup() {
  createCanvas(300, 300, WEBGL);

  for (let i = 0; i < quantity; i++) {
    let particle = new Particle();
    particles.push(particle);
  }
}

function draw() {
  background(15);
  orbitControl();
  directionalLight([255], createVector(0, 0, -1));
  translate(-width / 2, 0);

  particles.forEach(particle => {
    particle.update();
    particle.edges();
    particle.show();
  });
};


// particle.js
function Particle() {
  this.angle = 0;
  this.r = 10;
  this.x = random(width);
  this.y = sin(this.angle) * 200; // I am changing this angle later but it's not working
  this.z = random(-50, 50);
  this.pos = createVector(this.x, 0, 0);
  this.vel = createVector(2, this.y, 0);

  this.update = function() {
    this.pos.add(this.vel);
    this.vel.limit(4);
    this.angle += 3;
    // print(this.pos);
  }

  this.edges = function() {
    if (this.pos.x >= width) {
      this.pos.x = 0;
    }
  }

  this.show = function() {
    noStroke();
    push();
    translate(this.pos.x, this.pos.y, 0);
    sphere(this.r);
    pop();
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You only call sin one time in the construtor. You probably meant to apply it to this.y on every frame based on this.angle, or some other incrementing value. Also, there are duplicate vectors; pick either an x/y pair or use a vector x/y, but not both.

let particles = [];
let quantity = 20;

function setup() {
  createCanvas(300, 300, WEBGL);

  for (let i = 0; i < quantity; i++) {
    let particle = new Particle();
    particles.push(particle);
  }
}

function draw() {
  background(15);
  orbitControl();
  directionalLight([255], createVector(0, 0, -1));
  translate(-width / 2, 0);

  particles.forEach(particle => {
    particle.update();
    particle.edges();
    particle.show();
  });
};


// particle.js
function Particle() {
  this.angle = 0;
  this.r = 10;
  this.x = random(width);
  this.y = 0;

  this.update = function() {
    this.angle += 0.05;
    this.y = sin(this.angle) * 100;
  }

  this.edges = function() {
    if (this.x >= width) {
      this.x = 0;
    }
  }

  this.show = function() {
    noStroke();
    push();
    translate(this.x, this.y, 0);
    sphere(this.r);
    pop();
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda