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

141
Visualizações
Javascript - move and turn the ball with commands

I have very interesting task, might be someone can propose better algorithm.
We have a ball, which moves alone by the next parameters:

  • speed
  • heading (the angle of moving like 90/180/270/360 grad from defined Ost/West)

The ball have defined list of speed, heading and interval, after which he have to take the next speed and heading.

I'm trying to implement this in JavaScript, but really stuck. For example:

function go(ball, element) {
  let speed = element.speed;
  let heading = ball.heading;
  ball.roll(speed, heading); // this function makes just one coup
 }

Main function works with a list of elements (for example):

[
  {
    "point": 1,
    "heading": 90, //angle
    "speed": 50,
    "duration": 30 //duration
  },
  {
    "point": 2,
    "heading": 180, //angle
    "speed": 50,
    "duration": 6 //duration
  },
  {
    "point": 3,
    "heading": 270, //angle
    "speed": 50,
    "duration": 3 //seconds
  }
]

Main function as a first idee:

function startRolling (elements) {
  for (var element of elements) {
    let distance = element.distance;
    var timerId = setInterval(() => go(bolt, element), distance * 1000);
    setTimeout(() => {
      clearInterval(timerId)
    }, distance);
  }
}

I'm not a big an expert in JavaScript, I know how to implement this in Java, but cannot find solution in JavaScript. Could someone please help me?

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

0

You could also structure it something like this, where each ball is an object, stores its own velocity and encapsulates its own "go" logic. You then manage it as a list/array of Ball objects, iterating through it calling their go, draw, update, whatever, methods at regular intervals.

class Vector {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}
class Ball {
    constructor(x, y) {
        this.pos = new Vector(x, y);
        this.vel = new Vector(/* some random numbers here */);
    }
    go() {
        this.pos.x += this.vel.x;
        this.pos.y += this.vel.y;
    }
}
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