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

274
Visualizações
P5.js how can i fire circles at the mouse from 2 places?

Basically, I have some code that fires circles at the mouse from a player (also a circle) when the mouse is pressed. I'm trying to create another circle that also fires at the mouse (ideally from a key instead of mouse pressed), but I'm struggling to do so. I've pasted my p5.js code. Would greatly appreciate any suggestions!

var bullets = [];

function setup() {
    createCanvas(600, 600);
    player = new Player();

}

function draw() {
    clear();
    background(51);
    player.show();

    for (let i = 0; i < bullets.length; ++i) {
        bullets[i].toMouse();
        bullets[i].show();
    }
}


function mousePressed() {
    if (mouseX != player.x || mouseY != player.y) {
        bullets.push(new Bullet(mouseX, mouseY, player.x, player.y))
    }

}

function Bullet(X, Y, PX, PY) {
    this.speed = 5;
    this.x = PX;
    this.y = PY;
    this.dir = createVector(X - PX, Y - PY).normalize()
    this.r = 7;

    this.show = function() {
        fill(255, 255, 0);
        stroke(128, 128, 0);
        circle(this.x, this.y, this.r);
        circle(this.x + 50, this.y, this.r);
    }
    this.toMouse = function() {
        this.x += this.dir.x * this.speed;
        this.y += this.dir.y * this.speed;
    }

}

function Player() {
    this.x = width / 2;
    this.y = 450;
    this.r = 20;
    this.speed = 4;
    this.show = function() {
        fill(0, 255, 0);
        stroke(0, 128, 0);
        circle(this.x, this.y, this.r);
        circle(this.x + 50, this.y, this.r);
    }

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

0

I am new to p5 but I think I can help a bit.

I could not get your code to run, but I am pretty sure it would work if you make the bullet calculate the difference in x and y values between it and the player, then adding those to its current position.

So if the bullets position is [3,3], and the players position is [10,10], the vector of the direction the ball should travel is [10-3,10-3]. Then you procedurally add those values to the balls current position. dividing the values should make it so the balls do not instantly teleport.

Excuse me if I am wrong.

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