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

134
Vistas
How do I temporarily multiply a vector in p5.js?

I have an object called Boid which has a position and a velocity vector (the direction of movement). When updating the position, I want to apply a factor which the user can control. The following script does not work, however:

let speedFactor = 0.1; //ranges from 0 to 2

class Boid {
    constructor(x, y) {
        this.position = createVector(x, y);
        this.velocity = p5.Vector.random2D();
    }

    update() {
        this.position.add(this.velocity * speedFactor); // doesn't work
    }
}

I also tried to use things like speedFactor = createVector(0.1, 0.1) or = [0.1, 0.1], but it doesn't work.

If I use this.velocity.mult(speedFactor), this will affect the velocity vector, which I don't want (e.g. because if speedFactor == 0, this.velocity will get messed up). I have tried to create a copy of the velocity vector to overcome this by using Object.assign({}, this.velocity) or JSON.parse(JSON.stringify(this.velocity)), but somehow that doesn't work neither.

Most of the time I get the error p5.Vector.prototype.mult: x, y, or z arguments are either undefined or not a finite number.

What would be the best to do here?

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

0

Copy the vector, then multiply it with the normal mult method

let speedFactor = 0.1; //ranges from 0 to 2

class Boid {
    constructor(x, y) {
        this.position = createVector(x, y);
        this.velocity = p5.Vector.random2D();
    }

    update() {
        this.position.add(this.velocity.copy().mult(speedFactor));
    }
}
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