Help with terrain collision? (Page 1) ● SmileBASIC Source Forums

Register
This website has been moved, please see this post

# Help with terrain collision?

• #1 ✎ 68 Gaelstrom_Valence Drawing I like to draw! Hobbies Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength First Month Joined in the very first month of SmileBASIC Source Website So, in the current version of my program, I noticed the terrain collision doesn't handle diagonal movement well. At the moment, I have individual checks for each direction, done one after the other. But to fix the above issue I'm making a more dynamic system that does the necessary checks at the same time. Since before, say, since checks for tiles to right of the player came first, if they were also moving upwards, the player might 'hit' what should logically be a roof, but it would count as a wall collision since that check comes first, taking away their momentum, even if the upward checks which come later sets the player below the roof. But now I can't figure out how to have the game check only the tiles on the player's path(Ones the lines intersect), at the moment, it checks every tile in a rectangular field from the player's previous position, and to where their velocity would take them. Any advice? Posted Edited by randomous
• #2 ✎ 477 SquareFingers It sounds like you want to draw a straight line... except instead of plotting pixels on the screen, you want to check if pixels(/tiles) are open air, or are solid. Pick any algorithm for drawing a straight line - like Bresenham's algorithm. Replace the code 'plot a pixel' with 'check the tile'. Note that this algorithm includes 45-degree 'jumps' from one pixel to the next in the line, so the corners of your walls need to be closed. Your application will need some more clever application of the algorithm, though, to follow the direction of the line after a bounce. Posted
• #3 ✎ 68 Gaelstrom_Valence Drawing I like to draw! Hobbies Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength First Month Joined in the very first month of SmileBASIC Source Website
It sounds like you want to draw a straight line... except instead of plotting pixels on the screen, you want to check if pixels(/tiles) are open air, or are solid. Pick any algorithm for drawing a straight line - like Bresenham's algorithm. Replace the code 'plot a pixel' with 'check the tile'. Note that this algorithm includes 45-degree 'jumps' from one pixel to the next in the line, so the corners of your walls need to be closed. Your application will need some more clever application of the algorithm, though, to follow the direction of the line after a bounce.
I tried out a line algorithm, one that i could comprehend. But is there one that goes over every tile/pixel the line crosses with, instead of just the closest one to the line?
Posted
• #4 ✎ 477 SquareFingers If you're using Bresenham's algorithm, you know there's an 'error term' you update for every pixel by adding a 'delta' value. When the error term exceeds 1, you subtract 1, and take a diagonal step rather than a step in the direction of the primary axis. So you want to do steps in cardinal directions rather than diagonals. Where the algorithm says take a diagonal step, and the new error term is less than half of delta, take a step along the primary axis then a step in the other direction. Where the algorithm says take a diagonal step, and the new error term is more than half of delta, take the steps in the opposite order. If it is precisely half of delta, the line goes precisely through the corner, and you really take a step diagonally. Posted
• #5 ✎ 68 Gaelstrom_Valence Drawing I like to draw! Hobbies Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength First Month Joined in the very first month of SmileBASIC Source Website
If you're using Bresenham's algorithm, you know there's an 'error term' you update for every pixel by adding a 'delta' value. When the error term exceeds 1, you subtract 1, and take a diagonal step rather than a step in the direction of the primary axis. So you want to do steps in cardinal directions rather than diagonals. Where the algorithm says take a diagonal step, and the new error term is less than half of delta, take a step along the primary axis then a step in the other direction. Where the algorithm says take a diagonal step, and the new error term is more than half of delta, take the steps in the opposite order. If it is precisely half of delta, the line goes precisely through the corner, and you really take a step diagonally.