[Python] Why Is My Pygame DDA Ray Casting Stuck to 0-90 Degrees Angles and Misaligned with...

Discussão em 'Python' iniciado por Stack, Janeiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to add this DDA Algorithm Super Fast Ray Casting in Tiled Worlds using DDA into my Pygame ray caster but I'm getting strange results when attempting to visualize the ray's path.

    The ray is stuck to a 0-90 degree angle (Due to raylengthx and raylengthy being only positive) but also it is not aligned to any cell borders.

    cellStepSizeX = 1000000 if round(dx,10) == 0 else math.sqrt(1+ (dy/dx) * (dy/dx))
    cellStepSizeY = 1000000 if round(dy, 10) == 0 else math.sqrt(1 + (dx/dy) * (dx/dy))

    mapX = self.x // 32
    mapY = self.y // 32

    if dx < 0:
    stepX = -1
    rayLengthX = (self.x -mapX) * cellStepSizeX

    else:
    stepX = 1
    rayLengthX = ((mapX +1) - self.x) * cellStepSizeX

    if dy < 0:
    stepY = -1
    rayLengthY = (self.y - mapY)*cellStepSizeY
    else:
    stepY = 1
    rayLengthY = ((mapY +1) - self.y) * cellStepSizeY

    #collision = False
    while distance < 200:
    if rayLengthX < rayLengthY:
    mapX += stepX
    distance = rayLengthX
    rayLengthX += cellStepSizeX
    else:
    mapY += stepY
    distance = rayLengthY
    rayLengthY += cellStepSizeY


    [img1](https://i.sstatic.net/UDprZwZE.png)

    [img2](https://i.sstatic.net/AJWTID48.png)

    [img3](https://i.sstatic.net/5uqbZgHO.png)

    [img4](https://i.sstatic.net/bLtx4xUr.png)

    Here are some images so you can see what I'm talking about. Green line: Ray marching/Correct angle of the ray Blue line: DDA algorithm ray from current position + raylength Red lines: raylength x and y separated out in their respective directions.

    Continue reading...

Compartilhe esta Página