1
Programs / Re: Problem with Pseudo 3D Projection
« on: May 17, 2021, 04:10:02 pm »
Yeah excuse me, I did some errors.
I also already solved the problem by simply using my good ol' math skillz ;)
Its just the intersection with the y-axis and in fact this can be calculated by the slope (you calculate for a linear function y = mx+n the case x = 0 => n = intersection point)
You will find a first working demo. At the moment I implement PAINT to work correctly and then clipping, also sectors for different heights are implemented at the moment)
But obviously there are some problems with PAINT as when something is off the screen, it won't be rendered correctly ...
Have fun and stay tuned :)
I also already solved the problem by simply using my good ol' math skillz ;)
Its just the intersection with the y-axis and in fact this can be calculated by the slope (you calculate for a linear function y = mx+n the case x = 0 => n = intersection point)
You will find a first working demo. At the moment I implement PAINT to work correctly and then clipping, also sectors for different heights are implemented at the moment)
Code: QB64: [Select]
- mdx = 1280 / 2: mdy = 720 / 2 ' The Middle of the screen
- wheight = 500 ' our wall height
- pheight = 0
- ' lines start and end + player position
- px = 0: py = 0
- ' lines will be in an array
- wall(1, 1) = -10: wall(1, 2) = 10: wall(1, 3) = 10: wall(1, 4) = 10: wall(1, 5) = 10: wall(1, 6) = -1000
- wall(2, 1) = 10: wall(2, 2) = 10: wall(2, 3) = 10: wall(2, 4) = -10: wall(2, 5) = 20: wall(2, 6) = 0
- wall(3, 1) = 10: wall(3, 2) = -10: wall(3, 3) = 5: wall(3, 4) = -15: wall(3, 5) = -20: wall(3, 6) = 0
- wall(4, 1) = -5: wall(4, 2) = -15: wall(4, 3) = -10: wall(4, 4) = -10: wall(4, 5) = -5: wall(4, 6) = 0
- wall(5, 1) = -10: wall(5, 2) = -10: wall(5, 3) = -10: wall(5, 4) = 10: wall(5, 5) = 10: wall(5, 6) = 0
- ' players angle
- angle = 0
- scale = 100
- pi = 3.14159
- fwd = 0: bwd = 0: left = 0: right = 0: strafel = 0: strafer = 0: scaler = 0
- ' here goes the action now
- ' transform the wall into players position
- wx1 = wall(i, 1): wy1 = wall(i, 2): wx2 = wall(i, 3): wy2 = wall(i, 4)
- tx1 = wx1 - px: ty1 = wy1 - py: tx2 = wx2 - px: ty2 = wy2 - py
- ' rotate this
- ' now we know the start and end point
- ' draw the line around the player
- ' now we have our static player and the rotated map
- ' next thing to do is now project the 2d point to a 3d space
- ' render nothing behind us
- ' Clip the lines if one is partially behind the player
- ' this will be done with intersection calculation
- ' clip the line at y-axis
- iy = ry1 - (ry1 - ry2) / (rx1 - rx2) * rx1 ' line intersection calculation with simple linear function y = 0
- ' Calculations here
- zx1 = -ry1 * scale / rx1
- zd1 = -wheight / rx1 - wall(i, 5) / rx1 - pheight
- zu1 = wheight / rx1 - wall(i, 6) / rx1 - pheight
- zx2 = -ry2 * scale / rx2
- zd2 = -wheight / rx2 - wall(i, 5) / rx2 - pheight
- zu2 = wheight / rx2 - wall(i, 6) / rx2 - pheight
- ' draw the lines
- 'LINE (mdx * zx1 / 2 + mdx, mdy * zu1 / 2 + mdy)-(mdx * zx2 / 2 + mdx, mdy * zu2 / 2 + mdy), 14
- 'LINE (mdx * zx1 / 2 + mdx, mdy * zd1 / 2 + mdy)-(mdx * zx2 / 2 + mdx, mdy * zd2 / 2 + mdy), 14
- 'LINE (mdx * zx1 / 2 + mdx, mdy * zu1 / 2 + mdy)-(mdx * zx1 / 2 + mdx, mdy * zd1 / 2 + mdy), 6
- 'LINE (mdx * zx2 / 2 + mdx, mdy * zu2 / 2 + mdy)-(mdx * zx2 / 2 + mdx, mdy * zd2 / 2 + mdy), 6
- ' draw the lines
- notrender:
- NEXT i
- ' modified key input to detect several keypresses
- _LIMIT 20
- kpress = _KEYHIT
But obviously there are some problems with PAINT as when something is off the screen, it won't be rendered correctly ...
Have fun and stay tuned :)