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

184
Visualizações
Why is my line intersection code not detecting the closest boundaries for following boundaries?

I'm trying to make a raycasting algorithm using p5.js (not very efficiently) and have almost all of the collision set up. I cast a ray towards the edge of every boundary (I have yet to code the ray that goes right past the boundary edge), and that works perfectly. My only problem then is that the rays obviously sometimes go through other boundaries. So, I made a for loop that goes through every Boundary and casts a ray at the same angle it was before. If there is an intersection, I return the magnitude of the ray. If not, I return Infinity. Next, I find the index of the minimum value in the array, which all seems to work. This means that whatever the index of the minimum value is is the index of the boundary I want to calculate an intersection with. The code for the function is as follows:

//a = ray position x, b = ray position y, c = ray position + ray direction x, d = ray position + ray direction y
//the array bounds is array of Bound objects
//intersect() calculates if a ray and a line segment intersect and if so returns a vector with intersection point, otherwise returns vector of 0, 0
function findShortest(a, b, c, d) {
    shortestPossibilities = [];
    for(var i = 0; i < bounds.length; i ++) {
        if(intersect(a, b, c, d, bounds[i].x1, bounds[i].y1, bounds[i].x2, bounds[i].y2).mag() !== 0) {
            //add magnitude of intersection
            sPoss.push(intersect(a, b, c, d, bounds[i].x1, bounds[i].y1, bounds[i].x2, bounds[i].y2).mag());
        } else {
            //impossible to be smallest
            sPoss.push(Infinity);
        }
    }
    //find smallest value
    var minVal = Math.min(...sPoss);
    var indexOfVal = sPoss.indexOf(minVal);
    return indexOfVal;
}

However, this doesn't work. I tried to see why it wasn't working by assigning each boundary a new color ([0] = white, 1 = gray, [2] = orange, [3] = yellow, [4] = green, [5] = cyan, [6] = blue, [7+] = purple.) It seems that finding the closest boundary only works if the closest boundary has a lower index than the original boundary the ray was pointing to, as seen: enter image description here

Does anyone know what I did wrong?

about 4 years ago · Juan Pablo Isaza
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